Replacing (removing) numbers from beginning of line (Word 2000/Windows)

D

Dan

Hello All,

I'm slowly but surely gaining an education into at least the basics of
writing macros. I've got something that is pretty close to what I want,
but not quite there.

I've got a bunch of documents that have step numbers manually entered.
These steps should always be in one of four styles. So I put together a
macro that looks at the style and then searches for wildcard text that
should related to the step numbers and the following tab, and replaces
them with nothing.

It mostly worked, but would occasionally delete some other text that I
didn't want to delete in some of the lines.

Now I know the step numbers are always the first thing on a line, so I
added a paragraph mark to my wildcard text string. I figured that would
ensure it would only look for the numbers that follow a return.

However, when I added the paragraph mark to the .Text and
..ReplacementText entries, my macro gets stuck in a loop between "Do
While . . ." and "Loop". Entries.

Am I going about this the wrong way, or am I just making an ignorant
mistake?

Any help will be greatly appreciated!

Dan
---

Sub MacroName()
Dim FindStyle As Variant, k As Integer
FindStyle = Array("Para 1.1", "Para 1.1.1", "Para 1.1.1.1", "Para
1.1.1.1.1")
On Error Resume Next
For k = 0 To 3
With ActiveDocument.Range.Find
..ClearFormatting
..Replacement.ClearFormatting
..Text = "^p[1-9].*[1-9]^t"
..Replacement.Text = "^p"
..Format = True
..MatchWildcards = True
..Style = FindStyle(k)
Do While .Execute(Replace:=wdReplaceAll)
Loop
End With
Next
End Sub
 
D

Dan

Hello again,

Well, I figured out that if I change ^p to ^13 it stops that endless
loop from happening. So that's good!

However, I also noticed that this keeps me from removing a bunch of
step numbers if the step previous was at a different paragraph style.
So I've got to figure out how to fix this.
Slowly but surely I'm going to figure this stuff out. :)

Dan
 
G

G.G.Yagoda

Wildcards are strictly for people with brains, and sometimes, having
burned one's brains out, they burn the brain out of one's machine to
boot.

Anyway, you might try this humbler approach as an alternative. It
doesn't search for anything. It goes down paragraph by paragraph
skipping empty paragraphs, paragraphs that don't contain a tab, and
paragraphs where the text that precedes the tab contains no dots.

When it finds a number followed by a tab, it counts the dots therein
using a trick more or less stolen from Dave Lett - more, actually.
(Dave is the ranking master of the Replace function.)

Then it expands the range to include the tab, deletes the tab and text
numbers, and applies the appropriate numbering style to the paragraph.
Heading styles are used here; you can substitute your Para styles.

Hope you'll find some value in it.


Sub GoingDotty()
Dim R As Range, P As Paragraph, WithDots As Byte, WithoutDots As Byte,
DotCnt As Byte

For Each P In Selection.Range.Paragraphs
Set R = P.Range
If R.Characters.Count = 1 Then GoTo EndLoop
R.End = R.Start
R.MoveEndUntil vbTab
If InStr(R.Text, vbCr) > 0 Then GoTo EndLoop
If InStr(R.Text, ".") = 0 Then GoTo EndLoop
WithDots = Len(R.Text)
WithoutDots = Len(Replace(R.Text, ".", ""))
DotCnt = WithDots - WithoutDots
If R.Characters.Last <> "." Then DotCnt = DotCnt + 1
R.End = R.End + 1
R.Delete
P.Style = "Heading " & DotCnt
EndLoop:
Next
End Sub
 
D

Dan

That seems like a more efficient method of doing what I'm trying to do.
I will give it a try.

Thank you so much!

Dan
 

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