运行下面的代码会出现“请求的集合成员不存在”所有搜索都没有产生解决方案。
Sub WordTemplate()
Dim objWordapp As Object
Set objWordapp = CreateObject("Word.Application")
fileStr = "\\int.chc.concepts.co.nz\users\CBotting\Documents\VBA programming\SD Basic Template.docx"
objWordapp.Documents.Open FileName:=fileStr
With objWordapp.Selection.Sections(1).Headers(wdHeaderFooterPrimary)
If .Range.Text <> vbCr Then
MsgBox .Range.Text
Else
MsgBox "Header is empty"
End If
End With
End Sub
我尝试了许多不同的寻址header对象的变体
发布于 2016-07-26 14:43:15
问题不在于后期绑定。问题是,如果不引用Microsoft Word xx.x对象库,VBA就不知道wdHeaderFooterPrimary
的值。我告诉VBA wdHeaderFooterPrimary的值,然后你的代码不需要引用单词库集就可以工作。
Sub WordTemplate()
Const wdHeaderFooterPrimary = 1
Dim objWordapp As Object
Set objWordapp = CreateObject("Word.Application")
fileStr = "\\int.chc.concepts.co.nz\users\CBotting\Documents\VBA programming\SD Basic Template.docx"
objWordapp.Documents.Open Filename:=fileStr
With objWordapp.Selection.Sections(1).Headers(wdHeaderFooterPrimary)
If .Range.Text <> vbCr Then
MsgBox .Range.Text
Else
MsgBox "Header is empty"
End If
End With
End Sub
https://stackoverflow.com/questions/38580346
复制相似问题