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
 
J

Jean-Guy Marcil

Hi Jimmy,

Have you looked up the InsertAfter method instead of ".Text"?
Also, instead of using Collapse, redefine the range with a new "Set" ?

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
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


(e-mail address removed) (Jimmy) wrote in @posting.google.com:
 

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