Working With Fonts\Character Code\Symbols in VBA

G

Greg Maxey

I am really not very good at working with fonts, character codes, and
symbols in VBA and need some help.

I have been playing around with a UserForm macro that will replicate
UserForm option button selections in the document. The UserForm
displays 7 frames containing 5 optionbuttons each. The document
displays a seven questions with 5 WingDings2 Symbol 154 (a small
unfilled circle). All 35 symbols are bookmarked with a name
corresponding to the groups and optionbuttons in the UserForm. When
the user makes their selection in the UserForm and the command button
click event in the the UserForm is executed, corresponding the symbols
in the document are changed WindDings2 Symbol 158 (a small circle with
a filled center).

I used the macro recorder to get a start on how to insert the
appropriate symbol at the bookmark using VBA.

The result is:
Selection.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3942, _
Unicode:=True

Normally when I write to a bookmark and need to preserve the bookmark
I use:

Dim oRng as Word.Range
Set oRng = ActiveDocument.Bookmarks("Groupa1").Range
oRng.Text = "My desired text here"
ActiveDocument.Bookmarks.Add "Groupa1", oRng

Using .InsertSymbol as shown above the only way I have been able to
achieve the same result is:

Set oRng = ActiveDocument.Bookmarks("Groupa1").Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3938, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Groupa1", oRng

Note: CharacterNumber:=-3938 inserts the filled circle symbol.

Question: Is there a way I can use "Range.Text = ????" to do this
job? I often see code containing lines like ChrW$(####) that
represents text or symbol. Is there something like that that will
represent the WindDing2 symbols I need?

Thanks.

P.S.

Here is my complete Command_Button code:
Private Sub CommandButton1_Click()
Dim catArray() As String
Dim lngGroup As Long
Dim bSelection As Boolean
Dim i As Long
Dim acontrol As Control
Dim resultsArray(6) As Long 'One less than number of groups
Dim oRng As Word.Range
catArray = Split("a|b|c|d|e|f|g", "|")
'Loop throug each group. Note: controls are indexed starting with 0
Application.ScreenUpdating = False
For lngGroup = 0 To 6
bSelection = False
'Loop through each frame control
For i = 0 To 4
Set acontrol = Controls("Frame" & catArray(lngGroup)).Controls(i)
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup) &
i + 1).Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3942, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i + 1,
oRng
If acontrol.Value = True Then
resultsArray(lngGroup) = i + 1
bSelection = True
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup)
& i + 1).Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3938, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i +
1, oRng
End If
Next i
If Not bSelection Then
MsgBox "No selection was made for Group " & lngGroup + 1
Exit Sub
End If
Next lngGroup
lngGroup = 0
For i = 0 To UBound(resultsArray)
lngGroup = lngGroup + resultsArray(i)
Next i
MsgBox Format(Val(lngGroup) / 7, "#.00")
Unload Me
Application.ScreenUpdating = True
Application.ScreenRefresh
End Sub
 
G

Greg Maxey

I am really not very good at working with fonts, character codes, and
symbols in VBA and need some help.

I have been playing around with a UserForm macro that will replicate
UserForm option button selections in the document. The UserForm
displays 7 frames containing 5 optionbuttons each. The document
displays a seven questions with 5 WingDings2 Symbol 154 (a small
unfilled circle). All 35 symbols are bookmarked with a name
corresponding to the groups and optionbuttons in the UserForm. When
the user makes their selection in the UserForm and the command button
click event in the the UserForm is executed, corresponding the symbols
in the document are changed WindDings2 Symbol 158 (a small circle with
a filled center).

I used the macro recorder to get a start on how to insert the
appropriate symbol at the bookmark using VBA.

The result is:
Selection.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3942, _
Unicode:=True

Normally when I write to a bookmark and need to preserve the bookmark
I use:

Dim oRng as Word.Range
Set oRng = ActiveDocument.Bookmarks("Groupa1").Range
oRng.Text = "My desired text here"
ActiveDocument.Bookmarks.Add "Groupa1", oRng

Using .InsertSymbol as shown above the only way I have been able to
achieve the same result is:

Set oRng = ActiveDocument.Bookmarks("Groupa1").Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3938, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Groupa1", oRng

Note: CharacterNumber:=-3938 inserts the filled circle symbol.

Question: Is there a way I can use "Range.Text = ????" to do this
job? I often see code containing lines like ChrW$(####) that
represents text or symbol. Is there something like that that will
represent the WindDing2 symbols I need?

Thanks.

P.S.

Here is my complete Command_Button code:
Private Sub CommandButton1_Click()
Dim catArray() As String
Dim lngGroup As Long
Dim bSelection As Boolean
Dim i As Long
Dim acontrol As Control
Dim resultsArray(6) As Long 'One less than number of groups
Dim oRng As Word.Range
catArray = Split("a|b|c|d|e|f|g", "|")
'Loop throug each group. Note: controls are indexed starting with 0
Application.ScreenUpdating = False
For lngGroup = 0 To 6
bSelection = False
'Loop through each frame control
For i = 0 To 4
Set acontrol = Controls("Frame" & catArray(lngGroup)).Controls(i)
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup) &
i + 1).Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3942, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i + 1,
oRng
If acontrol.Value = True Then
resultsArray(lngGroup) = i + 1
bSelection = True
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup)
& i + 1).Range
oRng.Delete
oRng.InsertSymbol Font:="Wingdings 2", CharacterNumber:=-3938, _
Unicode:=True
oRng.MoveEnd wdCharacter, 1
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i +
1, oRng
End If
Next i
If Not bSelection Then
MsgBox "No selection was made for Group " & lngGroup + 1
Exit Sub
End If
Next lngGroup
lngGroup = 0
For i = 0 To UBound(resultsArray)
lngGroup = lngGroup + resultsArray(i)
Next i
MsgBox Format(Val(lngGroup) / 7, "#.00")
Unload Me
Application.ScreenUpdating = True
Application.ScreenRefresh
End Sub

I realized that I could create AutoText entries of the two symboles
and use:

ActiveDocument.AttachedTemplate.AutoTextEntries("Symbol1").Insert
Where:=oRng

in place of

oRng.Text = ?????

Still I would like to learn if there is a direct method of putting the
symbol at the range using some sort of character codes.
 
K

Klaus Linke

..Text=ChrW(-3938) should work fine, if you then apply the font.

The negative number is a relic of the fact that Word uses Long integers
(which can store numbers between -32768
and +32767) while Unicode uses numbers between 0 and 65535 (more commonly
written as hex codes, 0 to FFFF).

ChrW(-3938) = ChrW(&HF09E) = ChrW(61598).

Symbols from symbol fonts all use codes in the Unicode block starting at
&HF000 in Word.

You'd get the same character inserting &H9E (Alt+0158) in the symbol font.
If you do so, Word will change the code to &HF09E when you save the
document... until then, you can change the character back and forth between
Symbol fonts and regular fonts, where the character is a z with caron.

Regards,
Klaus
 
G

Greg Maxey

Klaus,

Thanks. I change the For i loop as follows and it seems to work great.


For i = 0 To 4
Set acontrol = Controls("Frame" & catArray(lngGroup)).Controls(i)
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup) & i +
1).Range
oRng.Text = ChrW(-3942)
oRng.Font.Name = "Wingdings 2"
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i + 1, oRng
If acontrol.Value = True Then
resultsArray(lngGroup) = i + 1
bSelection = True
Set oRng = ActiveDocument.Bookmarks("Group" & catArray(lngGroup) & i +
1).Range
oRng.Text = ChrW(-3938)
oRng.Font.Name = "Wingdings 2"
ActiveDocument.Bookmarks.Add "Group" & catArray(lngGroup) & i + 1,
oRng
End If
Next i
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top