none
File Name and Path Macro in Footer RRS feed

  • Question

  • I'm trying to make a macro that will (1) insert the following nested fields into the main Footer; (2) update the field; and (3) change the font size to 8:

    { IF { PAGE } = { NUMPAGES } { FILENAME \P \* MERGEFORMAT } }

    I've found a macro that would insert a simple non-nested field into the footer:

    Sub FileNameandPathFooter()

        Dim fld As Field
        With ActiveDocument
            Set fld = .Fields.Add( _
                Range:=.Sections(1).Footers(wdHeaderFooterPrimary).Range, _
                Type:=wdFieldEmpty, _
                Text:="FILENAME \p \* CHARFORMAT", _
                PreserveFormatting:=False)
            fld.Code.Font.Size = 8
            fld.Code.Bold = True
            fld.Update
        End With
    End Sub

    It works great, and it's so close to what I need, but I can't figure out how to modify it to work with nested fields. Every attempt I make breaks the macro in some way. Does anyone have any thoughts?

    Saturday, January 25, 2014 5:35 PM

Answers

  • Try the following:

    Sub InsertConditionalFilenameField()
    Application.ScreenUpdating = False
    'Toggle the field code display 'on'
    ActiveWindow.View.ShowFieldCodes = True
    Dim Rng As Range
    Set Rng = ActiveDocument.Sections.First.Footers(wdHeaderFooterPrimary).Range
    With Rng
      'Set the font attributes
      .Font.Size = 8
      .Font.Bold = True
      .Collapse wdCollapseStart
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
        PreserveFormatting:=False, Text:="IF="
      ' the Page # field goes between the "IF" and "=" characters
      .Start = .Start + 4
      .Collapse wdCollapseStart
      .Fields.Add Range:=Rng, Type:=wdFieldPage, _
        PreserveFormatting:=False
      'Move past the '=' to insert the NumPages field
      .End = .End + 11
      .Collapse wdCollapseEnd
      .Fields.Add Range:=Rng, Type:=wdFieldNumPages, _
        PreserveFormatting:=False
      'Move one space past the NumPages field to insert the FileName field
      .End = .End + 15
      .Collapse wdCollapseEnd
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
        Text:="FILENAME \p", PreserveFormatting:=False
      .Fields.Update
    End With
    'Toggle the field code display 'off'
    ActiveWindow.View.ShowFieldCodes = False
    Application.ScreenUpdating = True
    End Sub

    Note: rather than hard-formatting the font size and overriding whatever Style is present, it would be preferable to define and use a Style with the appropriate attributes.

    For the general case, it would be easier to insert a nested field code as a string (e.g. as in the one you posted), then use the macro I wrote at http://www.gmayor.com/export_field.htm#TextToField as the basis of some code to convert that to a working field.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Sunday, January 26, 2014 10:25 AM Code enhancements
    • Marked as answer by TK_KKRR Tuesday, January 28, 2014 2:22 PM
    Sunday, January 26, 2014 10:21 AM

All replies

  • Try the following:

    Sub InsertConditionalFilenameField()
    Application.ScreenUpdating = False
    'Toggle the field code display 'on'
    ActiveWindow.View.ShowFieldCodes = True
    Dim Rng As Range
    Set Rng = ActiveDocument.Sections.First.Footers(wdHeaderFooterPrimary).Range
    With Rng
      'Set the font attributes
      .Font.Size = 8
      .Font.Bold = True
      .Collapse wdCollapseStart
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
        PreserveFormatting:=False, Text:="IF="
      ' the Page # field goes between the "IF" and "=" characters
      .Start = .Start + 4
      .Collapse wdCollapseStart
      .Fields.Add Range:=Rng, Type:=wdFieldPage, _
        PreserveFormatting:=False
      'Move past the '=' to insert the NumPages field
      .End = .End + 11
      .Collapse wdCollapseEnd
      .Fields.Add Range:=Rng, Type:=wdFieldNumPages, _
        PreserveFormatting:=False
      'Move one space past the NumPages field to insert the FileName field
      .End = .End + 15
      .Collapse wdCollapseEnd
      .Fields.Add Range:=Rng, Type:=wdFieldEmpty, _
        Text:="FILENAME \p", PreserveFormatting:=False
      .Fields.Update
    End With
    'Toggle the field code display 'off'
    ActiveWindow.View.ShowFieldCodes = False
    Application.ScreenUpdating = True
    End Sub

    Note: rather than hard-formatting the font size and overriding whatever Style is present, it would be preferable to define and use a Style with the appropriate attributes.

    For the general case, it would be easier to insert a nested field code as a string (e.g. as in the one you posted), then use the macro I wrote at http://www.gmayor.com/export_field.htm#TextToField as the basis of some code to convert that to a working field.


    Cheers
    Paul Edstein
    [MS MVP - Word]


    • Edited by macropodMVP Sunday, January 26, 2014 10:25 AM Code enhancements
    • Marked as answer by TK_KKRR Tuesday, January 28, 2014 2:22 PM
    Sunday, January 26, 2014 10:21 AM
  • Works perfectly, thank you very much!
    Tuesday, January 28, 2014 2:22 PM