Consulting

Results 1 to 9 of 9

Thread: Identifying symbol with VBA

  1. #1
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location

    Identifying symbol with VBA

    This one has me stumped. How can I selected a symbol (or better yet) asign a range to a symbol and determine what symbol it is?

    [VBA]Sub ScratchMacro()
    With ActiveDocument
    .Range.Delete
    .Range.InsertAfter vbCr & vbCr
    .Paragraphs(1).Range.InsertSymbol 168, "Wingdings", True
    .Paragraphs(2).Range.InsertSymbol 254, "Wingdings", True
    MsgBox Hex(AscW(.Paragraphs(1).Range.Characters(1)))
    MsgBox Hex(AscW(.Paragraphs(2).Range.Characters(1)))
    End With
    End Sub
    [/VBA]

    The code should illustrate the question. If I have a checked box and an unchecked box, how can I identify them with VBA?

    Thanks.
    Greg

    Visit my website: http://gregmaxey.com

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Hi Greg,

    try:
    [VBA]Sub Demo()
    Dim StrFont, StrCharNum
    With ActiveDocument
    .Range.Delete
    .Range.InsertAfter vbCr & vbCr
    .Paragraphs.First.Range.InsertSymbol 168, "Wingdings", True
    .Paragraphs.Last.Range.InsertSymbol 254, "Wingdings", True
    .Paragraphs.First.Range.Characters.First.Select
    With Dialogs(wdDialogInsertSymbol)
    StrFont = .Font
    StrCharNum = .CharNum
    End With
    MsgBox StrFont & vbTab & 4096 + StrCharNum
    .Paragraphs.Last.Range.Characters.First.Select
    With Dialogs(wdDialogInsertSymbol)
    StrFont = .Font
    StrCharNum = .CharNum
    End With
    MsgBox StrFont & vbTab & 4096 + StrCharNum
    End With
    End Sub[/VBA]
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    I've never liked InsertSymbol for VBA. It seems to have a mind of it's own.

    You could try to handle your own symbols with font changes, etc.

    I'm still strugging with Selection when to use it, and when not, so I'm sure you could polish this, but it does seem to be able to tell the difference between Check and Box


    [vba]
    Sub ScratchMacro()
    Dim sFont As String

    With ActiveDocument
    .Range.Delete
    .Range.InsertAfter vbCr & vbCr

    sFont = Selection.Font.Name

    Selection.Font.Name = "Wingdings"
    Selection.TypeText Text:=ChrW(254 - 4096)
    Selection.TypeParagraph
    Selection.TypeText Text:=ChrW(168 - 4096)
    Selection.TypeParagraph
    Selection.Font.Name = sFont


    MsgBox AscW(.Paragraphs(1).Range.Characters(1))
    MsgBox AscW(.Paragraphs(2).Range.Characters(1))


    End With
    End Sub
    [/vba]

    If you were REALLY ambitious, you could write your own InsertMaxeySymbol to take a code and a font and insert it

    Paul
    Last edited by Paul_Hossler; 01-10-2012 at 08:56 PM.

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Paul x2,

    Thanks for your inputs. The objective of this exercise is to create an interactive document checkbox in Word2007/2003 that doesn't rely on a template buildingblock\AutoText entry.

    In a Word 2007 document insert a Macrobutton field:

    { MacroButton TestCB B } where "B" is a Wingdings 168 unchecked box.

    add a plain text CC titled Test CC and insert some text.

    Add this code to a standard vba module.

    [VBA]Option Explicit
    'Toggles Check Box and Show/Hide CC Text
    Sub Checkit(strCCTitle As String)
    Dim oFld As Word.Field
    Dim oRngTarget As Word.Range
    Dim i As Long
    Dim oRngSymbol As Word.Range
    Dim StrFont As String
    Dim strCharNum As Long
    Dim oCC As ContentControl
    Application.ScreenUpdating = False
    Set oFld = Selection.Fields(1)
    i = oFld.Code.Characters.Count
    'Defing target (or where symbol goes)
    Set oRngTarget = oFld.Code.Characters(i)
    'Does a space or spaces follow the symbol in the field code?
    Do While AscW(oRngTarget.Text) = 32
    'Move target until clear of spaces
    i = i - 1
    Set oRngTarget = oFld.Code.Characters(i)
    Loop
    'Show field codes
    oFld.ShowCodes = True
    'The selection now spans the field code.
    'Accounting for the the field brace, define range of symbol and select it
    Set oRngSymbol = Selection.Characters(i + 1)
    oRngSymbol.Select
    'What symbol is it?
    With Dialogs(wdDialogInsertSymbol)
    StrFont = .Font
    strCharNum = 4096 + .CharNum
    End With
    'Show field result
    oFld.ShowCodes = False
    Set oCC = ActiveDocument.SelectContentControlsByTitle(strCCTitle).Item(1)
    If strCharNum = 168 Then
    oRngTarget.InsertSymbol 254, "WingDings"
    oCC.Range.Font.Hidden = False
    Else
    oRngTarget.InsertSymbol 168, "WingDings"
    oCC.Range.Font.Hidden = True
    End If
    Application.ScreenUpdating = False
    Set oRngTarget = Nothing
    Set oRngSymbol = Nothing
    Set oCC = Nothing
    Set oFld = Nothing
    End Sub
    Sub TestCB()
    Checkit "Test CC"
    End Sub
    [/VBA]

    If either of you see a better way or a way to stay out of the physical field code then please advise.
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    I might not be understanding, but when I click the 254 or 168 Wingdings char in the MACROBUTTON Field, my CC updates

    (I did change the CC action to a text update just 'cause it was easier for me to see, instead of hidden text dots)

    Paul
    Attached Files Attached Files

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Paul the idea is to show or hide the content (or whatever else you want the checked/unchecked action to be).
    Greg

    Visit my website: http://gregmaxey.com

  7. #7
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Understand

    I wanted the macrobutton action to change the CC text that was being displayed to change between "254" and "168"

    When I check / uncheck the macrobutton, the char in the macrobutton changes and the CC text changes.

    I could have used .Hidden like you did.

    Paul

  8. #8
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Greg, IMHO trying to hide things by changing their font attribute is next-to-useless, since the end-user can just as easily display and print the hidden content by setting Word's options to do so. Indeed, they may already have Word configured that way and, disconcertingly, will see no change when they toggle the checkbox state.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  9. #9
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location
    Paul,
    I aggree. The pratical value of the exercise will need refinement that was just the proof of concept. One application I thought of was next to a list of terms in a contract. Check the box and all instances of the term or phrase is found and highlighed. Check again and the highlighting disappear. Just thinking out loud. Thanks.
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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