Export field constructions

Home Up Search This Site What's New? Audio On CDR Favourites Downloadable files Photo Gallery 2002 Photo Gallery 2003 Photo Gallery 2004/5 Photo Gallery 2006/7 Photo Gallery 2008 Photo Gallery 2009/10 UK Photo Gallery Ireland Photo Gallery Cats Photo Gallery 

 

 

Google
 

 

Many people access the material from this web site daily. Most just leech what they want and run. That's OK, provided they are not selling on the material as their own; however if your productivity gains from the material you have used, a donation from the money you have saved, however small, would help to ensure the continued availability of this resource.

Click the appropriate button above to access PayPal.

Export field constructions as text

I take no credit for this useful macro beyond making it available here.

Fellow Word MVP Cindy Meister lists the code featured on her web site, http://homepage.swissonline.ch/cindymeister/mergfaq.htm) which may be used to reproduce field constructions for use in (e.g) newsgroup messages.

If you have ever tried to document a field construction to a text file, you will know that it is not possible and where complex field constructions are involved (like those of fellow MVP Paul Edstein who uses the pseudonym 'Macropod' in the Word forums, in his remarkable work on date calculations) attempting to reproduce them is a nightmare. This simple macro solution overcomes that.

First step is to open the vba editor and create a new module in the default document template (normal.dot). In the following illustration, I have created the module in normal.dot and renamed it from the default Module1 to FieldCodeToString. The name isn't particularly important.


 

Copy and paste the following macro code into the module you have created, save and close the macro editor.

Sub FieldCodeToString()
Dim Fieldstring As String

Dim NewString As String

Dim CurrChar As String
Dim
CurrSetting As Boolean

Dim fcDisplay As Object
Dim MyData As DataObject

Dim X As Integer
NewString = ""
Set fcDisplay = ActiveWindow.View
Application.ScreenUpdating = False
CurrSetting = fcDisplay.ShowFieldCodes
If CurrSetting <> True Then fcDisplay.ShowFieldCodes = True
Fieldstring = Selection.Text
For X = 1 To Len(Fieldstring)
    CurrChar = Mid(Fieldstring, X, 1)
    Select Case CurrChar
        Case Chr(19)
            CurrChar = "{"
        Case Chr(21)
            CurrChar = "}"
        Case Else
    End Select

    NewString = NewString + CurrChar
Next X
Set MyData = New DataObject
MyData.SetText NewString
MyData.PutInClipboard
fcDisplay.ShowFieldCodes = CurrSetting
End Sub

 

Next step is from the VBA editor, select tools > references and check the Forms 2.0 Object Library - see below - this is required for the macro to work.

 

To use the macro, select the field(s) in the document and run the macro.

 

This copies a facsimile of the field layout to the clipboard, and which can be reproduced as plain text. The result from the above example is reproduced below:

{ IF { Mergefield Name } <> "Fred" "This isn't Fred" "{ Mergefield Name }" }

 

Note: For more information about installing macros use this link