none
How can I distinguish special characters or signs in Doc through VBA? RRS feed

  • Question

  • How can I distinguish special characters or signs in Doc through VBA?

    I have a few large documents written in word 2003 format and now I work on them under word 2007.
    Such large documents containing lots of special characters.  I want to change them into Microsoft 3.0 equation form(It is required by the team). I find no way but using function AddOLEObject and  Sendkeys through VBA. Common characters can be added to equation by function Sendkeys, but the return value of Asc() or AscW() for "double arrow", "in" , " not greater than " such special characters or signs, to name but a few are the same ,i.e., 40 And I cannot distinguish them.

    I found the link http://msdn.itags.org/word/44333/ may be of use to me, and the title is:
    How to read symbols from a DOC documents! (Microsoft Word)
     the original question is
    Using function "InsertSymbol", I can insert a symbol into a document. But
    how can I read a symbol from a document?
    Using "Characters(i).range.text", I can read a char. But when meeting a
    symbol, the value of "Characters(i).range.text" is always '('.How can I get the CharacterNumber of the symbol.


    And the final answer is :
    Hi chenfeng,

    Word protects symbols from symbol fonts if you insert them from the "Insert
    > Symbol" dialog.

    This is done so they aren't changed when you change the font or style. But
    it also results in Word reporting AscW( ) = 40 on all of them.

    I've posted a macro to "unprotect" them (or to protect them again if you
    want) ... just today again in

    Newsgroups: microsoft.public.word.vba.customization
    Subject: Symbol Characters
    Date: Mon, 3 May 2004 11:00:46 -0700
    Message-ID: <09f401c43138$8f92f900$7d02280a@msdn.itags.org.phx.gbl>

    It isn't archived on Google yet, but tomorrow you should be able to simply
    copy the message ID into http://www.google.com/advanced_group_search.

    Regards,
    Klaus


    But the link for the macro in the final answer is missing.

    Can anyone with kindness help me? Thaks a lot.

    Sunday, May 13, 2012 4:32 AM

All replies

  • Hi "s"

    <<I have a few large documents written in word 2003 format and now I work on them under word 2007.
    Such large documents containing lots of special characters.  I want to change them into Microsoft 3.0 equation form(It is required by the team). I find no way but using function AddOLEObject and  Sendkeys through VBA. Common characters can be added to equation by function Sendkeys, but the return value of Asc() or AscW() for "double arrow", "in" , " not greater than " such special characters or signs, to name but a few are the same ,i.e., 40 And I cannot distinguish them.>>

    The problem in this case is that you have to use the old Equation Editor. This is proprietary software that was licensed by Microsoft (from MathType, I believe). It provides no API you can use to create or "read" equations. Possibly, MathType provides a full API you could purchase/license that would give you this capability - you'd have to inquire there.

    In order to get away from this "black box" way of providing support for complex equations, Microsoft introduced "MathML" - in the object model look for OMath. You can use either the APIs or manipulate the Open XML file to create, edit and read equations in Office 2007 (and 2010) documents.

    If the team responsible for this project wants to automate the work with equations, it needs to consider converting these old equation objects into the new Equations format. Or you have to check with MathType about licensing a product that can access their proprietary APIs in the old tool.


    Cindy Meister, VSTO/Word MVP

    Sunday, May 13, 2012 6:44 AM
    Moderator
  • Hi "s"

    <<I have a few large documents written in word 2003 format and now I work on them under word 2007.
    Such large documents containing lots of special characters.  I want to change them into Microsoft 3.0 equation form(It is required by the team). I find no way but using function AddOLEObject and  Sendkeys through VBA. Common characters can be added to equation by function Sendkeys, but the return value of Asc() or AscW() for "double arrow", "in" , " not greater than " such special characters or signs, to name but a few are the same ,i.e., 40 And I cannot distinguish them.>>

    The problem in this case is that you have to use the old Equation Editor. This is proprietary software that was licensed by Microsoft (from MathType, I believe). It provides no API you can use to create or "read" equations. Possibly, MathType provides a full API you could purchase/license that would give you this capability - you'd have to inquire there.

    In order to get away from this "black box" way of providing support for complex equations, Microsoft introduced "MathML" - in the object model look for OMath. You can use either the APIs or manipulate the Open XML file to create, edit and read equations in Office 2007 (and 2010) documents.

    If the team responsible for this project wants to automate the work with equations, it needs to consider converting these old equation objects into the new Equations format. Or you have to check with MathType about licensing a product that can access their proprietary APIs in the old tool.


    Cindy Meister, VSTO/Word MVP

    Maybe I did not explain myself clearly.
    I wrote a sub to change characters to Microsoft equation3.x. And it works well for common characters .Such as "a=3+56","sinx=y*9",etc. It depends on the Asc() value from the Text form of the characters.But when the text expression involves Special characters in the "Insert
    > Symbol" dialog, such as "double arrow", "in"," not greater than",to name but a few. The return value of Asc() or AscW() is always 40 and so  I cannot distinguish them and my sub cannot be sure which is the incoming character. I need the help to solve the problem.
    In a word, how can I distinguish the different symbols in the "Insert
    > Symbol" dialog through VBA? Any functions or methods?


    Sunday, May 13, 2012 7:05 AM
  • Maybe I did not explain myself clearly.
    I wrote a sub to change characters to Microsoft equation3.x. And it works well for common characters .Such as "a=3+56","sinx=y*9",etc. It depends on the Asc() value from the Text form of the characters.But when the text expression involves Special characters in the "Insert
    > Symbol" dialog, such as "double arrow", "in"," not greater than",to name but a few. The return value of Asc() or AscW() is always 40 and so  I cannot distinguish them and my sub cannot be sure which is the incoming character. I need the help to solve the problem.
    In a word, how can I distinguish the different symbols in the "Insert
    > Symbol" dialog through VBA? Any functions or methods?
    Sunday, May 13, 2012 7:06 AM
  • s_xczp,

    I'm unable to reproduce your problem. AscW() works for me in Word 2003 with the following characters:

    ≥ (which is 8005)

    ≤ (8804)

    » (187)

    Here's a Word VBA sample, tested for 2003 and 2007:

    Dim rngTemp As Range Selection.Collapse Direction:=wdCollapseStart Set rngTemp = ActiveDocument.Range(Start:=Selection.Start, _ End:=Selection.Start + 1) Dim stringy As String stringy = rngTemp.Text

    Dim inty As Integer inty = AscW(stringy)

    MsgBox (inty)

    Run when you place the caret in front of a strange character, and a message box pops up with its numeric value. Obviously you can use selection statements in your code to perform the actions you want.

    Here's VB.NET code.




    • Edited by JosephFox Thursday, May 24, 2012 7:41 PM
    Sunday, May 13, 2012 4:17 PM
  • I am using Word 2007. The document is written in word 2003. And I use VBA method  Range.Fields.add to create equations with  through Eq field from an existing range of the active document. the Sub is:

    Sub myexampleforeq()

    Dim MyRange As Range

    Set MyRange = ActiveDocument.Range(0, 6)
    MyRange.Fields.Add Range:=MyRange, Type:=wdFieldEmpty, Text:=MyRange.Text, PreserveFormatting:=False

    End Sub

    the  ActiveDocument.Range(0, 6) is EQ ASD .And the Sub works well.

    But when ActiveDocument.Range(0, 6) contains Special characters in the "Insert Symbol" dialog, such as "double arrow", "in"," not greater than",to name but a few. The Sub cannot show the wanted Special characters properly.

    How can I perform this through VBA?Can anyone with kindness help me?

    Wednesday, May 23, 2012 3:46 AM
  • Did you try my code? I said 'VBA 2003', but I have now tested it with Word 2007 VBA. After inserting the 'greater than or equal to' symbol, selecting it, and running a macro with that code, it pops up with '8805', which is its numeric representation.

    I believe you can go through the text property of the range, and use the AscW to determine the numeric value for those symbols.

    When you say "The Sub cannot show the wanted Special characters properly", what do you mean? Is it displaying the characters in a message box that's the problem? In what context can they not be 'shown'?


    • Edited by JosephFox Wednesday, May 23, 2012 4:23 PM
    Wednesday, May 23, 2012 4:22 PM
  • Did you try my code? I said 'VBA 2003', but I have now tested it with Word 2007 VBA. After inserting the 'greater than or equal to' symbol, selecting it, and running a macro with that code, it pops up with '8805', which is its numeric representation.

    I believe you can go through the text property of the range, and use the AscW to determine the numeric value for those symbols.

    When you say "The Sub cannot show the wanted Special characters properly", what do you mean? Is it displaying the characters in a message box that's the problem? In what context can they not be 'shown'?


    when I say "The Sub cannot show the wanted Special characters properly." I mean that when the  ActiveDocument.Range(0, 6) is

    EQ A (the subset sign) D the result shows A(B , the subset sign changed to (. The AscW and Asc values of subset sign are both 40 . If I change the subset sign to the Greek letter  alpha , the same problem occurred . And I do not know how to manage it.


    Thursday, May 24, 2012 1:55 AM
  • Could you paste a code sample? I'd like to try and replicate the problem.

    'When the ActiveDocument.Range(0, 6) is'.... you're saying you're getting a range containing the first 6 characters of the document, right?


    • Edited by JosephFox Thursday, May 24, 2012 7:38 PM
    Thursday, May 24, 2012 7:37 PM
  • Following Cindy Meister's Advice ,I use MathType SDK.

    The sample in MathType SDK in the following may be meaningful,but I do not know how the MTEF for cos^^2Theta, MTEF for sin^^2Theta and MTEF for 1 are generated .

    It looks much complicated,and I searched the docs in MathType SDK to get a explanation.Is there any function to generate them? I want to use this method to change commaon text mathematical signs( in special character table) and  Greek letters and characters with subscripts and superscripts to Microsoft 3.0 equation form.

    Can anyone with kindness help me?

    Sub MTEFTextSubstitution()
        Dim MTEFStr1$, MTEFStr2$, MTEFStr3$
        Dim stat

        'MTEF for cos^^2Theta
        MTEFStr1$ = "% MathType!MTEF!2!1!+-" + _
            " % feaaeaart1ev0aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLn" + _
            " % hiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr" + _
            " % 4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq-Jc9" + _
            " % vqaqpepm0xbba9pwe9Q8fs0-yqaqpepae9pg0FirpepeKkFr0xfr-x" + _
            " % fr-xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaci4yaiaac+" + _
            " % gacaGGZbWaaWbaaSqabeaacaaIYaaaaOGaeqiUdehaaa!3B65!"

        'MTEF for sin^^2Theta
        MTEFStr2$ = "% MathType!MTEF!2!1!+-" + _
            " % feaaeaart1ev0aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLn" + _
            " % hiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr" + _
            " % 4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq-Jc9" + _
            " % vqaqpepm0xbba9pwe9Q8fs0-yqaqpepae9pg0FirpepeKkFr0xfr-x" + _
            " % fr-xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaci4CaiaacM" + _
            " % gacaGGUbWaaWbaaSqabeaacaaIYaaaaOGaeqiUdehaaa!3B6A!"
       
        'MTEF for 1
        MTEFStr3$ = "% MathType!MTEF!2!1!+-" + _
            " % feaaeaart1ev0aaatCvAUfeBSjuyZL2yd9gzLbvyNv2CaerbuLwBLn" + _
            " % hiov2DGi1BTfMBaeXatLxBI9gBaerbd9wDYLwzYbItLDharqqtubsr" + _
            " % 4rNCHbGeaGqiVu0Je9sqqrpepC0xbbL8F4rqqrFfpeea0xe9Lq-Jc9" + _
            " % vqaqpepm0xbba9pwe9Q8fs0-yqaqpepae9pg0FirpepeKkFr0xfr-x" + _
            " % fr-xb9adbaqaaeGaciGaaiaabeqaamaabaabaaGcbaGaaGymaaaa!36A4!"
       
        Selection.Copy
       
        'Init API, reset transform
    '    If MTUtil.CheckMTDLLVersion = 0 Then Exit Sub
        If Not IsDLLVersionOK() Then Exit Sub
        MTXFormReset
       
        'first substitution
        stat = MTXFormAddVarSub( _
            mtxfmSUBST_ONE, _
            mtxfmVAR_SUB_PLAIN_TEXT, "<v1>", 0, _
            mtxfmVAR_SUB_MTEF_TEXT, MTEFStr1$, Len(MTEFStr1$), 0)
        If stat <> 0 Then
            MsgBox "1st MTXFormAddVarSub returned: " + Str(stat)
            Exit Sub
        End If
       
        'second substitution
        stat = MTXFormAddVarSub( _
            mtxfmSUBST_ONE, _
            mtxfmVAR_SUB_PLAIN_TEXT, "<v2>", 0, _
            mtxfmVAR_SUB_MTEF_TEXT, MTEFStr2$, Len(MTEFStr2$), 0)
        If stat <> 0 Then
            MsgBox "2nd MTXFormAddVarSub returned: " + Str(stat)
            Exit Sub
        End If

        'third substitution
        stat = MTXFormAddVarSub( _
            mtxfmSUBST_ONE, _
            mtxfmVAR_SUB_PLAIN_TEXT, "<v3>", 0, _
            mtxfmVAR_SUB_MTEF_TEXT, MTEFStr3$, Len(MTEFStr3$), 0)
        If stat <> 0 Then
            MsgBox "3rd MTXFormAddVarSub returned: " + Str(stat)
            Exit Sub
        End If

        'do the substitution
        stat = TransformGraphicEquation
        If stat <> 0 Then
            MsgBox "TransformGraphicEquation returned: " + Str(stat)
            Exit Sub
        End If
       
        MTTermAPI
       
        'Paste new equation
        Selection.Collapse Direction:=wdCollapseEnd
        Selection.PasteSpecial Placement:=wdInLine
    End Sub

    Monday, June 4, 2012 4:15 AM
  • http://word.mvps.org/faqs/macrosvba/FindReplaceSymbols.htm
    Tuesday, January 20, 2015 3:34 PM