TechDoc01
TechDoc01

Reputation: 3

How can I set the parameter 'PreserveFormatting' of a cross-reference field to true by a Word VBA macro?

For inserted cross-reference fields the parameter 'PreserveFormatting' is by default set to false, but can manually be set to true by selecting 'Preserve formatting during updates' via the menu 'Edit field...' (right mouse-click).

I tried to record a macro, but in the record mode the menu does not open. For me it is unclear, how I can have access to the parameter 'PreserveFormatting' via a VBA macro.

I need a proposal for a Word VBA macro, to add the value true to all fields (type wdFieldRef) of the field collection of a document, so that style changes are not overwritten by the next update.

Regards

Upvotes: 0

Views: 258

Answers (1)

taller
taller

Reputation: 18898

  • Update field code with VBA code
Option Explicit

Sub SetPreserveFormatting()
    Dim oField As field
    Dim fieldCode As String
    Const MF_CODE = "\* MERGEFORMAT"
    ' Loop through all fields
    For Each oField In ActiveDocument.Fields
        ' Check the oField type
        If oField.Type = wdFieldRef Then
            ' Get the current oField code
            fieldCode = oField.Code.Text
            If InStr(fieldCode, MF_CODE) = 0 Then
                ' add the MERGEFORMAT switch
                fieldCode = fieldCode & Chr(32) & MF_CODE
                oField.Code.Text = fieldCode
            End If
        End If
    Next oField
End Sub


Upvotes: 1

Related Questions