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 Visual Studio 2008 Express)?

有帮助吗?

解决方案 2

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

其他提示

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 
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top