Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC
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
wordDocs.AttachDispatch(m_pWord->GetDocuments());
//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;
try
{
  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);
  window.AttachDispatch(finalDocument.GetActiveWindow());
  MSWORD97_View view;
  view.AttachDispatch(window.GetView());
  if (view.GetSplitSpecial() != 0)	//not equal to wdPaneNone
  {
     panes.AttachDispatch(window.GetPanes());
     pane.AttachDispatch(panes.Item(2));
     pane.Close();
  }
  const long wdSeekCurrentPageHeader = 9;
  const long wdSeekMainDocument = 0;
  view.SetSeekView(wdSeekCurrentPageHeader);
  HeaderFooter hf;
  hf.AttachDispatch(finalDocument.GetContent());
  //selection.AttachDispatch(finalDocument.GetContent());
  //selection.TypeText(tempText); --> 1
  range.AttachDispatch(finalDocument.GetContent());
  CString tempText = range.GetText();
  tempText.TrimRight();
  //range.InsertAfter(tempText);
  range.SetText(tempText); --> 2
  finalDocument.Save();
 
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 20:39pm
(unknown member)
Edited 12-Jul-10 1:25am
v5

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

  Print Answers RSS
0 George Jonsson 215
1 Kornfeld Eliyahu Peter 169
2 Zoltán Zörgő 139
3 PIEBALDconsult 130
4 OriginalGriff 120
0 OriginalGriff 6,165
1 DamithSL 4,658
2 Maciej Los 4,107
3 Kornfeld Eliyahu Peter 3,649
4 Sergey Alexandrovich Kryukov 3,342


Advertise | Privacy | Mobile
Web01 | 2.8.141220.1 | Last Updated 12 Jul 2010
Copyright © CodeProject, 1999-2014
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