Jasiper
Jasiper

Reputation: 21

How to get Selection.Text to read displaytext of Macro field code

I'm having some trouble figuring this out, and would really appreciate some help. I'm trying to write a macro that uses the selection.text property as a Case text-expression. When the macro is clicked in Microsoft Word, the selected text is automatically set to the DisplayText. This method worked great for the formatting via Selection.Font.Color for a quick and dirty formatting toggling macro, but it doesn't work for the actual text.

When debugging with MsgBox, it is showing a box (Eg: □ ) as the value.

For example,

Word Field Code:

{ MACROBUTTON Macro_name DisplayText }

VBA Code run when highlighting "DisplayText" in Word:

Sub Macro_name()    
    Dim Str As String

    Str = Selection.Text

    MsgBox Str

    Select Case Str            
        Case "DisplayText"
            MsgBox "A was selected"
        Case "B"
            MsgBox "B was selected"
    End Select        
End Sub

What is output is a Message Box that only shows □ When I run this macro with some regular text selected, it works just fine.

My question is this: Is there a way to have the macro read the displaytext part of the field code for use in the macro?

Upvotes: 2

Views: 1229

Answers (1)

Cindy Meister
Cindy Meister

Reputation: 25673

You can read the field code, directly, instead of the selection (or the Field.Result which also doesn't give the text).

It's not quite clear how this macro is to be used throughout the document, so the code sample below provides two variations.

Both check whether the selection contains fields and if so, whether the (first) field is a MacroButton field. The field code is then tested.

In the variation that's commented out (the simpler one) the code then simply checks whether the MacroButton display text is present in the field code. If it is, that text is assigned to the string variable being tested by the Select statement.

If this is insufficient because the display text is "unknown" (more than one MacroButton field, perhaps) then it's necessary to locate the part of the field code that contains the display text. In this case, the function InstrRev locates the end point of the combined field name and macro name, plus the intervening spaces, in the entire field code, searching from the end of the string. After that, the Mid function extracts the display text and assigns it to the string variable tested by the Select statement.

In both variations, if the selection does not contain a MacroButton field then the selected test is assigned to the string variable for the Select statement.

(Note that for my tests I needed to use Case Else in the Select statement. You probably want to change that back to Case "B"...)

Sub Display_Field_DisplayText()
    Dim Str As String, strDisplayText As String
    Dim textLoc As Long
    Dim strFieldText As String, strMacroName As String
    Dim strFieldName As String, strFieldCode As String

    strDisplayText = "text to display"

    If Selection.Fields.Count > 0 Then
        If Selection.Fields(1).Type = wdFieldMacroButton Then
            strFieldName = "MacroButton "
            strMacroName = "Display_Field_DisplayText "
            strFieldCode = strFieldName & strMacroName
            Str = Selection.Fields(1).code.text
            textLoc = InStrRev(Str, strFieldCode)
            strFieldText = Mid(Str, textLoc + Len(strFieldCode))

            MsgBox strFieldText
            Str = strFieldText
            'If InStr(Selection.Fields(1).code.text, strDisplayText) > 0 Then
             '   Str = strDisplayText
            'End If
        End If
    Else
        Str = Selection.text
    End If

    Select Case Str
        Case strDisplayText
            MsgBox "A was selected"
        Case Else
            MsgBox "B was selected"
    End Select
End Sub

Upvotes: 0

Related Questions