Reputation: 3
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
Reputation: 18898
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