• Convert Field Codes (not results) to Text

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Convert Field Codes (not results) to Text

    Author
    Topic
    #478994

    Hi, All —

    I am not a VB programmer. I have a Word document with hundreds of field codes. I would like to convert these codes (not the results) to text. The VB script posted here will work for the fields one at a time. I have been unable to get it to work for all codes in the document (i.e., I need to individually select each code and run the macro). Any help on getting this script to find and convert all field codes in the document?

    Thanks a bunch.

    Matt

    Viewing 4 reply threads
    Author
    Replies
    • #1298227

      Here is an inelegant macro that will pick up all field codes and copy them to another document. It doesn’t pick up the brackets on either end of the field code. You could probably do something to add them. Is this what you wanted, or did you want to have the code show in situ?

      Put this macro wherever you keep macros. Make sure the document you want to grab field codes from is the active document. Run the macro.

      Sub grabFieldCode()
      Dim MyString As String, FieldShowSetting As Boolean
      For Each aField In ActiveDocument.Fields
      aField.Select
      MyString = MyString & vbCr & Selection.Fields(1).Code.Text
      Next aField
      Documents.Add
      ActiveDocument.Content.InsertAfter MyString
      End Sub

      • #1298230

        Thanks for the reply. Actually, I would like to convert the field codes in-situ.

        I am using reference management software (RefWorks) to track citations in a document. I originally used an add-in for this (Write-N-Cite). However, this doesn’t work with 64-bit Word. Thus I have to use the RefWorks web service to create my bibliography, but it doesn’t recognize field codes themselves — it only recognizes the text in the field codes. So, each of my references is a field code that looks something like this:

        {ADDIN RW.CITE{{24340 Radjai,Farhang 1998}}}

        If I can convert that whole entity to plain text (which is what the macro I referenced does), I can then do two global search and replace (with nothing) operations to get:

        {{24340 Radjai,Farhang 1998}}

        which is what the RefWorks web service expects to see. Sorry for the long-winded explanation — and thanks again for the help.

        • #1298233

          Ok, try this.

          It probably won’t work for nested fields, and my ride is about to leave so I can’t do anything more with it. It also leaves field codes hidden. Turn ’em back on if you need to, or ditch the next-to-last line of the macro.
          Sub plopFieldCode()
          Dim MyString As String
          ActiveWindow.View.ShowFieldCodes = True
          For Each aField In ActiveDocument.Fields
          aField.Select
          MyString = “{ ” & Selection.Fields(1).Code.Text & ” }”
          Selection.Text = MyString
          Next aField
          ActiveWindow.View.ShowFieldCodes = False
          End Sub

    • #1298237

      Thanks again — I get a compile error ‘variable not defined’ for ‘aField’. When I try to add it to the ‘Dim’ line, it still doesn’t work. Thoughts?

    • #1298242

      Make sure you put Dim aField as Field.

      If that doesn’t work I will get back to you in the morning.

    • #1298243

      I don’t know if it would do what you want, but how about printing to pdf with the option of printing field codes rather than results. Send the pdf to your consultants with the original document. Remember to switch this option back when you are done because it will act on all documents, not just this one.

      Not what you asked for but it may help. Converting all the field codes destroys them as Word fields anyway and messes up the layout of your document (probably). They should be able to get the information they need from the pdf.

      Otherwise, here is a related post from macropod with code that puts the field code text into the Clipboard. I expect you could cycle through fields and apply it pasting to the document replacing the fields.

      http://windowssecrets.com/forums/showthread//139777-Field-codes-to-text-or-string-to-allow-posting-repost-from-macropod

    • #1298244

      That did it! Thanks a bunch — for some reason, when I added “, aField as Field” to the existing “Dim” line earlier, it didn’t work (or, I did something else wrong). When I added “Dim aField as Field” as its own line, it worked like a charm. I really appreciate the help.

    Viewing 4 reply threads
    Reply To: Convert Field Codes (not results) to Text

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: