Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Convert Field Codes (not results) to Text

    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

  2. #2
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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

  3. The Following User Says Thank You to jweissmn1 For This Useful Post:

    tmevans (2011-09-14)

  4. #3
    New Lounger
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

  5. #4
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    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
    Last edited by jweissmn1; 2011-09-14 at 16:16. Reason: extra variable left over

  6. #5
    New Lounger
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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?

  7. #6
    Star Lounger
    Join Date
    Feb 2011
    Location
    Washington, DC
    Posts
    98
    Thanks
    8
    Thanked 6 Times in 6 Posts
    Make sure you put Dim aField as Field.

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

  8. The Following User Says Thank You to jweissmn1 For This Useful Post:

    tmevans (2011-09-14)

  9. #7
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,049
    Thanks
    124
    Thanked 119 Times in 116 Posts
    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/sho...-from-macropod
    Last edited by Charles Kenyon; 2011-09-14 at 17:09. Reason: add warning about option
    Charles Kyle Kenyon
    Madison, Wisconsin

  10. #8
    New Lounger
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •