Target header and footer Content Controls in Word 2007
I'm trying to edit a .docx header through Word Automation. If the Content Controls are placed in the body of the text there is no problem, but if the content controls are in the header or footer I'm not able to target them. Is there any way of targetting Content Controls in the header or footer (using Visu开发者_开发知识库al Studio 2008 Express)?
Word has what are called Stories in the StoryRange
collection - most routines run on the main body of the document itself of the wdMainTextStory
type of WdStoryType
, unless you specifically change stories.
Here's a way to get to your controls in the header:
Dim ad as Document
Set ad = ActiveDocument
For Each objCC In ad.Sections(1).Headers(wdHeaderFooterPrimary).Range.ContentControls
''# Do your thing
Next
Using Word Automation one way of targetting ContentControls in the header can be:
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
精彩评论