how to insert header or footer to a word document using c++ automation

1.8k views Asked by At

I want to insert a header or footer in a word document using word automation.

_document ocDoc;
Sections DocSections = Sections(ocDoc.GetSections());
section firstSec = DocSections.Item( 1 );
HeaderFooter Hf = firstSec.GetHeaders();
Range MyRange = Hf.GetRange();
MyRange.SetText( L"salam" );

but code in part "Range MyRange = Hf.GetRange();" failed, how can i insert header or footer in word document using c++?

below code in c# work correctly:

_document ocDoc;
oDoc.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "My Header";
2

There are 2 answers

0
Lou Franco On

Where's your C++ equivalent to

Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary]

Something like

Hf.GetItem(wdHeaderFooterPrimary).GetRange();
0
AudioBubble On

For the people who are still looking for the answer:

HeadersFooters Hfs = firstSec.GetHeaders();
HeaderFooter Hf = Hfs.Item(1);
Range MyRange = Hf.GetRange();
MyRange.SetText( L"salam" );