Watch, Follow, &
Connect with Us

Please visit our new home
community.embarcadero.com.


Welcome, Guest
Guest Settings
Help

Thread: How to Add Header & Footer sections in Word Document


This question is not answered. Helpful answers available: 2. Correct answers available: 1.


Permlink Replies: 2 - Last Post: May 12, 2015 9:52 PM Last Post By: karthik thirumo...
karthik thirumo...

Posts: 23
Registered: 9/26/13
How to Add Header & Footer sections in Word Document  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 12, 2015 12:51 AM
I have below code , i just want to Add header and footer sections in existing word document .

procedure TForm1.AddHeader;
var
  openWord : OleVariant;
  wordText: string;
begin
  openWord := CreateOleObject('Word.Application');
  openWord.Visible := False;
  openWord.Documents.Open(DocName);
  openWord.ActiveDocument.Select;
  openWord.Sections.item(1).Headers.Item(wdHeaderFooterPrimary).Range.Text  := 'Header text';
  wordText:= openWord.Selection.Text;
  openWord.ActiveDocument.Close;
  openWord.Quit;
  openWord := unassigned;
  RichEdit2.Text := wordText;
end;


Please help me to solve this .
Peter Below

Posts: 1,227
Registered: 12/16/99
Re: How to Add Header & Footer sections in Word Document  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 12, 2015 10:29 AM   in response to: karthik thirumo... in response to: karthik thirumo...
karthik thirumoorthi wrote:

I have below code , i just want to Add header and footer sections in
existing word document .

procedure TForm1.AddHeader;
var
  openWord : OleVariant;
  wordText: string;
begin
  openWord := CreateOleObject('Word.Application');
  openWord.Visible := False;
  openWord.Documents.Open(DocName);
  openWord.ActiveDocument.Select;
 
openWord.Sections.item(1).Headers.Item(wdHeaderFooterPrimary).Range.Te
xt  := 'Header text';   wordText:= openWord.Selection.Text;
openWord.ActiveDocument.Close;   openWord.Quit;
  openWord := unassigned;
  RichEdit2.Text := wordText;
end;

And what is your specific problem? Does the code not compile? Does it
not work as intended? What happens instead? Do you get any error
messages? Have you tried to explicitely save the document before you
close it?
The world is full of questions...

By the way: we have a group specific for OLE automation questions.
embarcadero.public.delphi.oleautomation if you are using a NNTP reader,
https://forums.embarcadero.com/forum.jspa?forumID=69 if you use the web
interface.

--
Peter Below (TeamB)

karthik thirumo...

Posts: 23
Registered: 9/26/13
Re: How to Add Header & Footer sections in Word Document  
Click to report abuse...   Click to reply to this thread Reply
  Posted: May 12, 2015 9:52 PM   in response to: Peter Below in response to: Peter Below
Peter Below wrote:
karthik thirumoorthi wrote:

I have below code , i just want to Add header and footer sections in
existing word document .

procedure TForm1.AddHeader;
var
  openWord : OleVariant;
  wordText: string;
begin
  openWord := CreateOleObject('Word.Application');
  openWord.Visible := False;
  openWord.Documents.Open(DocName);
  openWord.ActiveDocument.Select;
 
openWord.Sections.item(1).Headers.Item(wdHeaderFooterPrimary).Range.Te
xt  := 'Header text';   wordText:= openWord.Selection.Text;
openWord.ActiveDocument.Close;   openWord.Quit;
  openWord := unassigned;
  RichEdit2.Text := wordText;
end;

And what is your specific problem? Does the code not compile? Does it
not work as intended? What happens instead? Do you get any error
messages? Have you tried to explicitely save the document before you
close it?
The world is full of questions...

By the way: we have a group specific for OLE automation questions.
embarcadero.public.delphi.oleautomation if you are using a NNTP reader,
https://forums.embarcadero.com/forum.jspa?forumID=69 if you use the web
interface.

--
Peter Below (TeamB)


The code is compiled but while running the program i got an error like "Section not supported by automation object" in the below line ,
openWord.Sections.item(1).Headers.Item(wdHeaderFooterPrimary).Range.Text := 'Header text';

i got a solution for that issue , below is the code which works for me,


procedure TForm1.AddHeaderandFooter(DocName : String);
var
  openWord : OleVariant;
begin
  openWord := CreateOleObject('Word.Application');
  openWord.Visible := False;
  openWord.Documents.Open(DocName);
  openWord.ActiveDocument.Sections.Item(1).Headers.Item(1).Range.Select;
  openWord.Selection.ParagraphFormat.TabStops.ClearAll;
  openWord.Selection.TypeText(WideString('This is The Header'));
  openWord.ActiveDocument.Sections.Item(1).Footers.Item(1).Range.Select;
  openWord.Selection.ParagraphFormat.TabStops.ClearAll;
  openWord.Selection.TypeText(WideString('This is The Footer'));
  openWord.ActiveDocument.SaveAs(DocName);
  openWord.ActiveDocument.Close;
  openWord.Quit;
  openWord := unassigned;
end;
 


Thank you very much for your support.
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02