Toggle Field Codes in a Macro

K

Karen Clark

A co-worker recorded the following macro which places the
FILENAME in the footer of the last page of her document:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sub NameOnLastPage()
If ActiveWindow.View.SplitSpecial <> wdPaneNone Then
ActiveWindow.Panes(2).Close
End If

If ActiveWindow.ActivePane.View.Type = wdNormalView Or
ActiveWindow. _
ActivePane.View.Type = wdOutlineView Then
ActiveWindow.ActivePane.View.Type = wdPrintView
End If

ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
If Selection.HeaderFooter.IsHeader = True Then
ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageFooter
Else
ActiveWindow.ActivePane.View.SeekView =
wdSeekCurrentPageHeader
End If

Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="IF "
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="PAGE"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.TypeText Text:=" = "
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="NUMPAGES"
Selection.MoveRight Unit:=wdCharacter, Count:=2
Selection.Fields.Add Range:=Selection.Range,
Type:=wdFieldEmpty, _
PreserveFormatting:=False
Selection.TypeText Text:="FILENAME"
Selection.EndKey Unit:=wdLine
ActiveWindow.ActivePane.View.SeekView =
wdSeekMainDocument

End Sub

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The problem is, she couldn't update to toggle the field
code while she was recording, so that the FILENAME would
display (instead of the field codes).

The macro works and the FILENAME prints properly in the
footer of the last page. But she still would like the
FILENAME to display in the footer instead of the field
codes.

Did I explain that OK?

Thanks in advance for your help.

Still WAY too new to know what I'm doing...
Karen
 
W

Word Heretic

G'day "Karen Clark" <[email protected]>,

For a start, use ActiveDocument.StoryRanges(check these out)

Don't use the window - YUCK and PROBLEMS. Recorded macros are often
misleading, and anything in the Headers and Footers especially so.

Check out word.mvps.org/FAQs/index.htm for the VBA tab, and then get
ripping into the story range version.

Then, use ActiveDocument.Fields.Add (check out all the parms, provide
it with the right range to use to insert into)

Steve Hudson - Word Heretic

steve from wordheretic.com (Email replies require payment)
Without prejudice


Karen Clark reckoned:
 
S

Shauna Kelly

Hi Karen

Steve's advice is good. You also need to consider that Word has three
different footers for each section. So you really need to add the fields to
all 3 footers. And, you might want to append the fields to existing text in
the footers. I recently had to do something similar for someone.
Here's some code adjusted for your circumstances.

If you're not sure what to do with it, see
http://www.gmayor.com/installing_macro.htm

You can use Tools > Customize to create a button on a toolbar to invoke the
ShowFileNameOnLastPage macro.


Sub ShowFileNameOnLastPage()

'Shauna Kelly, 18 December 2004
'This version for microsoft.public.word.vba.general
'Insert fields { if { page ) = {numpages} {filename}}
'in the footers for the last section of the active document.

Dim nSectionCount As Long
Dim bFooterHasPageNumber As Boolean
Dim bAppendFooter As Boolean
Dim bOldShowFieldCodes As Boolean

With ActiveDocument

With .Windows(1).View
'Store the user's choice about displaying field codes
bOldShowFieldCodes = .ShowFieldCodes

'Display field codes, or none of this will work
.ShowFieldCodes = True
End With

'Find out which is the last section in the document
nSectionCount = .Sections.Count

'Find out whether the footer already has a page
'number field
bFooterHasPageNumber = _
FooterHasPageNumber(nSection:=nSectionCount, _
eFooter:=wdHeaderFooterEvenPages) _
Or FooterHasPageNumber(nSectionCount, _
eFooter:=wdHeaderFooterFirstPage) _
Or FooterHasPageNumber(nSectionCount, _
eFooter:=wdHeaderFooterPrimary)

'Work out whether to replace the existing footer, or append
'the page number to the existing footer
bAppendFooter = True
If bFooterHasPageNumber Then
If MsgBox("The last section of this document" _
& " already has page numbers." _
& vbCrLf & vbCrLf _
& "Replace existing footers in this section?", _
vbQuestion + vbYesNo) = vbYes Then
bAppendFooter = False
End If
End If

'Insert the file name fields into all 3 footers
'in the last section of the document
InsertFileNameFields _
nSection:=nSectionCount, _
eFooter:=wdHeaderFooterEvenPages, _
bAppendFooter:=bAppendFooter

InsertFileNameFields _
nSection:=nSectionCount, _
eFooter:=wdHeaderFooterFirstPage, _
bAppendFooter:=bAppendFooter

InsertFileNameFields _
nSection:=nSectionCount, _
eFooter:=wdHeaderFooterPrimary, _
bAppendFooter:=bAppendFooter

'Return the display of field codes to the user's preference
.Windows(1).View.ShowFieldCodes = bOldShowFieldCodes

End With


End Sub


Function FooterHasPageNumber(nSection As Long, _
eFooter As WdHeaderFooterIndex) As Boolean
'Return True if this footer contains a page number field

Dim oField As Field

FooterHasPageNumber = False

With ActiveDocument
For Each oField In .Sections(nSection).Footers(eFooter).Range.Fields
If oField.Type = wdFieldPage Then
FooterHasPageNumber = True
End If
Next oField
End With
End Function


Function InsertFileNameFields(nSection As Long, _
eFooter As WdHeaderFooterIndex, bAppendFooter As Boolean) As Boolean

'Inserts fields for { if { page ) = {numpages} {filename}} into the eFooter
'of section nSection.
'If bAppendFooter is true, then append to the existing footer.
'Otherwise, replace the existing footer.
'There is no return value.

Dim oFooterRange As Word.Range

Set oFooterRange = ActiveDocument. _
Sections(nSection).Footers(eFooter).Range
If bAppendFooter Then
If Len(oFooterRange.Text) > 1 Then
'Inset a carriage return to put the page number on a new line
oFooterRange.InsertAfter vbCrLf
End If
Else
'Empty the existing footer
With oFooterRange
.Text = ""
.ParagraphFormat.Reset
End With
End If

'Insert the field codes
With oFooterRange
.Collapse wdCollapseEnd

.Fields.Add Range:=oFooterRange, _
Type:=wdFieldIf, PreserveFormatting:=False
.Move Unit:=wdWord, Count:=2

.Fields.Add Range:=oFooterRange, _
Type:=wdFieldPage, PreserveFormatting:=False
.Move Unit:=wdWord, Count:=3
.Move Unit:=wdCharacter, Count:=-1

.InsertAfter " = "
.Collapse wdCollapseEnd

.Fields.Add Range:=oFooterRange, _
Type:=wdFieldNumPages, PreserveFormatting:=False
.Move Unit:=wdWord, Count:=3
.Move Unit:=wdCharacter, Count:=-1

.Fields.Add Range:=oFooterRange, _
Type:=wdFieldFileName, PreserveFormatting:=False
End With
End Function






Hope this helps.

Shauna Kelly. Microsoft MVP.
http://www.shaunakelly.com/word
 

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