Paragraph.Range and Page Breaks?

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
 
J

Jean-Guy Marcil

Hi Ed,

Try this:
'_______________________________________
Dim oP As Paragraph
Dim rngp As Range

For Each oP In ActiveDocument.Paragraphs
Set rngp = oP.Range
If rngp.Characters(1).Text = "1" Then
If rngp.Start <> 0 Then
rngp.SetRange rngp.Start, rngp.Start + 1
rngp.InsertBreak Type:=wdPageBreak
Else
rngp.Characters(1).Delete
End If
Else
rngp.Characters(1).Delete
End If
Next oP
'_______________________________________

<rngp.InsertBreak Type:=wdPageBreak>
replaces the whole range (in this case the current oP in the loop), unless
you use a Collapse statement or you reset the range as I did. I like the way
I did it because I not only insert the page break where you want, but at the
same time I delete the leading "1" as you also want to do.

Note that if you have "empty paragraphs" (i.e. paragraphs consisting of
nothing but a paragraph mark -¶- they will be deleted)

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
E

Ed

Thank you! I *knew* there was something important I was missing! Let's see
if I have this right: the way I had it, the Page Break replaced the whole
range, *including* the paragraph break at the end of it. So when it went to
the next paragraph, it skipped what appeared to be the start of the next
line -but was actually just a continuation of the same paragraph it was
already in. By resetting the range to include actually only the first
character, the paragraph break is left intact, and Next oP will take the
code down one line. Perfect!

Ed

(BTW - there are *NO* "empty paragraphs" in this doc. They either have the
1 or the garbage character I posted about earlier.)
 
J

Jean-Guy Marcil

Ed said:
Thank you! I *knew* there was something important I was missing! Let's see
if I have this right: the way I had it, the Page Break replaced the whole
range, *including* the paragraph break at the end of it. So when it went to
the next paragraph, it skipped what appeared to be the start of the next
line -but was actually just a continuation of the same paragraph it was
already in. By resetting the range to include actually only the first
character, the paragraph break is left intact, and Next oP will take the
code down one line. Perfect!

I am not the most knowledgeable regarding the iteration of collections, but
I think you got the gist of it.
Ed

(BTW - there are *NO* "empty paragraphs" in this doc. They either have the
1 or the garbage character I posted about earlier.)

Just thought I'd mention it... then you do not have to worry about this.

Glad I could help.

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.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