Word delete section breaks

  • Thread starter david epsom dot com dot au
  • Start date
D

david epsom dot com dot au

I would like to delete any section breaks at the end of the document, if
there are any. Assuming that I have an open document object, how would I go
about this? This is what I have so far:

6030 Set objTarget = GetObject(str_CombinedFileName)

6040 Set objApp = objTarget.Application

6041 objApp.Visible = True

6042 objApp.Activate
 
J

Jezebel

Try this:

Dim pRange As Word.Range

Set objTarget = GetObject(str_CombinedFileName)

With objTarget
Do

'Get a reference to the final section
Set pRange = .Sections(.Sections.Count).Range

'If not empty, then we're done
If Len(Trim(pRange)) > 1 Then
Exit Do
End If

'Delete it and go round again
.Range(pRange.Start - 1, pRange.End).Delete
Loop
End With

MsgBox "DONE"

This deletes any final sections that contain a maximum of one character
(assumed to be a paragraph mark) plus any number of leading and trailing
spaces.
 
P

Peter Hewett

Hi david epsom dot com dot au

Do you really all Section breaks in the document or just Section breaks at the end of the
document. Also, if you mean Section breaks at the end of the document are they
immediately before the documents final paragraph mark?

Cheers - Peter


I would like to delete any section breaks at the end of the document, if
there are any. Assuming that I have an open document object, how would I go
about this? This is what I have so far:

6030 Set objTarget = GetObject(str_CombinedFileName)

6040 Set objApp = objTarget.Application

6041 objApp.Visible = True

6042 objApp.Activate

HTH + Cheers - Peter
 
D

david epsom dot com dot au

All section breaks at the end of the document.
Is there a final paragraph mark after the last
hard section break? Can you delete it? Can
you select it? Is it in any range?

(david)
 
P

Peter Hewett

Hi david epsom dot com dot au

I'm still not sure that I understand *exactly* what you want to do. Or maybe more
accurately I'm not sure how you end up with a document with multiple section breaks at the
end!

Anyway, if your document has one or more section breaks immediately before the final
paragraph mark of the document this code will delete them:

Public Sub DeleteSectionsAtEOD()
Dim rngEOD As Word.Range

' Move the start of the range object over any consecutive
' contiguous Section breaks at the end of the document

' Setup collapsed range at the end of the document
Set rngEOD = ActiveDocument.Content
rngEOD.Collapse wdCollapseEnd

' vbFormFeed is the character Word uses for all types of Section breaks
' MoveStartWhile with a count of wdBackward returns a negative number
If rngEOD.MoveStartWhile(vbFormFeed, wdBackward) < 0 Then

' Delete the section break(s)
rngEOD.Delete
End If
End Sub

HTH + Cheers - Peter
 
D

david epsom dot com dot au

In WordBasic, I moved to the end of the document, then deleted
the last character -- which took out 1 trailing section break.
Using the Word application object model, the code has become so
complex that I'm never sure what I've got at the end of the document,
so I'm quite happy to have more explicit code with testing.

It's interesting to compare the two examples:

last section:
.Sections.Last.Range
.Sections(.Sections.Count).Range

delete:
selection.Delete
.Range(pRange.Start - 1, pRange.End).Delete

My preference is to go with Range rather than Select, even
though this makes the code less intuitive, because I have
an idea that Select implies use of the GUI, and might work
differently if the application was hidden and not activated.

..Last. /looks/ better than .Section.Count Jezebel, is this just
coding practice or am I missing something?

Question: is a Paragraph Mark the same as a lf/cr? or is there
a different test for that?


(david)


Jezebel said:
Hardly a problem, just a matter of your definition of 'blank'. Mine was a
maximum of one character (presumptively a paragraph mark) plus any number of
spaces. But you could be a lot more sophisticated with your test, eg

If pRange.Text Like "[ " & vbcr & vblf & "]*" then





DA said:
Hi Jezebel,

Problem with this is that you're taking for granted that
the final section has a single character. What about if
your blank sections have 2 or more carriage returns -
linefeeds - or both?

I couldn't get my brain into gear this morning, got any
other ideas on how we can cover the above (apart from
that rather ugly looking if statement I posted earlier)?

Dennis.
 

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