VBA Word Footers....

M

MikeL

Good day... The fact that I'm working at 5:00pm on a Saturday instead of
outside enjoying the Florida Sunshine should should show how either I can't
see the forest through the trees or, finally going to give in and ask for
help... Checked all the usual sorces, even ordered some books, drove 30 miles
to find a book that didn't help...

Tried to record a macro that created footers... Unfortuneatly, the macro's
wont play... (I would consider this a bug, but some would say it's a
feature...)

Also, read the help files that the MVP suggested (always try that first...)
Teach a man to fish...

We are converting our Word 2003 Macro's to Word 2007. Having difficulties
with the Footer which contains three parts... Left - has Printed: w/current
date... Center has current file name, and the right has Page: x of y...

Tried many variations... Some from books some from help... Here's 2 of the
ones...

1: (Doesn't work - get's error 5941)
WordBasic.ViewFooterOnly
ActiveDocument.AttachedTemplate.BuildingBlockEntries( _
" Blank (Three Columns)").Insert Where:=Selection.Range,
RichText:=True
Selection.TypeText Text:="Printed: "
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"DATE \@ ""M/d/yyyy h:mm am/pm"" ", PreserveFormatting:=True
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:= _
"FILENAME ", PreserveFormatting:=True
Selection.MoveRight Unit:=wdCharacter, Count:=2
ActiveDocument.AttachedTemplate.BuildingBlockEntries("Bold Numbers"). _
Insert Where:=Selection.Range, RichText:=True
ActiveWindow.ActivePane.View.SeekView = wdSeekMainDocument

2. (Works but only one field... Need Building block entries???)

Private Sub test()


WordBasic.ViewFooterOnly
ActiveDocument.Sections(ActiveDocument.Sections.Count).Footers(wdHeaderFooterPrimary).Range.Select

With Selection
..Paragraphs(1).Alignment = wdAlignParagraphRight
..TypeText Text:="Page "
..Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="PAGE ",
PreserveFormatting:=True
..TypeText Text:=" of "
..Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:="NUMPAGES
", PreserveFormatting:=True
End With


Many thanks... Hope you can help....
 
J

Jay Freedman

Hi Mike,

My first reaction to this is, "Why are you using a macro to create a
footer? Why isn't this footer already set up in the template, to be
inherited by every document based on the template?"

Granting that there may be situations where this may be necessary, the
next best thing is to store the entire footer in a Building Block in
the template. Then the macro just has to insert the single Building
Block into the correct footer pane.

If you absolutely insist on doing it the hard way :) then for Pete's
sake don't fiddle around with the Selection and all that garbage.
Using a Range object is the way to go. By default, every footer is
automatically formatted with the Footer style, which has tab stops at
the center and right margin, so you can separate the three pieces of
information with tabs (in code, vbTab). Here's a working sample that
does what it appears your code was trying to do:

Sub footer_demo()
Dim oRge As Range

Set oRge = EndOfLastFooter()
oRge.Text = "Printed: "

oRge.Collapse wdCollapseEnd
oRge.Fields.Add Range:=oRge, Type:=wdFieldEmpty, Text:= _
"DATE \@ ""M/d/yyyy h:mm am/pm"" "

Set oRge = EndOfLastFooter()
oRge.Text = vbTab

oRge.Collapse wdCollapseEnd
oRge.Fields.Add Range:=oRge, Type:=wdFieldFileName

Set oRge = EndOfLastFooter()
oRge.Text = vbTab & "Page "

oRge.Collapse wdCollapseEnd
oRge.Fields.Add Range:=oRge, Type:=wdFieldPage

Set oRge = EndOfLastFooter()
oRge.Text = " of "

oRge.Collapse wdCollapseEnd
oRge.Fields.Add Range:=oRge, Type:=wdFieldNumPages

With ActiveDocument.Sections
Set oRge = .Item(.Count).Footers(wdHeaderFooterPrimary).Range
End With
oRge.Fields.Update

Set oRge = Nothing
End Sub

Private Function EndOfLastFooter() As Range
Dim oRg As Range

With ActiveDocument.Sections
Set oRg = .Item(.Count).Footers(wdHeaderFooterPrimary).Range
End With
oRg.Collapse wdCollapseEnd

Set EndOfLastFooter = oRg
Set oRg = Nothing
End Function


The function handles the quirk of Word that when you insert a field
into a Range and then try to collapse the range to its end, the result
actually winds up *before* the field. So instead, the function sets
the range to the whole footer and collapses that. The function needs
to be called only after inserting a field; a simple collapse suffices
if you've just inserted plain text.

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

MikeL

Thanks Jay,

I will try your suggestion and give your reply some serious thought...

We started automating word in version 97... Our Macro's are written such
that we pass parameters into the macro... We then open a text with word,
format it, and save it... Different formats call different macro's... Word
merge, general formatted documents, find / replaces... We also didn't want to
save the macro with the document... (just the formatting...) Didn't see a
way of opening a text file with a document style / macro at the time...
Maybe an option on the open statement, but didn't see it... Been searchin
along time... (We'll give it another look...)

Thanks again...
MikeL
 
M

MikeL

Thanks again Jay... Have a follow-up question...

I notice that the time updated is dynamic (updated during the print of the
document), this is perfect!!!! Thank You!!!!

Also, the file name is the name is the one where when we import the text
file, it gets that name.... We later save the file name as something
different... Is there a filed that we can use to say the "Current" file
name??? I did my best to look, but only see the filename field... (Hope the
day is a good one...)
 
J

Jay Freedman

Hi Mike,

The filename field is the one-and-only. The problem is that it doesn't
automatically update. After you do a SaveAs to a new file name, you
need to update that field (or all the fields) and save again to make
it show the current document name.

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

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