View Single Post
 
Old 10-07-2020, 09:50 AM
gmaxey gmaxey is offline Windows 10 Office 2016
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,429
gmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the roughgmaxey is a jewel in the rough
Default

Nothing is easy in Word if trying to deal with "Sentences" That is because Word doesn't see a sentence like you or I. Consider the code that Graham and Andrew have provided:


If you have this sentence:

I gave my old dog to Mr. Smith and he renamed him Fido.


If you run the code and enter "dog" as the word to find then you will be given the chance to delete "I gave my old dog to Mr." (not the entire sentence as we understand a sentence to be).


You could have course add code to handle such things as Mr. Mrs. Dr. etc. but it is hit and miss.



Also, you don't need the iResponse variable:

Code:
Sub Delete_Sentence_if_Word_found()
Dim strTexts As String, oRng As Range
  strTexts = InputBox("Enter texts to be found here: ", , "continued")
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    While .Execute(findText:=strTexts)
      oRng.Expand Unit:=wdSentence
      oRng.Select
      ActiveDocument.ActiveWindow.ScrollIntoView oRng, True
      Select Case MsgBox("Are you sure to delete the sentence?", vbYesNoCancel)
        Case vbYes
        oRng.Text = ""
        oRng.ListFormat.RemoveNumbers
        Case vbCancel: Exit Sub
      End Select
      oRng.Collapse wdCollapseEnd
    Wend
  End With
lbl_Exit:
  Set oRng = Nothing
  Exit Sub
End Sub
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote