Click here to Skip to main content
15,892,059 members

How to insert header into a word document using word automation?

deepthi_c785 asked:

Open original thread
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???)

C++
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
Tags: C++, MFC

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900