M
Maritza van den Heuvel
Hello everyone,
Up to now, my VBA has been limited to basic Find and Replace macros, or
updating links and other formatting issues. Now I'm trying to do
something a bit more complex involving looping and a counter, and I'm
coming unstuck completely. Any help would be much appreciated!
My apologies if this is quite a long post, but I thought the more detail
the more likely that someone may be able to see what I'm trying to do
and have some answers that will help me learn how to do this. Part of my
problem is that with all the reading up I've done (MVPS, various other
sites) that I can't see the forest for the trees anymore.
MTIA!
Maritza van den Heuvel
Team Lead: Documentation and QA
STT Trainer : Kaplan IT Learning
MACRO SCENARIO:
I have two placeholder strings that appear in a document, call them
"StringA" and "StringB" for now. I need to loop through the document
and do the following:
All occurrences of StringA need to be replaced with a named bookmark,
let's call it "FirstBookmarkName". Each of these bookmarks needs to
increment for each occurrence of the string, so if there were ten
occurrences of "StringA", I'm left with 10 bookmarks in the document,
starting with "FirstBookmarkName0" and ending with "FirstBookmarkName9".
Each bookmark must be inserted in the position where each occurrence of
"StringA" was replaced. The original text strings must be deleted.
I need to do exactly the same with "StringB", but instead the
replacement should be "SecondBookmarkName" throughout.
My attempt so far:
I have two main subs, each using selection.find to find the relevant
string, and replace it with an empty string (""), while adding the
required bookmark (with the incremented counter concatenated onto the
bookmark name) to the ActiveDocument. After adding the bookmark, the
counter is supposed to increment by one as the starting point for the
next pass. There is a third sub that simply calls these two subs.
The bad news is, that the two subs compile without errors, but none of
the replacement or incrementing is happening, and only one instance of
each bookmark is being added ...
Since my initial attempt, I have done some more reading up, and have
come to some conclusions/assumptions:
1) From the error about "bad bookmark name" I keep getting, I'm starting
to suspect that I cannot use the str function to concatenate the counter
value as a string onto the bookmark name?
2) That perhaps with Find.Execute I can only use text strings as
replacements, and not bookmarks? I read something that the only way to
use a non-text entity as the replacement value is to copy this entity to
the clipboard first and then insert it from the clipboard during the
find and replace? (If that's true, then HOW do I do this?)
3) I found some sample code which led me to the conclusion that I first
need to use selection.find to find the required string, and then with
selection.find execute the replace and bookmarks.add commands as part of
a Do While loop. Trouble is ... I can't seem to get the syntax right
with this. :-(
Here's the code that I've pieced together so far on my own in one sub.
This is the sub that compiles, but only the Bookmarks.Add works - ONCE:
Sub InsertMarkerA()
Dim Counter As Integer
Counter = 0
With Selection.Find
.Text = "StringA"
.Replacement.Text = ""
.Execute Replace:=wdReplaceOne
ActiveDocument.Bookmarks.Add Name:="FirstBookmarkName"+Str(Counter)
Counter = Counter + 1
End With
End Sub
Here's the other sub, but with the changes to the selection.find and Do
loop that I found tonight. This gives a compile error (external name not
defined) on the ReplaceWith variant, but I don't know how to fix it. :-(
So I don't know yet if this comes even close to solving (some/more) of
my problem.
Sub InsertMarkerB()
Dim Counter As Integer
Counter = 0
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "StringB"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute([ReplaceWith=""],
[Replace:=wdReplaceOne])
ActiveDocument.Bookmarks.Add Name:="CreateResultStep" +
Str(Counter)
Counter = Counter + 1
Loop
End Sub
Up to now, my VBA has been limited to basic Find and Replace macros, or
updating links and other formatting issues. Now I'm trying to do
something a bit more complex involving looping and a counter, and I'm
coming unstuck completely. Any help would be much appreciated!
My apologies if this is quite a long post, but I thought the more detail
the more likely that someone may be able to see what I'm trying to do
and have some answers that will help me learn how to do this. Part of my
problem is that with all the reading up I've done (MVPS, various other
sites) that I can't see the forest for the trees anymore.
MTIA!
Maritza van den Heuvel
Team Lead: Documentation and QA
STT Trainer : Kaplan IT Learning
MACRO SCENARIO:
I have two placeholder strings that appear in a document, call them
"StringA" and "StringB" for now. I need to loop through the document
and do the following:
All occurrences of StringA need to be replaced with a named bookmark,
let's call it "FirstBookmarkName". Each of these bookmarks needs to
increment for each occurrence of the string, so if there were ten
occurrences of "StringA", I'm left with 10 bookmarks in the document,
starting with "FirstBookmarkName0" and ending with "FirstBookmarkName9".
Each bookmark must be inserted in the position where each occurrence of
"StringA" was replaced. The original text strings must be deleted.
I need to do exactly the same with "StringB", but instead the
replacement should be "SecondBookmarkName" throughout.
My attempt so far:
I have two main subs, each using selection.find to find the relevant
string, and replace it with an empty string (""), while adding the
required bookmark (with the incremented counter concatenated onto the
bookmark name) to the ActiveDocument. After adding the bookmark, the
counter is supposed to increment by one as the starting point for the
next pass. There is a third sub that simply calls these two subs.
The bad news is, that the two subs compile without errors, but none of
the replacement or incrementing is happening, and only one instance of
each bookmark is being added ...
Since my initial attempt, I have done some more reading up, and have
come to some conclusions/assumptions:
1) From the error about "bad bookmark name" I keep getting, I'm starting
to suspect that I cannot use the str function to concatenate the counter
value as a string onto the bookmark name?
2) That perhaps with Find.Execute I can only use text strings as
replacements, and not bookmarks? I read something that the only way to
use a non-text entity as the replacement value is to copy this entity to
the clipboard first and then insert it from the clipboard during the
find and replace? (If that's true, then HOW do I do this?)
3) I found some sample code which led me to the conclusion that I first
need to use selection.find to find the required string, and then with
selection.find execute the replace and bookmarks.add commands as part of
a Do While loop. Trouble is ... I can't seem to get the syntax right
with this. :-(
Here's the code that I've pieced together so far on my own in one sub.
This is the sub that compiles, but only the Bookmarks.Add works - ONCE:
Sub InsertMarkerA()
Dim Counter As Integer
Counter = 0
With Selection.Find
.Text = "StringA"
.Replacement.Text = ""
.Execute Replace:=wdReplaceOne
ActiveDocument.Bookmarks.Add Name:="FirstBookmarkName"+Str(Counter)
Counter = Counter + 1
End With
End Sub
Here's the other sub, but with the changes to the selection.find and Do
loop that I found tonight. This gives a compile error (external name not
defined) on the ReplaceWith variant, but I don't know how to fix it. :-(
So I don't know yet if this comes even close to solving (some/more) of
my problem.
Sub InsertMarkerB()
Dim Counter As Integer
Counter = 0
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "StringB"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Do While Selection.Find.Execute([ReplaceWith=""],
[Replace:=wdReplaceOne])
ActiveDocument.Bookmarks.Add Name:="CreateResultStep" +
Str(Counter)
Counter = Counter + 1
Loop
End Sub