Problem with refreshing fields on the header and footer

M

Mahmut Akbolat

I am using ole TOleContainer in Delphi and createing a word document and
adding fields in that way.

var
Val: Variant;
begin
Val := Edit1.Text;

OleContainer1.OleObject.Application.ActiveDocument.Variables.Add('FIELD_NAME
', Val);
end;

then manuallly adding fields from word like that.
Insert/Fields/Document Outomation/ Doc Variable /"FIELD_NAME" <OK>

then value of the variable display in the document then from my program
change the value of the field doing that:

var
Field: Variant;
begin
Field := 'FIELD_NAME';

OleContainer1.OleObject.Application.ActiveDocument.Variables.Item(Field).Val
ue := Edit1.Text;
OleContainer1.OleObject.Application.ActiveDocument.Fields.Update;
end;

After that line

OleContainer1.OleObject.Application.ActiveDocument.Fields.Update;

all the fields in the document getting updated but the fields on the header
and footer is not updated. How can I update the fields by code.

Then I found a method like this:
with OleContainer1 do
begin
OleObject.Application.ActiveWindow.ActivePane.View.SeekView :=
wdSeekCurrentPageFooter;
OleObject.Application.ActiveDocument.Select;
OleObject.Application.ActiveWindow.Selection.Fields.Update;
OleObject.Application.ActiveDocument.Fields.Update;
OleObject.Application.ActiveWindow.ActivePane.View.SeekView :=
wdSeekCurrentPageHeader;
OleObject.Application.ActiveDocument.Select;
OleObject.Application.ActiveWindow.Selection.Fields.Update;
OleObject.Application.ActiveDocument.Fields.Update;
end;

But this is not a effecient method and there must be a shorter way.

thanks for helps.
 
C

Cindy Meister -WordMVP-

Hi Mahmut,
After that line

OleContainer1.OleObject.Application.ActiveDocument.Fields.Update;

all the fields in the document getting updated but the fields on the header
and footer is not updated. How can I update the fields by code.
Word documents contain various "Stories", or "StoryRanges"; these are members
of the Word object model. The main document is a story, the headers/footers
of each section are a story, the elements in a drawing layer are a story...

So, your code snippet above only updates the fields in the main body of the
document. You have to repeat the .Update method for each story in which you
have fields.

Look up StoryRange in the Word VBA Help and you'll see an example in that (or
a related topic) how to address all stories in a document. For a particular
header or footer it would resemble this:

ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Fields.Update

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jan 24 2003)
http://www.mvps.org/word

This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top