-
Yes. By using bookmarks.
Bookmarks can be nested - that is, a section of text that is bookmarked, may contain a portion within it that is also bookmarked. Howeverjust to make it simple, I am attaching a ZIP file with a little demo.
It has a few paragraphs in it. "Blah blah blah....." The FIRST paragraph also has the text "ABC Hospital" in it. There is also a paragraph, a different paragraph, that is bookmarked. The bookmark name is "OuttaHere". In the paragraph is some text "Take this paragraph out", but the text itself is not really relevant.
There is an ActiveX command button in the document. If you have macro security set to high. this will not work. You may get a warning regarding it, as ActiveX controls set off Word security warnings.
You can run the code by either going into the VBE and running the routine UseBookmarks; or finding UseBookmarks in the list of available macros - Tools > Macro > Macros (or press Alt-F8).
Or you can click the button. In any case, the routine UseBookmarks will fire, and the bookmarked paragraph will be deleted. The code is in the attached file, but here it is.
[vba]Sub UseBookmarks()
Dim oRange As Word.Range
Set oRange = ActiveDocument.Range.Paragraphs(1).Range
With oRange.Find
.Text = "ABC Hospital"
.Execute
If .Found = True Then
ActiveDocument.Bookmarks("OuttaHere").Range.Delete
End If
End With
Set oRange = Nothing
End Sub
Private Sub cmdDeleteBM_Click()
Call UseBookmarks
End Sub[/vba]
What this does is create a Range object of Paragraph(1), the first paragraph. it searches the first paragraph for the text "ABC Hospital". If it finds it, it deletes the range of the bookmark "OuttaHere". Note that you must delete the Range of the bookmark. If you just delete the bookmark...then just the bookmark is deleted, not the text.
So there you go. VBA looking for a specific word(s), and based on that word (being found, or not), deleteing a specific portion of the document.
To take a look, download the attached file, and unzip it. Open it and run the code, as per the mention above.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules