Find and Replace in Bookmarked Text - First Occurence Only

S

Stephen Eccleston

Hi,

Using Word 2003: I am looking for the VBA code to find and replace the first
end of paragraph in a block of bookmarked text with a colon and the paragraph
mark (find "^P" replace with ":^P").

So i have Sub that accepts the name of the bookmark, the string to find and
the string to replace - but i am stuck finding the code to execute the actual
find and replace.

Can anyone help?
 
D

Doug Robbins - Word MVP

Best to show us the code that you are starting with so a suggestion can be
made as to how to modify it.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 
S

Stephen Eccleston

Hi Doug,

Each bookmark's text is several line long, I have 1500 in my document, I
need the first line in each bookmark to end with ":^p", some do have ":^p"
and some don't.

This is my code so far;

Private Sub CheckEndPara()

Dim sSourceName, sFindText, sReplaceText, sBkName, sBkPrefix As String
Dim d As Word.Document

sSourceName = "REQUIREMENTS.DOC"
sFindText = "^p"
sReplaceText = ":^p"
sBkPrefix = "REQ"

Documents.Open FileName:=sSourceName
Application.ScreenUpdating = False

'For each Bookmark in Source, extract book mark name and test
For Each Bookmark In ActiveWindow.Bookmarks
'Extract book mark name
sBkName = Bookmark.Name
'If this is a "REQ" bookmark then update it
If Left(sBkName, 3) = sBkPrefix Then
UpdateBookmark sBkName, sFindText, sReplaceText
End If
Next

'Close the documents
d.Close
Set d = Nothing

End Sub

Sub UpdateBookmark(BookmarkToUpdate As String, TextToFind As String,
TextToReplace As String)
Dim BMRange As Range
Set BMRange = ActiveDocument.Bookmarks(BookmarkToUpdate).Range

{ what code do I put here? }

ActiveDocument.Bookmarks.Add BookmarkToUpdate, BMRange
End Sub
 
D

Doug Robbins - Word MVP

Hi Stephen,

Sub UpdateBookmark(BookmarkToUpdate As String)
Dim BMRange As Range
Set BMRange =
ActiveDocument.Bookmarks(BookmarkToUpdate).Range.Paragraphs(1).Range
BMRange.End = BmRange.End - 1
BMRange.Text = BMRange.Text & ":"
End Sub

should do it
--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP, originally posted via msnews.microsoft.com
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top