PDA

View Full Version : Identifying symbol with VBA



gmaxey
01-10-2012, 06:13 PM
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?

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


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

Thanks.

macropod
01-10-2012, 08:06 PM
Hi Greg,

try:
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

Paul_Hossler
01-10-2012, 08:44 PM
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



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


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

Paul

gmaxey
01-11-2012, 08:43 AM
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.

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


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

Paul_Hossler
01-11-2012, 09:47 AM
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

gmaxey
01-11-2012, 03:49 PM
Paul the idea is to show or hide the content (or whatever else you want the checked/unchecked action to be).

Paul_Hossler
01-11-2012, 04:37 PM
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

macropod
01-11-2012, 10:20 PM
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.

gmaxey
01-11-2012, 10:29 PM
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.