Thursday, September 8, 2011

Insert Field formatting switch




The InsertField dialog (the illustrations are from Word 2003 (top) and 2007, other Word versions are similar) has the Preserve formatting during updates check box checked by default, with no obvious way of resetting that default. This adds a MERGEFORMAT switch to the inserted field. Frankly I have never found any real use for the switch and so I always uncheck it .... when of course I remember, so the first macro I created simply intercepts the InsertFile command and uses the SendKeys command to physically uncheck the box i.e.



Sub InsertField()
SendKeys "{Tab 2} +{Tab 2}"
Dialogs(wdDialogInsertField).Show
End Sub



This worked fine, until fellow MVP Paul Edstein, who uses the pseudonym Macropod in the forums, baited me to produce a version which gave the user the opportunity to add a CHARFORMAT switch as an alternative to the MERGEFORMAT switch. The result was the following.
Inserting a field from the Insert > Field menu option (Insert > Quick Parts > Field in Word 2007/2010) opens the dialog with the check box unchecked, using the same method as above, but if you check the box, you are presented with a message box which offers the opportunity to choose the type of formatting switch, then adds the appropriate switch to the field.
Check the box and you will see the further dialog




The result is that the field may be inserted with either switch as appropriate e.g.

{ CREATEDATE \@ "dddd, dd MMMM yyyy" \* CHARFORMAT }

by selecting YES

{ CREATEDATE \@ "dddd, dd MMMM yyyy" \* MERGEFORMAT }
by selecting No
or none if the Insert Field Dialog box is left unchecked.
{ CREATEDATE \@ "dddd, dd MMMM yyyy" }



Sub InsertField()
Dim oRng As Range
Dim i As Variant
Dim sSwitch As String
Dim strChoice As String
SendKeys "{Tab 2} +{Tab 2}"
Dialogs(wdDialogInsertField).Show
On Error Goto Finish 'User has cancelled
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Set oRng = Selection.Range
For i = 1 To oRng.Fields.Count
    With oRng.Fields(i)
        If InStr(1, .Code, "MERGEFORMAT") <> 0 Then
             sSwitch = MsgBox("Use charformat in place of the mergeformat switch?", _
             vbYesNo, _
             "Insert Field")
             If sSwitch = vbYes Then
                  .Code.Text = Replace(.Code.Text, _
                  "MERGEFORMAT", _
                  "CHARFORMAT")
             End If
        End If

    .Update
    End With
Next
i
Selection.MoveRight Unit:=wdCharacter, Count:=1
Finish:
End Sub

Note:

I am informed, by the aforementioned Paul Edstein, that the SendKeys approach will not work under the Windows Vista operating system, returning error code 70: "Permission denied" - so that's another reason why I will be keeping Windows XP for the foreseeable future.

This then negates the use of the above macro for Vista users, but I have included a modified version below with the SendKeys line removed and a separate step to remove the switch. It is not as elegant as un-checking the check box, but it does the job.

No comments:

Post a Comment