Special Symbols/Multi bytes Symbols in Microsoft Word VBA

Special Symbols/Multi bytes Symbols in Microsoft Word VBA

Special Symbol

Special characters built with Unicode/ASCII. In Microsoft Word there are various methods to bring special symbols using Insert options or just copy from some other source and paste in Word. The real challenge is in programming world where you are creating code that would read these symbols from a Word Document and parse them back.

These days, mostly applications are moving on APIs or Cloud technologies and VBA also taken into that by putting Web Services. Transporting the data from one end to another end and vise versa. The use case I have encountered and sharing solution based on that. May be developers can use it by modifying to fit in their needs.

I have used some tricks and some straight forward code to get symbol parsing bidirectionally. See how to identify special symbols in Microsoft Word:

Common ASCII Symbols

These symbols gets represented by code greater than 122 and can be trapped as follows:

Public Function ReadSymbolExample(strInput As String) As String
	Dim i As Integer
    For i = 1 To Len(strCharacteers)
		If Asc(Mid(strCharacteers, i, 1)) > 122 Then
			'Build your string as you have caught a special symbol
		End If
	Next i
End Function

Multi Bytes Symbol

These symbols are having one value SPACE 40 SPACE and cannot be directly determined:

Code example

Public Function ReadSymbol(strInput As String) As String
	Dim i As Integer
    For i = 1 To Len(strCharacteers)
		If Asc(Mid(strCharacteers, i, 1)) = " 40 " Then
			Debug.Print DecodeMultiByteSymbol Asc(Mid(strCharacteers, i, 1))
		End If
	Next i
End Function

Decode multi bytes symbols example

Public Function DecodeMultiByteSymbol(sSymbol As String) As String
    Dim fontName As String
    Dim charNum As String
    Selection.Find.ClearFormatting
    With Selection.Find
      .text = "[" & ChrW(61472) & "-" & ChrW(61695) & "]"
      .Replacement.text = ""
      .Forward = False
      .Wrap = wdFindStop
      .Format = False
      .MatchCase = False
      .MatchWholeWord = False
      .MatchAllWordForms = False
      .MatchSoundsLike = False
      .MatchWildcards = True
    End With
    While Selection.Find.Execute
        With Dialogs(wdDialogInsertSymbol)
            fontName = .Font
            charNum = .charNum
        End With
        GoTo exitpoint
    Wend
exitpoint:
End Function

Encode multi bytes code example

Public Sub EncodeMultiByteSymbols(fontName As String, charNumber As String)
    Selection.InsertSymbol Font:=fontName, CharacterNumber:=charNumber, Unicode:=True
End Sub

Leave a Reply

Your email address will not be published.