Results 1 to 11 of 11
  1. #1
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Convert field codes to text for posting

    Did someone already post a tool to convert field codes to text? It is awfully tiresome not having a tool for that so I wrote one just now. Testers welcome.
    Code:
    Option Explicit
    
    Sub DocumentSelectedFieldCode()
    ' To aid in documenting field codes for the Lounge
    ' Turn a field code into text, even if nested
    If Selection.Fields.Count = 0 Then
        MsgBox "Select the field and try again."
        Exit Sub
    End If
    ' Call with bold Lounge markup on the braces
    ConvertFieldCodeToText Selection.Fields(1), True
    MsgBox "Done!"
    End Sub
     
    Sub ConvertFieldCodeToText(fld As Field, Optional blnLoungeBold As Boolean = False)
    ' Recursive field code converter; writes to ActiveDocument
    Dim intStart As Integer, strCode As String
    ' Keep digging until the field contains no more fields
    While fld.Code.Fields.Count > 0
        ' This field code contains yet other field codes, so
        '  call myself to decode the last of them
        ConvertFieldCodeToText fld.Code.Fields(fld.Code.Fields.Count), blnLoungeBold
    Wend
    ' Now we have no internal fields, capture the position and
    '  field code contents, delete the field, and insert the plain text
    intStart = fld.Code.Start - 1
    If blnLoungeBold Then
        strCode = "[b]{[/b]" & fld.Code.Text & "[b]}[/b]"
    Else
        strCode = "{" & fld.Code.Text & "}"
    End If
    fld.Delete
    ActiveDocument.Range(Start:=intStart, End:=intStart).InsertAfter strCode
    ' Clean up objects and return to calling routine
    If Not fld Is Nothing Then Set fld = Nothing
    End Sub
    A sample document containing this macro is attached. The field codes are from this thread, but it should work across a wide variety of codes of any (ha ha) complexity. If it fails, please post with comments.
    Attached Files Attached Files
    Last edited by jscher2000; 2011-07-28 at 00:12. Reason: Fixed [code] tags.

  2. #2
    Uranium Lounger
    Join Date
    Dec 2000
    Location
    Los Angeles Area, California, USA
    Posts
    7,453
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Convert field codes to text for posting

    Hi Jefferson:
    I haven't had a chance to test yours yet, but this is one that Cindy Meister wrote a few years ago. I'm not sure of the differences, but it's always worked for me.
    Code:
    Sub CopyFieldCodes()
    '
    ' CopyFieldCodes Macro
    ' Macro created 11/24/99 by Cindy Meister
    '
        Dim Fieldstring As String, NewString As String
        Dim CurrSetting As Boolean, fcDisplay As Object
        Dim MyData As DataObject
        Dim x As Integer
        Dim CurrChar As String
        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
    Last edited by jscher2000; 2011-07-28 at 00:07. Reason: Fixed [code] tags.

  3. #3
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Convert field codes to text for posting

    I started going down that road, and agree that it is the most straightforward. I feared that using the character codes to identify the field braces might not be reliable in all cases, so I went the other way. Output should be the same, except that hers goes on the clipboard and mine replaces the selected original.
    Last edited by jscher2000; 2011-07-28 at 00:16. Reason: Fixed smiley.

  4. #4
    2 Star Lounger
    Join Date
    Feb 2001
    Location
    Wellington, New Zealand
    Posts
    143
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Convert field codes to text for posting

    Something I noticed is that if a hyperlink is converted the visible text is lost in the process. Was it meant to be included somewhere?

  5. #5
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Convert field codes to text for posting

    You're right, it was not intended to document the "results" of field codes, only the internal code itself.

    The macro could be revised to preserve the hyperlink text, but I wonder how it should be displayed? One method would be as a bracketed notation, e.g.,

    { HYPERLINK "http://www.wopr.com" [display text: the WOPR home page] }

    or

    { HYPERLINK "http://www.wopr.com" }<the WOPR home page>

    What do you think would make the most sense?

  6. #6
    Gold Lounger
    Join Date
    Feb 2003
    Location
    Wardrobe Malfunction Junction, Derry
    Posts
    2,953
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Convert field codes to text for posting

    Jefferson--

    Forgive the basic question but what's a field code--is this VBA programming-- i.e where do you use this "field code to text tool"? Is it something that I should have learned by now--or something I'll be glad to have learned--is it a tool made with VBA? Or is it something that I'd have to know VBA or VB to use? I'm sorry to have to ask but I have to start somewhere with this, and I didn't want to ignore that I didn't know what it was.

    Is it related at all to the tool/tip Diane Chapman has made here? I'm just trying to context this a little and can you put what the tool does into some language so I can have a springboard to read up or know at least the area?

    Thanks,

    SMBP

  7. #7
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Convert field codes to text for posting

    Field codes are the formulas that Word uses to display a calculation. You can think of them in the same way as a formula in Excel. Any time a field is used in Word (see Insert > Field) the displayed text is the result of calculating the 'Field codes'. Fields are routinely used in Word for items such as TOCs, Cross-references, page numbers and dates. If you select any one of these items and right click it and choose "Toggle Field Codes" then you will see the formula that is used to display the text you saw a moment ago.

    Now, when you understand how a field is just a formula, it becomes relatively easy to edit the field codes to tailor fields to do much more than what is available out of the box. An example is this current post which asks how to create automatic numbers in reverse order. Microsoft never envisaged anyone would want to do this and it can't be done without editing field codes.

    The reason that Jefferson wanted to create the macro is that when you want to copy and paste a field to something other than Word (eg this forum) - you only get the results of the field, not the field codes themselves. This is a real pain when the contents of a field have been heavily customised and you need to show the workings of the formula, not the result. This is especially annoying when you need to show the contents of a nested field which uses the results of one (or more) fields in the calculations for another field.

    Field codes are not VBA programming although they can be created and modified by using VBA. The link you posted talks about field codes but doesn't have anything particularly relevant to Jefferson's macro. That article by Dian Chapman is using WordBasic which has been long superceded by VBA. The code on that page performs a search and replace function on field codes but you can do that without any programming as long as you display the field codes in Word before doing the search and replace.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  8. #8
    Gold Lounger
    Join Date
    Feb 2003
    Location
    Wardrobe Malfunction Junction, Derry
    Posts
    2,953
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Convert field codes to text for posting

    Thanks very much for the good explanation. You give me reference as a springboard to read.

    So rather than any "type of programming language" when Jefferson and Phil wrote in the small type font, that is the displayed text and formula that becomes the tool?

    SMBP

  9. #9
    Super Moderator
    Join Date
    Jan 2001
    Location
    Melbourne, Victoria, Australia
    Posts
    3,852
    Thanks
    4
    Thanked 259 Times in 239 Posts

    Re: Convert field codes to text for posting

    The first two posts in this thread contained VBA code which could be used to translate selected fields in a document into 'text which looks like a field but isn't'. That text can then be used by pasting to lounge as per the 5th or 6th post in this thread. In that post Jefferson has used { } braces to indicate the field code braces which look like this but are very different.

    In Word, if you create a field and toggle the field codes you will see the field is enclosed in a pair of braces eg {TOC o "1-3"} but you cannot type this and expect that it is now a field. To create a field by typing you need to press Ctrl-F9 and then type inside the special braces that were inserted.
    Andrew Lockton, Chrysalis Design, Melbourne Australia

  10. #10
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts

    Re: Convert field codes to text for posting

    If you use the Insert > Field dialog and the online help, you will see that fields can be used for a wide variety of things, from page numbers (those are field codes) to calculations, hyperlinks, embedded files... the list goes on. On the board in just the past week we discussed the ADVANCE field, the EQ field, the IF field, the CREATEDATE and SAVEDATE fields. And probably others I don't remember. They are everywhere.

  11. #11
    Gold Lounger
    Join Date
    Feb 2003
    Location
    Wardrobe Malfunction Junction, Derry
    Posts
    2,953
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Convert field codes to text for posting

    Andrew and Jefferson--

    I appreciate this. I will put it to use. It gives me some context versus a complete enigma. It's a shame I didn't learn some of this years ago, but I'm going to work harder (much) on Word and Excel hopefully. Getting out a passable document is one thing, but learning many of the tricks can take a while. Then getting to this. The term "toc" you see all the time in MSDN and Technet articles over on the taskpane that has links--where does that originate-- and--you see all kinds of web articles on making a toc for this and a toc for that. I know there are different kinds of tocs. Obviously it stands for Table of Contents not Total Organic Carbon analyzer but it h as a functional and developer's meaning--beyond just a table of contents.

    How To Use the MSDN Table of Contents

    I'm not trying to stay off topic but you see the term come up and it must be a developer's term for a lot of different types. I saw it used in the context of xml comparing it to the "msdn toc."

    Thanks for the help,

    SMBP

Posting Permissions

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