使用变量按名称选择ActiveX控件复选框(在Excel中的Word中)? [英] Use a variable to select an ActiveX control Checkbox by name (in Word from Excel)?
问题描述
这行代码有什么问题?
WordDoc.CheckBoxNum.Value = CheckBoxVal
为清楚起见,我正在使用ActiveX控件复选框".我将值(true/false)和复选框名称保存在Excel文件中.该代码将打开我的Word文档,并自动遍历复选框,并设置其值.我在Excel中控制单词的事实可能会更改我的对象?
如果它们是表单域" ...可以正常工作
CheckBoxNum = "Check1"
CheckBoxVal = "True"
WordDoc.FormFields(CheckBoxNum).CheckBox.Value = CheckBoxVal
这是我的完整代码
Sub Doit()
Dim WordApp As Object
Dim WordDoc As Object
Dim filepath As String
Dim CheckBoxNum As String
Dim CheckBoxVal As String
Dim i As Integer, k As Integer
'Open Word
filepath = "C:\test.docx"
Set WordApp = CreateObject("word.application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open(filepath)
'Editing
Range("a1").Select 'first value
i = 0
For i = 0 To 6 ' number of rows in table
'get from excel
CheckBoxNum = ActiveCell.Offset(i, k + 1).Value
CheckBoxVal = ActiveCell.Offset(i, k).Value
'put to word
WordDoc.CheckBoxNum.Value = CheckBoxVal '<-- HELP!
Next i
'// CLEAN UP //
WordDoc.Application.ActiveDocument.Save
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
不幸的是,我们不能使用WordDoc.InlineShapes(CheckBoxNum)
直接按其名称引用ActiveX控件,只有在知道其索引号:WordDoc.InlineShapes(1)
时,此方法才有效. >
这意味着您必须遍历所有控件,将其OLEFormat.Object.Name
与要查找的名称进行比较:
Dim obj As Object
For i = 0 To 6 ' number of rows in table
'get from excel
CheckBoxNum = ActiveCell.Offset(i, k + 1).Value
CheckBoxVal = ActiveCell.Offset(i, k).Value
For Each obj In WordDoc.InlineShapes
If obj.OLEFormat.Object.Name = CheckBoxNum Then
obj.OLEFormat.Object.Value = CheckBoxVal
End If
Next obj
Next i
What is wrong with this line of code?
WordDoc.CheckBoxNum.Value = CheckBoxVal
To be clear, I am working with an ActiveX control "Checkbox". I have the values (true/false) and checkbox names saved in an Excel file. The code opens my word document and automatically loops through the checkboxes, setting their values. The fact I am controlling word from Excel may change my objects?
If they were Form Field... This works--
CheckBoxNum = "Check1"
CheckBoxVal = "True"
WordDoc.FormFields(CheckBoxNum).CheckBox.Value = CheckBoxVal
Here is my whole code
Sub Doit()
Dim WordApp As Object
Dim WordDoc As Object
Dim filepath As String
Dim CheckBoxNum As String
Dim CheckBoxVal As String
Dim i As Integer, k As Integer
'Open Word
filepath = "C:\test.docx"
Set WordApp = CreateObject("word.application")
WordApp.Visible = True
Set WordDoc = WordApp.Documents.Open(filepath)
'Editing
Range("a1").Select 'first value
i = 0
For i = 0 To 6 ' number of rows in table
'get from excel
CheckBoxNum = ActiveCell.Offset(i, k + 1).Value
CheckBoxVal = ActiveCell.Offset(i, k).Value
'put to word
WordDoc.CheckBoxNum.Value = CheckBoxVal '<-- HELP!
Next i
'// CLEAN UP //
WordDoc.Application.ActiveDocument.Save
WordDoc.Close
WordApp.Quit
Set WordDoc = Nothing
Set WordApp = Nothing
End Sub
Unfortunately we cannot refer to an ActiveX control directly by its name using WordDoc.InlineShapes(CheckBoxNum)
, this only works if we know its index number: WordDoc.InlineShapes(1)
.
This means that you have to loop through all the controls, comparing its OLEFormat.Object.Name
to the name you are looking for:
Dim obj As Object
For i = 0 To 6 ' number of rows in table
'get from excel
CheckBoxNum = ActiveCell.Offset(i, k + 1).Value
CheckBoxVal = ActiveCell.Offset(i, k).Value
For Each obj In WordDoc.InlineShapes
If obj.OLEFormat.Object.Name = CheckBoxNum Then
obj.OLEFormat.Object.Value = CheckBoxVal
End If
Next obj
Next i
这篇关于使用变量按名称选择ActiveX控件复选框(在Excel中的Word中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!