Consulting

Results 1 to 7 of 7

Thread: Need urgent help with a ‘find a word copy sentence’ macro

  1. #1
    VBAX Newbie
    Joined
    Mar 2019
    Posts
    4
    Location

    Question Need urgent help with a ‘find a word copy sentence’ macro

    Hi! I’m new to building macros and really need some urgent help with building a macro to do the foll:

    *search word doc for a keyword
    *then copy the sentence containing that keyword
    *then placing the sentence into a 3 column table along with the paragraph heading where the word was found and also the page number. So table should have 1 column with the sentence, 2nd column with the paragraph heading and 3rd column with page #.

    Table should be in the same word doc.

    Hope someone can help soon, thanks in advance!

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    The first hurdle you'll have to overcome is that VBA has no idea what a grammatical sentence is. For example, consider the following:
    Mr. Smith spent $1,234.56 at Dr. John's Grocery Store, to buy: 10.25kg of potatoes; 10kg of avocados; and 15.1kg of Mrs. Green's Mt. Pleasant macadamia nuts.
    For you and me, that would count as one sentence; for VBA it counts as 5 sentences.

    The second hurdle is having the code recognise what you mean by 'the paragraph heading', which lacks definition in your problem description.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  3. #3
    VBAX Newbie
    Joined
    Mar 2019
    Posts
    4
    Location
    Thanks for reviewing! Would it be possible to somehow ‘define’ a sentence as being any string starting with an upper case letter and ending with a period (.)? Not sure how vba works and if this would be possible. This definition would work in my doc as the sentences are fairly simple. Also for paragraph heading those are all bolded and underlined throughout so could that be used to define heading in this scenario? Appreciate your help.


    Thanks!

  4. #4
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    So with:
    I saw Mr. Smith on his way to lunch.
    you'd be happy for the result to be either:
    I saw Mr.
    or:
    Smith on his way to lunch.
    Somehow I doubt it. And there's all those abbreviations like i.e., e.g., etc. ...


    As for your headings, relying on formatting alone just makes things so much harder than they need to be. Anyone ever heard of using heading Styles?

    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  5. #5
    VBAX Newbie
    Joined
    Mar 2019
    Posts
    4
    Location
    Well I only mentioned those ‘features’ that could be used because I know for sure my doc has nothing of the sorts such as Mr., Ms. or any abbreviated words. This would be the easiest way to identify the sentences in my doc. In regards to the heading do you have any suggestions as to how this could be identified in a vba?

  6. #6
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,435
    Location
    Quote Originally Posted by smilez View Post
    Well I only mentioned those ‘features’ that could be used because I know for sure my doc has nothing of the sorts such as Mr., Ms. or any abbreviated words.
    I suggest you make an attitude adjustment. We're not mind readers here and you didn't bother to describe the content in a way anyone would understand what you're now telling us.

    Getting 'simple' sentences is trivial. For example:
    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long, StrOut As String
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = InputBox("What is the Text to Find")
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindStop
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
        .Execute
      End With
      Do While .Find.Found
        i = i + 1
        StrOut = StrOut & vbCr & .Sentences.First.Text
        .Collapse wdCollapseEnd
        .Find.Execute
      Loop
    End With
    StrOut = Replace(StrOut, vbCr & vbCr, vbCr)
    Application.ScreenUpdating = True
    MsgBox i & " instances found:" & StrOut
    End Sub

    A search will turn up various threads for converting such output to a table. What's more problematic though, it finding headings of the kind you've described. Basically, that means nesting another 'Find' within the above to look backwards through the document to the first preceding instance of a paragraph heading whose content is all bold and underlined. If you'd used heading Styles, the approach could be much simpler. See, for example: http://www.vbaexpress.com/forum/show...a-line-of-code
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

  7. #7
    VBAX Newbie
    Joined
    Mar 2019
    Posts
    4
    Location
    Hm, attitude adjustment?? See I could’ve said the same in response to your previous post but hey I figured everyone’s interpretation is different right? It all boils down to how the msg is read n interpreted by the reader n just to clarify I responded with not an ounce of attitude, was just on the run at the time n wanted to respond ASAP. Wouldn’t make sense since I’m the one looking for help believe me! Lol

    Anyhow, thanks again n I appreciate your efforts.

Posting Permissions

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