Changing numbers

S

Steve Wylie

** This is a re-posting of the question asked on word.vba.beginners on 9
November. I'm reposting it here due to a paucity of responses to my first
post! **


We sometimes get documents where paragraph numbering needs changing
and for one reason or another we cannot use automatic paragraph
numbering in the document, just manual numbering. I am trying to
write a macro that will enable you to renumber basic paragraphs in a
document (nothing fancy, just ones numbered 1, 2, 3, 4 etc).

I am using the Selection.Find method to search for the first number,
then I want to replace it with another (incremented) number, search
for the next number, replace that, and so on till the end of the
document. I cannot get this to work. If finds the first number and
replaces it but then stops. Here's the code I've got so far, and I
daresay I'm going about it the wrong way.

Sub ReplaceNos()
Dim a As Integer
Number = 1
Selection.Find.ClearFormatting
With Selection.Find
.Text = Number
.Forward = True
.Wrap = wdStop
.Format = False
Do While .Execute
if Selection.Information(wdHorizontalPositionRelativeToTextBoundary)/72<2
then
Number = Number + 1
a = Selection.Text
a = a + 5
Selection.Text = a
Selection.Collapse Direction:=wdCollapseEnd
End if
Loop
End With
End Sub

This *should* search for the digit 1 and, if it is within the first 2
inches of the left margin, replace it with 6. Then search for "2",
replace it with 7, etc etc.

Silly question... why does it not work? I really need a lot more
knowledge of how the selection.find method works, I think!

Steve Wylie
 
H

Helmut Weber

Hi Steve,
"wdstop" is not a word constant at all.
It is "wdfindstop".
Then
stopping is not a good idea, if you want to
process all of the doc (the main story).
Then
use "option explicit".
If you had done so, VBA would have told you,
that the variable wdstop was not declared.
Then
Selection.Information(wdHorizontalPositionRelativeToTextBoundary)/72<2
limits you to using the selection object.
As the number you are looking for, is probably the first string
in a paragraph, I'd calculate its horizontal position by using
the page's margin and the indents of the paragraph.
You may even search for paragraphs which start with digits,
check the properties of the paragraph (indentations) and act
according to these criteria.
Keep on!
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 

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