I've written this code in an attempt to first locate a string, or set of strings specified by the end-user. After finding the string the code will ask whether the user wants to in fact modify the content. If the user selects yes, it will then prompt the question of how many paragraphs including current paragraphs and after the found string, does the user want to delete.
I can't seem to get it to not simply delete the paragraphs from the beginning of the document. Is there anything I can do to achieve the function I need?
Thanks in advance!
This is MS Word VBA code.
Sub DeleteParagraphs_INPROCESS()
Dim strFindTexts As String
Dim strButtonValue As String
Dim nSplitItem As Long
Dim objDoc As Document
Dim pcnt As Long
strFindTexts = InputBox("Enter texts to be found here, and use commas to
separate them: ", "Texts to be found", "SNP, POS")
nSplitItem = UBound(Split(strFindTexts, ","))
With Selection
.HomeKey Unit:=wdStory
' Find the entered texts one by one.
For nSplitItem = 0 To nSplitItem
With Selection.Find
.ClearFormatting
.Text = Split(strFindTexts, ",")(nSplitItem)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWholeWord = False
.MatchCase = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchAllWordForms = False
.Execute
End With
Do While .Find.Found = True
strButtonValue = MsgBox("Delete the content?", vbYesNoCancel)
If strButtonValue = vbYes Then
pcnt = InputBox("How many paragraphs need to be deleted?", "Number of subsequent paragraphs:", "")
ActiveDocument.Paragraphs(pcnt).Range.Select
Selection.Delete
End If
.Collapse wdCollapseEnd
.Find.Execute
Loop
Next
End With
MsgBox ("Finished finding all entered texts.")
Set objDoc = Nothing
End Sub
Expected result: Deleting paragraphs, including the string and subsequent paragraphs according to what the user specifies in the input box, and not simply deleting paragraphs at the beginning of the document.