我正在尝试通过Word Automation编辑.docx标头。如果内容控件放在文本正文中没有问题,但如果内容控件位于页眉或页脚中,我将无法定位它们。是否有任何方法可以在页眉或页脚中定位内容控件(使用Visual Studio 2008 Express)?
答案 0 :(得分:1)
Word在StoryRange
集合中有所谓的 Stories - 大多数例程都在wdMainTextStory
类型WdStoryType
的文档主体上运行,除非你专门改变故事。
这是一种在标题中获取控件的方法:
Dim ad as Document
Set ad = ActiveDocument
For Each objCC In ad.Sections(1).Headers(wdHeaderFooterPrimary).Range.ContentControls
''# Do your thing
Next
答案 1 :(得分:1)
使用Word Automation,在标题中定位ContentControls的一种方法是:
Dim ContControlCollec As Word.ContentControls
ContControlCollec = WordDoc.Doc.Sections(1).Headers(Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage).Range.ContentControls
ContControlCollec.Item(1).Range.Text = Text1
ContControlCollec.Item(2).Range.Text = Text2