I thought that I was close but I’m seeing some weird behavior. A little background. I’m writing a requirements specification and each requirement should have a unique number (e.g. HRS0010, HRS0020). Once the number is established in the first revision of the document it mustn’t change; that’s why I haven’t used something like SEQ, other fields, or lists. What I’d like to be able to do is create Normal paragraphs for each requirement and preface the paragraph with a placeholder text.
E.g.
[REQ0000] This is a requirement.
Once I’m ready, I’d like to run a macro that will wildcard search for the placeholder and replace them all with an incrementing value. I’d like something generic so I should be able to specify the text prefix of the placeholder (the “REQ” in the example) and enter a custom prefix for the replacement (e.g. “HRS” or “SRS”). All this so that something like
REQ0000 First requirement.
REQ0000 Second requirement.
REQ0000 Third requirement.
becomes
HRS0010 First requirement.
HRS0020 Second requirement.
HRS0030 Third requirement.
Here is the code that I’m working from.
' ' AutoIncReqNumber ' ' Searches through a selection for #### and replaces the text ' with where is an auto-incremented value that ' starts at 0 and increments by 10 each time. The primary use for ' this is to assign requirement numbers just before you commit the ' draft version of the document. This will not hold onto old numbers ' and will overwrite numbers that already exist so it shouldn't be ' used for documents that have already been in the review process ' against which people are starting development. ' Sub AutoIncReqNumber() Dim iCount As Integer Dim strSearchStr As String Dim strOutPrefix As String Dim strFormatStr As String ' Get the search prefix that you would like to ' replace in the text. This allows you to search ' for already created requirement numbers and ' replace them with incremented numbers. Use with ' caution strSearchStr = InputBox("Search string for Requirement (REQ, SSS, HRS, etc.)", "Search String", "REQ") strSearchStr = UCase(strSearchStr) strSearchStr = strSearchStr & "[0-9]{4}" ' Get the prefix that you would like to use with ' the default of SSS for a system specification strOutPrefix = InputBox("Type of Requirement (SSS, HRS, SRS, etc.)", "Requirement Type", "SSS") strOutPrefix = UCase(strOutPrefix) ' This is the main part of the find. It sets up the find ' criteria for the regular expression #### Selection.Find.ClearFormatting Selection.Find.Replacement.ClearFormatting iCount = 0 With Selection.Find .Text = strSearchStr .Forward = True .Wrap = wdFindStop .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceOne ' Here is the replacement. The iCount increments by ' 10 and the value is formatted into the replacement ' string before performing the replacement. Do Until Not .Found iCount = iCount + 10 strFormatStr = strOutPrefix & Format(iCount, "0000") .Replacement.Text = strFormatStr .Execute Replace:=wdReplaceOne Selection.MoveRight Unit:=wdCharacter, Count:=1 Loop End With End Sub
Seems to make sense to me but I’m seeing some odd behavior. Things like it will delete the first instance of what it finds and only replace the second instance. Or, it will only replace the first instance but with the number associated with the last instance. Can anyone make heads or tails of this and offer some suggestions?
Thanks,
Ben