E
Ed
In my attempts to delete a garbage character (see previous post), I created
a macro that looped through every paragraph and deleted the first character.
I used the Selection object, and it takes a bit of time to run. I thought I
would try re-doing using the Range object, which I am not at all familiar
with. It's not going exactly like I planned (are you surprised?), and I
could use a shove in the right direction understanding the Range object.
I have two situations: either each paragraph starts with a "1", or it starts
with a garbage character. If 1, delete and insert a page break; Else just
delete. Then move to the next paragraph. To accomplish this, I set a For
Each loop through all Paragraphs and Set a range to the paragraph.
At the top of the document is a 1 - this was deleted and a page break
inserted. The next iteration deleted the page break and skipped the
paragraph under it! The loop went on as designed until it hit the next 1 -
the 1 was deleted, but the page break was inserted AT THE TOP OF THE
DOCUMENT!
Okay - I have a serious gap in my understanding of working with ranges! Any
help is appreciated.
Ed
Loop code:
For Each oP In ActiveDocument.Paragraphs
Set rngP = oP.Range
If rngP.Characters(1).Text = "1" Then
rngP.Characters(1).Delete
rngP.InsertBreak Type:=wdPageBreak
Else: rngP.Characters(1).Delete
End If
Next oP
a macro that looped through every paragraph and deleted the first character.
I used the Selection object, and it takes a bit of time to run. I thought I
would try re-doing using the Range object, which I am not at all familiar
with. It's not going exactly like I planned (are you surprised?), and I
could use a shove in the right direction understanding the Range object.
I have two situations: either each paragraph starts with a "1", or it starts
with a garbage character. If 1, delete and insert a page break; Else just
delete. Then move to the next paragraph. To accomplish this, I set a For
Each loop through all Paragraphs and Set a range to the paragraph.
At the top of the document is a 1 - this was deleted and a page break
inserted. The next iteration deleted the page break and skipped the
paragraph under it! The loop went on as designed until it hit the next 1 -
the 1 was deleted, but the page break was inserted AT THE TOP OF THE
DOCUMENT!
Okay - I have a serious gap in my understanding of working with ranges! Any
help is appreciated.
Ed
Loop code:
For Each oP In ActiveDocument.Paragraphs
Set rngP = oP.Range
If rngP.Characters(1).Text = "1" Then
rngP.Characters(1).Delete
rngP.InsertBreak Type:=wdPageBreak
Else: rngP.Characters(1).Delete
End If
Next oP