Consulting

Results 1 to 5 of 5

Thread: Select sentence by searchgin for keyword and add comment

  1. #1
    VBAX Regular
    Joined
    Aug 2013
    Posts
    6
    Location

    Question Select sentence by searchgin for keyword and add comment

    Hi,

    I'm trying the following. I want to search for a word, select the whole sentence and add a comment to the sentence.
    My macro works, but only for the word I search for. How can I select the whole sentence?
    Any help would be more than welcome.

    Regards,
    Frits


    Sub Keywordselection()
    '
    ' Select keywords and add automatic comment
    '
    Dim range As range
    Set range = ActiveDocument.Content

    Text = InputBox("Type a keyword you want to search for")

    Do While range.Find.Execute(Text) = True
    ActiveDocument.Comments.Add range, "This is the keyword incl sentence you have been searching for"
    Loop
    End Sub

  2. #2
    VBAX Contributor
    Joined
    Oct 2012
    Location
    Brisbane, Queensland, Australia
    Posts
    163
    Location
    Use

    Sub Keywordselection()
     '
     ' Select keywords and add automatic comment
     '
     Dim range As range, rngfound As range
     Set range = ActiveDocument.Content
     
    Text = InputBox("Type a keyword you want to search for")
     
    Do While range.Find.Execute(Text) = True
        Set rngfound = range.Sentences(1)
     ActiveDocument.Comments.Add rngfound, "This is the keyword incl sentence you have been searching for"
     Loop
     End Sub

  3. #3
    VBAX Regular
    Joined
    Aug 2013
    Posts
    6
    Location
    Great, thanks Doug!

    It works good, however there is a small error when type certain key words. The error is:
    "run-time error '4605': This method or property is not available because the object refers to the end of a table row."

    How could I solve this?

  4. #4
    VBAX Contributor
    Joined
    Oct 2012
    Location
    Brisbane, Queensland, Australia
    Posts
    163
    Location
    Try:

    Sub Keywordselection()
     '
     ' Select keywords and add automatic comment
     '
     Dim range As range, rngfound As range
     Set range = ActiveDocument.Content
     
    Text = InputBox("Type a keyword you want to search for")
     
    Do While range.Find.Execute(Text) = True
        Set rngfound = range.Sentences(1)
        rngfound.End = rngfound.End - 2
     ActiveDocument.Comments.Add rngfound, "This is the keyword incl sentence you have been searching for"
     Loop
     End Sub

  5. #5
    VBAX Regular
    Joined
    Aug 2013
    Posts
    6
    Location
    Great! It works. Thanks Doug.

Tags for this Thread

Posting Permissions

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