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);
wordDocs.AttachDispatch(m_pWord->GetDocuments());
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) {
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());
range.AttachDispatch(finalDocument.GetContent());
CString tempText = range.GetText();
tempText.TrimRight();
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