Consulting

Results 1 to 13 of 13

Thread: Detect sentences and do something with it in msword vba

  1. #1
    VBAX Newbie
    Joined
    Apr 2012
    Posts
    5
    Location

    Detect sentences and do something with it in msword vba

    Hello experts,
    I am vary new to VBA but I want to use it to solve my problems of text processing in ms-word and that why I am here expecting some appropriate helps.

    The problem part:
    I have some word .docx files which have some texts in a table
    (single row and column), where I want to:
    #1: detect the ending characters (".", "?", "!" "."" "?"", etc.) of each sentences and replace them with other texts.
    #2: detect some characters in each sentences accept the ending characters and
    replace them with some other texts.

    The full criteria is given in the "Criteria.docx" attachment.

    Please help me experts.
    Attached Files Attached Files

  2. #2
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    Having done a "bit" of work on this concept with another contributor to this site (Greg Maxey), I can tell you that this is not a simple project at all, and I fear you are not up to the task.

    To summarize, you are looking for a series of Find/Replaces to facilitate proof-reading, however, there are a whole host of issues in determining true sentences in VBA.

    You're going to need to do a good bit of work on recording some macros to do find/replace (this will help you learn VBA), and in addition... the following link may help you in looking at the whole concept of what determines a sentence (and what doesn't) in Word (and how what you and I think are a sentence is not necessarily what Word thinks is a sentence).

    Check out the link, try recording some macros, and come back to describe your problems... we should be able to help you move forward, but simply giving you the entire solution to your problem based on your criteria.docx would be a lot of time.

    Hope this helps.

    http://gregmaxey.mvps.org/word_tip_p...sentences.html

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    That should get the OP started.

    A "bit" of work? LOL

    VanDiZ, just to get a taste of how much is involved in trying to do this...download and open up the add-in and look at the code. LOL.
    Last edited by fumei; 04-25-2012 at 08:19 PM.

  4. #4
    VBAX Newbie
    Joined
    Apr 2012
    Posts
    5
    Location
    Thank you vary much, Frosty and fumei for your replies and obviously for the add-in also.

    fumei, I have copy-pasted the add-in file in the StartUp folder of msword. The add-in seems to work properly, but I can not view it's code, word says it is looked.

    I did some simple coding before posting here to check word's behavior in sentence detection, and as you said Frosty, I found ms-word to do not detect sentences properly. Bellow is the code.


    [vba]Sub Xyz()

    Dim Cnt As Long
    Dim TSCnt As Long
    Dim CSChrCnt As Long
    Dim TSChrCnt As Long
    Dim TSChrCntTmp As Long
    Dim CSEndChrRng1 As Range
    Dim CSEndChrRng2 As Range
    Dim CSRstChrRng1 As Range
    Dim CSRstChrRng2 As Range

    TSCnt = ActiveDocument.Sentences.Count
    Cnt = 1

    Do
    CSChrCnt = ActiveDocument.Sentences(Cnt).Characters.Count
    TSChrCnt = ActiveDocument.Sentences(Cnt).Characters.Count + TSChrCntTmp

    Set CSEndChrRng1 = ActiveDocument.Range(TSChrCnt - 2, TSChrCnt - 1)
    Set CSRstChrRng1 = ActiveDocument.Range(TSChrCnt - CSChrCnt, TSChrCnt - 2)
    Set CSEndChrRng2 = ActiveDocument.Range(TSChrCnt - 3, TSChrCnt - 1)
    Set CSRstChrRng2 = ActiveDocument.Range(TSChrCnt - CSChrCnt, TSChrCnt - 3)

    MsgBox _
    "CSEndChrRng1 = #" & CSEndChrRng1 & "#" _
    & String(2, Chr(13)) _
    & "CSEndChrRng2 = #" & CSEndChrRng2 & "#" _
    & String(2, Chr(13)) _
    & "CSRstChrRng1 = #" & CSRstChrRng1 & "#" _
    & String(2, Chr(13)) _
    & "CSRstChrRng2 = #" & CSRstChrRng2 & "#" _
    & String(2, Chr(13)) _
    & "TSCnt = #" & TSCnt & "#" _
    & Chr(13) _
    & "Cnt = #" & Cnt & "#"

    TSChrCntTmp = TSChrCnt
    Cnt = Cnt + 1
    Loop Until TSCnt = Cnt
    End Sub

    '(It is not well coded, it shows error like "Run-time error '4608' value out of range".)[/vba] You can see, with the range "CSEndChrRng1" and "CSEndChrRng2" it is possible to edit the ending characters as said in the Criteria.docx using some condition contracts and "Text" property of the range object.
    And with the "CSRstChrRng1" and "CSRstChrRng2" ranges it's maybe possible to edit the rest of a sentence accept the ending characters maintaining the Criteria.docx conditions.
    I have added the msgbox part to see what the code is doing.

    I am attaching a .zip file which have some sample .docx files which I want the code to properly operate on following the "Criteria.docx" conditions.

    Please experts, post some sample codes using the DSS add-in, it will help me to get familiar with it.

    Again, thanks a lot, Frosty and fumei for your replies.
    Attached Files Attached Files

  5. #5
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    In addition to having the addin load from the startup folder, you need to actual open the template in order to see the code. If you attempt to expand the project without having the temate open, you will get the "project locked" error, at which point you will need to restart word, then open the template, then attempt to expand the project in the VBA IDE again.

    Take a look, and then post your questions again. There are already a lot of samples in the project

  6. #6
    VBAX Newbie
    Joined
    Apr 2012
    Posts
    5
    Location
    Thanks for your guidance Frosty , I was able to view the code and the document also but the code was sufficiently complex for me.

    As my tests, the DSS is not detecting sentences properly but better then the MSWords native detection.
    I have highlighted the wrong detections done by the DSS in the samples. So please take a look at the samples in the zip file attached.

    My primary problem is to properly detect sentences programmatically, and I do not understanding how to code with the DSS, how can I use the DSS in a macro ??? Please post some sample codes using DSS or point me to some example codes.

    BTW, can you please tell me how to add custom icons for macros in the quick access toolbar ??? I am using MSOffice 2010 Professional.
    Attached Files Attached Files

  7. #7
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    VanDiZ,

    As you say, your primary problem is "to properly detect sentences programmatically" -- that is what we were trying to solve with the DSS. And, after several weeks of programming by two very experienced programmers, it is better than MS Word, but it still isn't perfect. The class I programmed is flexible enough to be useful to an experienced programmer, but it is not for a novice.

    Greg's coding of the DSS using the class is actually the samples you are looking for. But since you're asking for samples on how to use the DSS, that means to me that you don't have the depth of understanding in VBA to be able to use the DSS (or understand the samples contained therein).

    My best suggestion is this:
    For your immediate problem, figure out how to do what you want to do manually, then record some macros that do the manual process, and then ask some questions (new threads) on how to structure all those recorded macros together. This will help you learn VBA a bit better, with the idea that you might be able to come back to the DSS later and understand the code.

    But if the DSS is too complex for your at this time, then making the DSS "better" for your purposes is not possible for you, at this time.

  8. #8
    VBAX Newbie
    Joined
    Apr 2012
    Posts
    5
    Location
    Thanks Frosty for your rapid reply,

    So you are saying the codings in the "modAppUI", "modMain" and "OpenBrowser" modules are the semple codes. Thanks for pointing me out I will surely check those and obviously will try to understand.

    Recording seems not to solve my problems, it mostly generates codes using selection object and which is not appropriate for my work I think.

    But Frosty, can you please review the code I had posted previously and check why some time it shows error like "Run-time error '4608' value out of range", where i am doing wrong or what should I do to make it more error-less? And can you please tell me is it possible to use regular-expressions in the "IF" statement like the find object?

    Again, thank you vary much for your reply Frosty.

  9. #9
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Regarding your code, you have an array named String? No a good idea.
    [vba]String(2, Chr(13))[/vba]

  10. #10
    VBAX Newbie
    Joined
    Apr 2012
    Posts
    5
    Location
    Thanks for your quick reply fumei and vary sorry for my delay.

    msword VBA's built-in help says:
    String Function
    Returns a Variant (String) containing a repeating character string of the length specified.

    Is it actually returns array of a given string of the given length ???

    If so and if it is the problem then, I can replace "String(2, Chr(13))" with the "Chr(13) & Chr(13)" so now the code is as bellow

    [vba]Sub Xyz()

    Dim Cnt As Long
    Dim TSCnt As Long
    Dim CSChrCnt As Long
    Dim TSChrCnt As Long
    Dim TSChrCntTmp As Long
    Dim CSEndChrRng1 As Range
    Dim CSEndChrRng2 As Range
    Dim CSRstChrRng1 As Range
    Dim CSRstChrRng2 As Range

    TSCnt = ActiveDocument.Sentences.Count
    Cnt = 1

    Do
    CSChrCnt = ActiveDocument.Sentences(Cnt).Characters.Count
    TSChrCnt = ActiveDocument.Sentences(Cnt).Characters.Count + TSChrCntTmp

    Set CSEndChrRng1 = ActiveDocument.Range(TSChrCnt - 2, TSChrCnt - 1)
    Set CSRstChrRng1 = ActiveDocument.Range(TSChrCnt - CSChrCnt, TSChrCnt - 2)
    Set CSEndChrRng2 = ActiveDocument.Range(TSChrCnt - 3, TSChrCnt - 1)
    Set CSRstChrRng2 = ActiveDocument.Range(TSChrCnt - CSChrCnt, TSChrCnt - 3)

    MsgBox _
    "CSEndChrRng1 = #" & CSEndChrRng1 & "#" _
    & Chr(13) & Chr(13) _
    & "CSEndChrRng2 = #" & CSEndChrRng2 & "#" _
    & Chr(13) & Chr(13) _
    & "CSRstChrRng1 = #" & CSRstChrRng1 & "#" _
    & Chr(13) & Chr(13) _
    & "CSRstChrRng2 = #" & CSRstChrRng2 & "#" _
    & Chr(13) & Chr(13) _
    & "TSCnt = #" & TSCnt & "#" _
    & Chr(13) _
    & "Cnt = #" & Cnt & "#"

    TSChrCntTmp = TSChrCnt
    Cnt = Cnt + 1
    Loop Until TSCnt = Cnt
    End Sub[/vba] Now it seems not to show the error, I had tested it with a large document (i.e. =rand(99,200)).


    Some other questions, please answer.
    #1:> Is it possible to use regular-expressions in the IF statement, Do loops, For loops etc. like the find object?
    #2:> How can I add custom icons for macros in the quick access toolbar ??? I am using MSOffice 2010 Professional.

    Thanks again fumei for your reply.

  11. #11
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    You can use regular expressions in conjunction with the LIKE operator (check the help file). This is still somewhat limited... and Wildcard searching may be more useful. I would do searches on both of those.

    Custom icons in the QAT can be done and distributed, but you're going to need to look up how to customize the XML. There is no user-interface way to do this easily.

    Do google searches on "Custom UI Editor for Microsoft Office"

    But the short answer is:
    1. You need to load the custom graphic into the file xml file in the right way
    2. You need to modify the .xml by hand (since CustomUI Editor won't do it for you, but it can give you a quick entry into doing it).

    Add a custom QAT button ONLY for the document, and then rename that document to .zip. Explore through the .zip file... you will see a "userCustomization" folder with a "CustomUI.xml" file in it. In there, you will see something related to the "mso:qat" and then you should see information about your button. You will need to delete the imageMso and replace with "getImage" and have the value be the name of the graphic you loaded into your document.

    This is a cursory overview, as if you are not familiar with manipulating the FluentUI in Word 2010 in general, you've picked the hardest thing to do to start.

  12. #12
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,334
    Location

    Custom QAT Macro icons

    Question #2: See http://gregmaxey.mvps.org/word_tip_p...mage_text.html

    Quote Originally Posted by VanDiZ
    Thanks for your quick reply fumei and vary sorry for my delay.

    msword VBA's built-in help says:
    String Function
    Returns a Variant (String) containing a repeating character string of the length specified.

    Is it actually returns array of a given string of the given length ???

    If so and if it is the problem then, I can replace "String(2, Chr(13))" with the "Chr(13) & Chr(13)" so now the code is as bellow

    [vba]Sub Xyz()

    Dim Cnt As Long
    Dim TSCnt As Long
    Dim CSChrCnt As Long
    Dim TSChrCnt As Long
    Dim TSChrCntTmp As Long
    Dim CSEndChrRng1 As Range
    Dim CSEndChrRng2 As Range
    Dim CSRstChrRng1 As Range
    Dim CSRstChrRng2 As Range

    TSCnt = ActiveDocument.Sentences.Count
    Cnt = 1

    Do
    CSChrCnt = ActiveDocument.Sentences(Cnt).Characters.Count
    TSChrCnt = ActiveDocument.Sentences(Cnt).Characters.Count + TSChrCntTmp

    Set CSEndChrRng1 = ActiveDocument.Range(TSChrCnt - 2, TSChrCnt - 1)
    Set CSRstChrRng1 = ActiveDocument.Range(TSChrCnt - CSChrCnt, TSChrCnt - 2)
    Set CSEndChrRng2 = ActiveDocument.Range(TSChrCnt - 3, TSChrCnt - 1)
    Set CSRstChrRng2 = ActiveDocument.Range(TSChrCnt - CSChrCnt, TSChrCnt - 3)

    MsgBox _
    "CSEndChrRng1 = #" & CSEndChrRng1 & "#" _
    & Chr(13) & Chr(13) _
    & "CSEndChrRng2 = #" & CSEndChrRng2 & "#" _
    & Chr(13) & Chr(13) _
    & "CSRstChrRng1 = #" & CSRstChrRng1 & "#" _
    & Chr(13) & Chr(13) _
    & "CSRstChrRng2 = #" & CSRstChrRng2 & "#" _
    & Chr(13) & Chr(13) _
    & "TSCnt = #" & TSCnt & "#" _
    & Chr(13) _
    & "Cnt = #" & Cnt & "#"

    TSChrCntTmp = TSChrCnt
    Cnt = Cnt + 1
    Loop Until TSCnt = Cnt
    End Sub[/vba] Now it seems not to show the error, I had tested it with a large document (i.e. =rand(99,200)).


    Some other questions, please answer.
    #1:> Is it possible to use regular-expressions in the IF statement, Do loops, For loops etc. like the find object?
    #2:> How can I add custom icons for macros in the quick access toolbar ??? I am using MSOffice 2010 Professional.

    Thanks again fumei for your reply.
    Greg

    Visit my website: http://gregmaxey.com

  13. #13
    VBAX Master
    Joined
    Feb 2011
    Posts
    1,480
    Location
    I should have known you had a tip for this, Greg Nice link!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •