Click here to Skip to main content
13,150,344 members (30,571 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am using word automation for inserting header into a word document. The MS Word version used is MSWord97. I need to first insert the whole body/text available into a word document and then insert header into the final document.

I am able to insert the whole body/text available into the word document but i am not able insert the header. The code i am using to insert the header is as below:- (Can anybody tell me whats wrong with the code below as I am getting an exception if I try using the below code and could you please correct my code to insert the text in the header???)

CLnkCltWordDocuments wordDocs;
CLnkCltWordDocument otherDocument;
CLnkCltWordDocument finalDocument;
CLnkCltWordDocument  WordDocument = NULL; 
MSWORD97_Range range;
MSWORD97_Windows windows;
MSWORD97_Window window;
MSWORD97_View view;
MSWORD97_Selection selection;
MSWORD97_Panes panes;
MSWORD97_Pane pane;
COleVariant          covTrue((short)TRUE);
COleVariant          covFalse((short)FALSE);
COleVariant          covOptional((long)DISP_E_PARAMNOTFOUND, VT_ERROR);
//Open word
//Open the pageheaderfile using Word
msgBuf.Format("Opening page header file %s ", pageheaderFileName.GetBuffer(0));
m_pControlHelper->LogControllerMessage(msgBuf.GetBuffer(0), eLogDebug);
bool pagehdrFileOpened = false;
  finalDocument.AttachDispatch(wordDocs.Open( COleVariant((LPCTSTR )pageheaderFileName.GetBuffer(0), VT_BSTR), 							  covFalse,   					  covFalse,   						  covFalse,   					  covOptional,					  covOptional,					  covFalse,   						  covOptional,					  covOptional,covOptional 							));
  pagehdrFileOpened = true;
  m_pControlHelper->LogControllerMessage("Checking View using word for headerFileName document...", eLogDebug);
  MSWORD97_View view;
  if (view.GetSplitSpecial() != 0)	//not equal to wdPaneNone
  const long wdSeekCurrentPageHeader = 9;
  const long wdSeekMainDocument = 0;
  HeaderFooter hf;
  //selection.TypeText(tempText); --> 1
  CString tempText = range.GetText();
  range.SetText(tempText); --> 2

I have tried using both selection and range functions but not able to insert any text into the header. When I used selection its breaking at 1 and when used range its throwing exception at 2.

Please give me the correct code which inserts header for me.. Its URGENT
Posted 11-Jul-10 19:39pm
(unknown member)
Updated 12-Jul-10 0:25am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.170924.2 | Last Updated 12 Jul 2010
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100