Problems adding fields and text to footer

J

Jimmy

Hi,

I need to add a footer that looks like the following in a document
that's generated using VBA:

Page X of Y
Creation date: yyyy-mm-dd

Can anyone tell me why I can't get it with this bit of code?

With Doc.Sec(1).Footers(wdHeaderFooterPrimary)
Set rngFooter = .Range
With rngFooter
.Text = "Page "
.Collapse wdCollapseEnd
.Fields.Add Range:=rngFooter, Type:=wdFieldPage
.Collapse wdCollapseEnd
'Here I have : Page X
.Text = "of "
'At this point : Page of X
.Collapse wdCollapseEnd
.Fields.Add Range:=rngFooter, Type:=wdFieldNumPages
'Page of YX
.Collapse wdCollapseEnd
.Text = "Created on : "
'Page of Created on : YX
.Collapse wdCollapseEnd
.InsertDateTime "yyyy-mm-dd", insertasfield:=False
'Page of Created on : yyyy-mm-ddYX
End With
End With

It looks like the collapse method doesn't consider any of the fields
as part of the range. Does anyone have a solution? I would prefer not
to use Selection since my document window is not visible and I wish to
keep it that way.

TIA

Jimmy
 
D

Dave Lett

Hi Jimmy

For some fields, when you insert them programmatically, Word leaves the selection/range at the _beginning_ of the field. To see for yourself, tr

With Doc.Sec(1).Footers(wdHeaderFooterPrimary
Set rngFooter = .Rang
With rngFoote
.Fields.Add Range:=rngFooter, Type:=wdFieldPag
End Wit
End Wit

If you can, you might try to insert the AutoText Entry (Page X of Y

HTH
Dav
 
P

Peter Hewett

Hi Jimmy

The Word Document object does not have a Property or Method called "Sec", use
"Sections" instead and your code will work.

HTH + Cheers - Peter


[email protected] (Jimmy) wrote in @posting.google.com:
 
Top