Vbscript Create Word Footer

J

jb_Live

I am trying create a footer in Word using VBScript.
Currently i am only able to insert 1 wdfield.

How do you insert 2 wdfields into a footer?
How do you insert date saved and filename into a word footer?

Here is what i have so far. My script creates a new word document, then
insert the footer with wdfieldlastsavedon and then wdFieldFileName overwrite
it.

Const wdFieldFileName = 29
Const wdFieldSaveDate = 22

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()


objDoc.Fields.Add objDoc.Sections(1).footers(1).Range, wdFieldSaveDate
'objDoc.Sections(1).footers(1).Range.ParagraphFormat.Alignment = 0

objDoc.Fields.Add objDoc.Sections(1).footers(1).Range, wdFieldFileName
objDoc.Sections(1).footers(1).Range.ParagraphFormat.Alignment = 2
 
J

Jay Freedman

Sorry, that's bad design. Start by creating a template that has the needed
fields in the footer. Pass the full path of the template as the parameter of the
Documents.Add call, and the resulting document will have the fields already in
place.

If you insist on creating the fields in code, then you need to use a Word.Range
object that you can manipulate to put things where you want them. In particular,
you need to collapse that range to its right end so you can add more text
without overwriting what's already there. I think this will do what you had in
mind:

Const wdFieldFileName = 29
Const wdFieldSaveDate = 22
Const wdCollapseEnd = 0

Set objWord = CreateObject("Word.Application")
objWord.Visible = True
Set objDoc = objWord.Documents.Add()

Set objFtrRange = objDoc.Sections(1).footers(1).Range

objDoc.Fields.Add objFtrRange, wdFieldSaveDate

objFtrRange.Collapse wdCollapseEnd
objFtrRange.Text = vbTab & vbTab
objFtrRange.Collapse wdCollapseEnd

objDoc.Fields.Add objFtrRange, wdFieldFileName

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
J

jb_Live

Thanks works great.
This is just a smaller part of the script that will add the footers to
existing docs.
 

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