2

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";
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
  • 1
    When you say it failed -- what did it do exactly? Throw an exception, crash, return NULL, something else? – Lou Franco May 17 '12 at 15:57
  • This looks awesome. Im still using OLEMethods, can you provide me with some code for _document and the Section class. – masche Dec 18 '12 at 09:12

2 Answers2

1

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" );
Alex Myers
  • 6,196
  • 7
  • 23
  • 39
0

Where's your C++ equivalent to

Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary]

Something like

Hf.GetItem(wdHeaderFooterPrimary).GetRange();
Lou Franco
  • 87,846
  • 14
  • 132
  • 192