Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need to Select Through 1st Section Break in Word

Status
Not open for further replies.

clhare

Technical User
May 29, 2003
118
US
I'm trying to use a macro to clean up a document that will start out with a varying number of paragraph marks followed by a section break and two more paragraph marks before the text starts.

Is there a way to select starting with the 2nd paragraph mark in the document and including the first paragraph mark after the first section break and then delete that selection? The number of paragraph marks before the first section break will vary.

Example:

[paragraph mark]
[paragraph mark] <-- delete starts with this one
[paragraph mark] <-- delete
[paragraph mark] <-- delete
. <-- delete
. <-- delete
[section break] <-- delete
[paragraph mark] <-- delete includes this one
[paragraph mark]
Text starts here...


Any help is greatly appreciated!

Cheryl
 


Hi,

Would this work?
Code:
Dim par As Paragraph, sec As Section, iPar As Integer
iPar = 0
For Each par In Sections(1).Range.Paragraphs
    If iPar > 0 Then _
      par.Range.Delete
    iPar = iPar + 1
Next

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
That works!

The only thing that is wrong is that I lose my headers and footers when that first section break is deleted. Is there a way to copy the headers/footers from section 2 to section 1 before I run your code?

Any help is greatly appreciated!

Cheryl
 


Code:
Dim iPar As Integer

For iPar = Sections(1).Range.Paragraphs.Count - 1 To 2 Step -1
    Sections(1).Range.Paragraphs(iPar).Range.Delete
Next

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The only thing that is wrong is that I lose my headers and footers when that first section break is deleted. Is there a way to copy the headers/footers from section 2 to section 1 before I run your code? "

1. Headers and footers are child objects of the Section, and their content/values are contained in the Section break. Deleting the Section breaks deletes its child objects (the headers and footers). Word should be bringing Section 2 headers and footers into Section 1 if the section break between Section 1 and 2 is deleted. In other words, normally, the content/values are moved backwards. This is a key element of how Microsoft considers the Document Model. It has been this way since Word was only a DOS application.

2. Generally, any content can be put in your headers and footers. Could you please post precisely the code you are using?

BTW: while Skip's code apparently works for you, it could be better to declare and use a Range - rather than doing counting. You can set the Range object for precisely the range you want. Like this:
Code:
Dim r As Range
Set r = ActiveDocument.Range( _
      Start:=ActiveDocument.Sections(1).Range _
      .Paragraphs(2).Range.Start, _
      End:=ActiveDocument.Sections(2).Range _
      .Paragraphs(2).Range.Start)
r.Delete
So that your:

[paragraph mark]
[paragraph mark] <-- delete starts with this one
' i.e Section 1 Paragraph(2).Start
' in the code above, this makes the Start
' of the Range object =
' Start:=ActiveDocument.Sections(1).Range _
.Paragraphs(2).Range.Start
[paragraph mark] <-- delete
[paragraph mark] <-- delete
. <-- delete
. <-- delete
[section break] <-- delete
[paragraph mark] <-- delete includes this one
' i.e up to the Start of Section 2 Paragraph(2).Start
' in the code above, this makes the End
' of the Range object =
' End:=ActiveDocument.Sections(1).Range _
.Paragraphs(2).Range.Start
[paragraph mark]
Text starts here...

In other words it creates a range object that starts at the beginning of Section(1).Paragraph(2), and ends at the start of Section(2).Paragraph(2).

Then you delete the range.

Please post the exact code you are using, we can probably help with suggestions about fixing your header/footer.

Gerry
 



Jerry, you are the last word in Word! Don't get deleted!

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Well.... your code is all I've got so far. It does remove the extra paragraph marks in the "empty" section 1, but I do lose my footers.

The headers are ok because I don't link to previous in the headers. But, I do need to link to previous in the footers. So is there anything I can add to your code that will first copy the footers (first page, even, and odd) from section 2 to section 1?

Any help is greatly appreciated!

Cheryl
 



did you look at Gerry's post?

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 


The Sections should not be deleted UNLESS the last paragraph of a section INCLUDES the section mark. Then the Section is part of the paragraph, I believe.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
So, what do I need to do to make sure I don't lose the footers in the "new" first section?

Any help is greatly appreciated!

Cheryl
 


When I run, I loose no headers or footers.

What do you mean by the "new" first section?

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Really? Are your footers in the original section 2 linked to previous footers? When mine are linked, I lose the footers entirely.

Any help is greatly appreciated!

Cheryl
 
yes.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
Can you attach your file so I can try it?

Any help is greatly appreciated!

Cheryl
 


Did you check to see whether any of your paragraphs include the section break mark?

I cannot store from here.

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
I don't understand. How do I check that?

Any help is greatly appreciated!

Cheryl
 


ctr+* to toggle ON, and View Normal

Skip,
[glasses]Don't let the Diatribe...
talk you to death![tongue]

[glasses]Just traded in my old subtlety...
for a NUANCE![tongue]
 
The Sections should not be deleted UNLESS the last paragraph of a section INCLUDES the section mark. Then the Section is part of the paragraph, I believe. "

Negative. A section break is an independent object. It follows the last paragraph of the Section. It is the container (or technically, the Range that is...) holding the Paragraphs collection of the Section. The last paragraph never includes the Section break.

In other words, it is very possible to delete the last paragraph of a Section, and NOT delete the Section break.

With one MAJOR and crucial exception. The last paragraph of a document can be deleted, but the invisible (internal!)Section break that follows can NOT ever be deleted.

This is actually another reason why it is better to use Range, rather than Selection. It is very easy, when selecting, to include (or not include) things like paragraph marks and/or Section breaks. It is very easy to select up to the last paragraph of a Section and not realize the Section break is not included in the Selection.

Also another reason to always have Show/Hide set to ON.

Cheryl, please post the exact and full code you are using. Again, let's work with what you are actually using. There have been changes along the way. I need to see exactly what you are using.

"The headers are ok because I don't link to previous in the headers. But, I do need to link to previous in the footers. "

This certainly should be possible, but I need to know exactly what is there, and what you are doing.

Gerry
 
Ok, I'm in Normal View and I've toggle Ctrl+*. What do I look at now? How do I tell if the section break is part of the paragraph?

Any help is greatly appreciated!

Cheryl
 
This discussion - which I've only skimmed - seems to be getting out of hand. There is an issue when you have some - but not all - headers and footers linked to previous and you delete a section break. I have an example here, where it can happen with generated breaks inside Index fields. I will study this and come back.

Enjoy,
Tony

------------------------------------------------------------------------------------
We want to help you; help us to do it by reading this: Before you ask a question.

I'm working (slowly) on my own website
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top