none
How to modify existing configuration parameters of Word cross-reference fields using VBA RRS feed

  • Question

  • Hi,

    I need to add the MERGEFORMAT option to cross reference fields (e.g. links to Tables, Figures, Headings) in which this parameter is missing.

    Can someone suggest how to implement this action with a VBA script?

    Thanks!

    Tuesday, December 20, 2016 8:23 AM

Answers

  • To all who may be interested, the following script adds the MERGEFORMAT option to cross reference fields (e.g. links to Tables, Figures, Headings) in which this parameter is missing:

    Dim fieldLoop As Field

    Dim fieldText As String

    For Each fieldLoop In ActiveDocument.Fields

       If InStr(1, fieldLoop.Code.Text, " REF", 1) Then

          fieldText = fieldLoop.Code.Text

          If InStr(1, fieldText, " \h", 1) And Not InStr(1, fieldText, " \* MERGEFORMAT", 1) Then

             fieldText = fieldText & " \* MERGEFORMAT "

             fieldLoop.Code.Text = fieldText

           End If

        End If

    Next

    fieldLoop

    • Edited by arberx Wednesday, December 21, 2016 4:07 PM
    • Marked as answer by arberx Wednesday, December 21, 2016 4:07 PM
    Wednesday, December 21, 2016 4:03 PM

All replies

  • To all who may be interested, the following script adds the MERGEFORMAT option to cross reference fields (e.g. links to Tables, Figures, Headings) in which this parameter is missing:

    Dim fieldLoop As Field

    Dim fieldText As String

    For Each fieldLoop In ActiveDocument.Fields

       If InStr(1, fieldLoop.Code.Text, " REF", 1) Then

          fieldText = fieldLoop.Code.Text

          If InStr(1, fieldText, " \h", 1) And Not InStr(1, fieldText, " \* MERGEFORMAT", 1) Then

             fieldText = fieldText & " \* MERGEFORMAT "

             fieldLoop.Code.Text = fieldText

           End If

        End If

    Next

    fieldLoop

    • Edited by arberx Wednesday, December 21, 2016 4:07 PM
    • Marked as answer by arberx Wednesday, December 21, 2016 4:07 PM
    Wednesday, December 21, 2016 4:03 PM
  • I would question the wisdom of doing that, as the MERGEFORMAT switch regularly causes cross-reference fields to display incorrectly, especially when the range referred to expands after the cross-reference has been created. They are also quite unnecessary (and are often ineffective) when the cross-reference fields are themselves embedded in other fields. A better approach (IMHO) is to remove all MERGEFORMAT switches, which you can do by toggling the field code display on and using Find/Replace.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Thursday, December 22, 2016 12:41 AM
  • Paul, thank you for your comment.

    I need to show links to Sections/Tables/Figures embedded in the document in blue color so that they can be easily identified when the PDF version of the document is browsed.

    I am using Word 2010. Coloring of the link texts in the documents is obtained applying a character style with Blue font color. The character style is applied by a dedicated script and all links become colored in blue.

    However, testing the step to produce the PDF file I have noted that in the last phase of the action to Save the Word file with PDF file type the links that do not include the MERGEFORMAT option change color back from blue to black and are not colored in blue in the saved PDF(!).

    This is the reason why I have managed to automatically include the MERGEFORMAT switch in the field definitions in which it is missing (Note: the links have been added to the documents by many persons who contributed to their editing; this can be the reason why not all the links have the same switches).

    In the tests I made up to now after executing the script to include the MERGEFORMAT switch in the field definitions that I have posted, the blue coloring of the links is preserved both in the Word file and in the PDF file...

    Any additional comment or suggestion from you is definitely wellcome!
    Thank you

    Thursday, December 22, 2016 2:15 PM
  • Depending on your requirements, you might consider using the Charformat switch instead. With that, you'd format the 'R' of 'REF' in the cross-reference field with whatever font attributes you want for the cross-reference as a whole. The only downside (which might be significant in some cases) is that any cross-reference using the Charformat switch wouldn't have mixed font attributes - everything would be in the whatever font attributes were applied to the 'R' of 'REF' in the cross-reference field. Table borders would also be unaffected (i.e. they'd retain their original properties).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by macropodMVP Wednesday, January 4, 2017 10:50 AM
    Thursday, December 22, 2016 11:24 PM
  • Paul,

    I have tested what you suggested and confirm that the addition of the CHARFORMAT switch to the field definitions is able to prevent that the link color is changed back from blue to black when the word document is saved as PDF.

    Thank you for the additional clarification and your kind help!

    Wednesday, January 4, 2017 9:52 AM