Abdallah El-Yaddak
Abdallah El-Yaddak

Reputation: 466

MS Word: Getting the actual ASCII code of an inserted symbol

I am trying to write a VBA code for MS-Word to remove rows with the unticked symbol from a table. To do that, I need MS-Word to recognize it and differentiate between it and the ticked symbol. But unlike Excel, MS-Word seems to be bad at it.

To focus on the problem, I inserted the symbols, but I can not get the correct ASCII code of the character I have just inserted.

Here is what I tried:

Sub SymbolsTest()
    Selection.InsertSymbol 163, "Wingdings 2", True 'Insert unticked
    Selection.MoveRight Unit:=wdCharacter, Count:=-1, Extend:=wdExtend 'Select it
    Debug.Print AscW(Selection.Text) & "  " & Selection.Text 'Ask for ASCII
    Selection.Collapse 0
    Selection.InsertSymbol 82, "Wingdings 2", True 'Insert ticked
    Selection.MoveRight Unit:=wdCharacter, Count:=-1, Extend:=wdExtend 'Select it
    Debug.Print AscW(Selection.Text) & "  " & Selection.Text 'Ask for ASCII
End Sub

The output is:

40  (
40  (

I would expect it to be:

163  ?
82  ?

I also tried ?Selection.Characters(1) = Selection.Characters(2) in the immediate window while selecting both of them, and I got True

Any help would be appreciated.

Symbols to be recognized

Upvotes: 0

Views: 1468

Answers (1)

user12750785
user12750785

Reputation:

As far as I know

  • there is no simple way to get the character's code point or font name directly using any of the properties of the Selection or Range

  • In this situation, Word always uses the code point 40 (")"), and internally, it does does store the name of the font you specified and a Unicode codepoint (e.g. U+F052 for the checked box).

Two things you can do. If the character is not 40, assume it already has the correct codepoint (although I am not certain about that). But if not,

  • retrieve the .XML or .WordOpenXML of the Selection or Range and look for the relevant element, e.g. ><w:sym w:font="Wingdings 2" w:char="F052"/> in both the old-style .XMLand the newer .WordOpenXML. You could either search for the text <w:sym and look for the font and codepoint in the following text, or "do it properly" using an XML parser. In that case, it may be useful to know that F052 either means "the Unicode character with code point F052, or it means "it's F000 + the code point in the original character set", i.e. Wingdings 2 in this case.

e.g. one way would be to make a reference in the VB Editor's Tools-References to the Microsoft XML library (in this case 6.0) and use code along these lines:

Sub getCharFontAndCodepoint()
Dim xdoc As MSXML2.DOMDocument60
Dim xSymNodes As MSXML2.IXMLDOMNodeList
Set xdoc = New MSXML2.DOMDocument60
xdoc.async = False
If xdoc.LoadXML(Selection.XML) Then
  xdoc.SetProperty _
    "SelectionNamespaces", _
    "xmlns:w='http://schemas.microsoft.com/office/word/2003/wordml'"
  Set xSymNodes = xdoc.SelectNodes("//w:sym/@w:font")
  If xSymNodes.Length > 0 Then
    Debug.Print xSymNodes(0).NodeValue
  End If
  Set xSymNodes = xdoc.SelectNodes("//w:sym/@w:char")
  If xSymNodes.Length > 0 Then
    Debug.Print xSymNodes(0).NodeValue
  End If
End If
Set xSymNodes = Nothing
Set xdoc = Nothing
End Sub
  • or, if you only need the codepoint, copy the character and use paste special to paste it using the Unformatted Unicode Text format, e.g.

     Selection.Copy
     Selection.PasteSpecial link:=False, DataType:=22 ' There does not seem to be a named enum for this particular format
     Selection.MoveLeft Unit:=WdUnits.wdCharacter, Count:=1, Extend:=WdMovementType.wdExtend
     Debug.Print Hex(AscW(Selection))
     Selection.Document.Undo
    

Upvotes: 2

Related Questions