Can anyone find the bug in this macro?

R

Raj

Hi Macro greats !

This macro will parse a document search for string that start as
RAJ-XXX- and appending a running serial number to it so that the
resulting string would be RAJ-XXX-001, RAJ-XXX-002, so on.
The macro works fine when the document is opened for the first time
and the macro is run.
It works everytime I close the document and open it.
If I run the macro the second time, without closing the document, the
macro parses the document twice and renumbers the tags twice, so I get
the tag numbers twice than what is actually there. Basically the Macro
runs twice before hitting the find.found = false statement.
I checked this with display statement.
Any help is welcomed
===========================================================================
Sub FSDTagGenerator()
'
' FSDTagGenerator Macro
' Macro created 08/20/03 by Raj
'
Dim CNT As Integer
Dim pos As Integer
Dim st1 As String
Dim st2 As String
Dim st3 As String

st2 = "RAJ-XXX-"
st1 = st2 + "*>"
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = st1
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
'Selection.Find.Execute
CNT = 0
Do While True
'For CNT = 1 To 1000
CNT = CNT + 1
If CNT < 10 Then
st3 = st2 + "00" + CStr(CNT)
ElseIf CNT < 100 Then
st3 = st2 + "0" + CStr(CNT)
Else
st3 = st2 + CStr(CNT)
End If
Selection.Find.Execute
If Selection.Find.Found = False Then
MsgBox ("No more finds")
Exit Do
End If
Selection.Find.Replacement.Text = st3
With Selection
.Find.Execute Replace:=wdReplaceOne
End With
Loop
End Sub


===========================================================================
 
H

Helmut Weber

Hi Raj,
sorry to say so, but your code runs perfectly,
at least with Word 97 and NT 4.0 and a not too
complicated doc of 5 pages.
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT 4.0
 
A

Andi Mayer

It works everytime I close the document and open it.
If I run the macro the second time, without closing the document, the
macro parses the document twice and renumbers the tags twice, so I get
the tag numbers twice than what is actually there. Basically the Macro
runs twice before hitting the find.found = false statement.


something in the back of my small brain is vage memory of problems
with the word" find".
Some of the arguments are nasty sticky, once set they stay set.

But I can't remember which, I believe on the http://www.mvps.org/word
site there is an explanation
 
R

Raj

Andi Mayer said:
something in the back of my small brain is vage memory of problems
with the word" find".
Some of the arguments are nasty sticky, once set they stay set.

But I can't remember which, I believe on the http://www.mvps.org/word
site there is an explanation

Guys,

I found the error, the parameter for .Wrap should be wdFindStop and
not wdFindContinue. Helmut, the code would work the first time, but if
you run it again with the document not closed, you will see the error,
I am talking about.

thanks for your help
RAJ
 

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