SetRange not working as expected?

S

ShaunW

Hi,
I am currently trying to write a VBA macro (Word 2003) that syntax checks
a document. I need to check that a style contains only the following text:

Requirement: aaa_bbb_123, aaa_bbb_123, aaa_bbb_123, aaa_bbb_123,

There is no limit on the number of comma seperated items within the
paragraph, but each item must be in this form.

So I wrote the following code extract which attempts to use ranges to select
a single requirement "aaa_bbb_123," and then reduce the overall (refRng)
range by the currently processed requirement (singleref):

Dim refRng As Range
Dim singleReq As String
Dim singleref As Range

colonIn = InStr(aParagraph, ":")

'Set refrng to be the paragrph minus the text Requiremnet:
and minus the carriage return at the end
Set refRng = mySTS.Range(Start:=aParagraph.Range.Start +
colonIn, _
End:=aParagraph.Range.End - 1)

While (InStr(refRng, ","))
'extract text upto comma
Set singleref = mySTS.Range(Start:=refRng.Start, _
End:=refRng.Start +
InStr(refRng, ","))
singleref.Select

'match string to pattern
If Trim(singleref.Text) Like "?*_?*_?*," Then
Else
mySTS.Comments.Add Range:=singleref,
Text:="SyntaxChecker: Not Matched" & singleref.Text
End If

refRng.SetRange Start:=singleref.End, _
End:=refRng.End

Wend


However, when I attempt to parse the paragraph:
Reference: VE900_ISUP_090, VE900_ISUP_091, VE900_ISUP_092, VE900_ISUP_093,
,VE900_ISUP_094,VE900_ISUP_095, VE900_ISUP096, VE900_ISUP_097

The code gets stuck in an infinate loop due to the text "093,
,VE900_ISUP_094,VE900_ISUP_095, VE900_ISUP096, VE900_ISUP_097"
For each iteration of the loop, the refRng.Start value incriments, but the
value of the refRng.Text remains the same making the value of singleref
always "".

I don't understand why the textual value of the refRng.Text does not shrink
by one character for each iteration of the loop.

Is this just some bad code (which is highly likely) caused by my lack of
understanding of the setrange method, or is there something more untoward
going on?

Any help greatly appreciated as I am fairly new to VBA.
Cheers,
Shaun
 
J

Jay Freedman

Hi Shaun,

I'm not going to try to analyze the problem you ran into, because there's a
simpler way to approach the problem: Read the paragraph's text into a string
variable, use the Split function to separate it into substrings delimited by
commas, and check each substring for syntax. If a substring fails the check,
find that substring in the paragraph and attach a comment to it. Here's
sample code:

Sub CheckSyntax()
Dim aParaRg As Range
Dim WorkStr As String
Dim WorkAry As Variant
Dim colonIn As Long, ind As Long
Dim oRg As Range

Set aParaRg = ActiveDocument.Paragraphs(1).Range
' or however you identify the paragraph to check...

WorkStr = aParaRg.Text

colonIn = InStr(WorkStr, ":")
If colonIn > 0 Then
' remove everything up to the colon
WorkStr = Mid$(WorkStr, colonIn + 1)
End If

' chop the string into comma-delimited substrings
WorkAry = Split(WorkStr, ",")

' check the substrings
For ind = 0 To UBound(WorkAry)
WorkStr = Trim(WorkAry(ind))
If Not (WorkStr Like "?*_?*_?*") Then
Set oRg = aParaRg.Duplicate
With oRg.Find
.Text = WorkStr
.Format = False
.Forward = True
.Wrap = wdFindStop
If .Execute Then
ActiveDocument.Comments.Add Range:=oRg, _
Text:="SyntaxChecker: Not Matched " & _
WorkStr
End If
End With
End If
Next ind
End Sub
 
H

Helmut Weber

Hi Shaun,

I don't know where the problem is in your code.
Can't paste it in the VBA-Editor.
Don't have sufficient reliable testing material.

Do you want:
1) the text to be in the right syntax,
or do you want to know,
2) where it isn't in the right syntax?

For 1)
I'd make the text absolutely consistent first,
example, as far the commas are concerned:
Replace every comma with comma plus space.
Replace every comma followed by more than one space
with comma plus space.
Insert a comma before the paragraph mark.

Then You got:
Reference: some text, some text, some text,¶

Then You need a grammar for strings like " VE900_ISUP_090,"
which seems to be:
Space, 2 capital letters, three digits, underscore,
4 capital letters, underscore, 3 digits, comma

all repeating from "reference:" to a paragraphs end.


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
S

ShaunW

Thanks Jay,
Your solution provides me with a working solution to my problem. That
allows me to complete my task. I will endeavour to try and workout why my
setrange does not seem to work as I expect.

If anyone can answer this specifically, please feel free to post.

Thanks,
Shaun
 
S

ShaunW

Thanks Helmut,
Jay has already provided me with a solution to the problem.
Regards,
Shaun
 

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