Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / December 2004

Tip: Looking for answers? Try searching our database.

Toggle Field Codes in a Macro

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Karen Clark - 16 Dec 2004 15:55 GMT
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
Word Heretic - 17 Dec 2004 22:15 GMT
G'day "Karen Clark" <anonymous@discussions.microsoft.com>,

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:

>A co-worker recorded the following macro which places the
>FILENAME in the footer of the last page of her document:
[quoted text clipped - 64 lines]
>Still WAY too new to know what I'm doing...
>Karen
Shauna Kelly - 17 Dec 2004 23:58 GMT
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

> G'day "Karen Clark" <anonymous@discussions.microsoft.com>,
>
[quoted text clipped - 84 lines]
>>Still WAY too new to know what I'm doing...
>>Karen
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.