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 best solutions below

0
On

Where's your C++ equivalent to

Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary]

Something like

Hf.GetItem(wdHeaderFooterPrimary).GetRange();
0
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" );