Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

Deleting empty paragraphs and spaces at end of document

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Santa Claus - 06 Oct 2005 08:08 GMT
Hi, finally nearing the end of my project. Thanks to those who have helped.

I'm selecting and saving pages into a new document. Often there are five or
six carriage returns at the end of a page that is copied into a new
document. There might also be a number of spaces. Im wanting to delete
these.....so the last character in my new document is text or not null. I
dont want to tinker with the rest of the document.

I guess I want to go to the end of the document and use a look macro to test
if it is an empty paragraph or space. I can keep deletinhg until i reach
text.

I've search the web...found a few macros that are close, but they all seem
to edit the whole document. I only want to edit the end of the document.

Anyone able to point me in the right direction?

Thanks
Tony Jollans - 06 Oct 2005 09:18 GMT
No real need for code for this but you can record a macro if you want some.

Go to the end of the document (Ctrl+End)
Find and Replace (Ctrl+h)

Find [ ^13]{1,} - that's left (square) bracket, caret, one, three, right
(square) bracket, left brace, one, comma, right brace

Replace - leave blank

(Make sure there's no formatting applied - go to each of the Find and
Replace boxes and press No Formatting if it's not greyed out)

Select Up for Search direction
Check Use Wildcards

Hit Find Next
Hit Replace

--
Enjoy,
Tony

> Hi, finally nearing the end of my project. Thanks to those who have helped.
>
[quoted text clipped - 14 lines]
>
> Thanks
Helmut Weber - 06 Oct 2005 10:17 GMT
Hi Santa,

that's what I use, for a lot of different reasons,
which would take too much time to explain.
Just one point is to leave the last paragraph mark untouched,
as it is the end-of-doc mark at the same time.

Public Sub PurgeDocEnd()
If Len(ActiveDocument.Range) = 1 Then Exit Sub
Dim z As Long
Dim r As Range
With ActiveDocument
  z = .Range.End
  Set r = .Range(z - 2, z - 1)
  While r.Text = " " Or r.Text = Chr$(13)
  r.Delete
  z = .Range.End
  Set r = .Range(z - 2, z - 1)
Wend
End With
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Santa Claus - 06 Oct 2005 13:50 GMT
Hi Helmut, you are incredible....absolute legend!!!

Works perfectly for return's, but small problem with spaces .....seems
to get stuck but can't work out why.

For spaces, it seems to get caught behind the range being tested. I
could make an adjustment, but it has me affled as to why it is getting
stuck.

Do you get the same problem?

> Hi Santa,
>
[quoted text clipped - 22 lines]
> "red.sys" & chr(64) & "t-online.de"
> Word 2002, Windows 2000
Helmut Weber - 06 Oct 2005 14:39 GMT
Hi Santa,

the macro has been in use for quite some years,
and I never changed it. I wouldn't do it quite same way again.
It looks now odd to me in some details,
however,
my users never put in protected spaces [ctrl shift spacebar], chr(160),
which hopefully is the cause for the trouble.

If so, just add chr(160), like:

While r.Text = " " Or r.Text = Chr(160) Or r.Text = Chr$(13)

HTH

Greetings from Bavaria, Germany
Helmut Weber, MVP WordVBA
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
Helmut Weber - 06 Oct 2005 17:10 GMT
Hi Santa,

(best to use a non-proportional font to display the posting)

You wouldn't believe how complicated this can be,
but if you want to know the whole truth....

There are 5 kinds of spaces,
you might include tabs if you like.

ordinary         ascW(32)
non-breaking     ascW(160)
em-space         ascW(8195)
en-space         ascW(8194)
¼-em-space       ascW(8197)

What I first do is to delete spaces before paragraph marks.
I think they are of no use.
Why? Cause simple replacement may ruin formatting, held in
the paragraph marks.

Sub PurgeParagraphEnd()
Dim lChr As Long
Dim rPrg As Paragraph
Dim rTmp As Range
Dim sTmp As String
With ActiveDocument
  For Each rPrg In .Paragraphs
     Set rTmp = rPrg.Range
     rTmp.Collapse Direction:=wdCollapseEnd
     rTmp.End = rTmp.End - 1
     While IsSpace(rTmp.Previous.Text)
        rTmp.Previous.Delete
     Wend
  Next
  ' looks good, but leaves a space beore the last paragraph mark
  ' if there was more than one space before,
  ' needs special treatment. Don't know why.
  sTmp = .Characters.Last.Previous
  lChr = .Characters.Count
  If IsSpace(sTmp) Then
     .Characters(lChr - 1).Delete
  End If
End With
End Sub

Thanks for the flowers, by the way,
but not to touch the last paragraph mark
was nonsense. To preserve formatting,
quite the opposite is required.
Delete the last paragraph mark,
after having run PurgeParagraphEnd,
until there isn't a paragraph mark anymore,
preceding the last paragraph mark...

All together:

' ---
Public Function IsSpace(ByVal sTmp As String) As Boolean
Select Case AscW(sTmp)
  Case 32, 160, 8195, 8194, 8197: IsSpace = True
  Case Else: IsSpace = False
End Select
End Function
' ---
Sub PurgeDocEnd()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
PurgeParagraphEnd
While ActiveDocument.Characters.Last.Previous = Chr(13)
  ActiveDocument.Characters.Last.Delete
Wend
End Sub

Enjoy!

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Robert - 09 Oct 2005 15:38 GMT
>my users never put in protected spaces [ctrl shift spacebar], chr(160),
>which hopefully is the cause for the trouble.
>If so, just add chr(160), like:

>    While r.Text = " " Or r.Text = Chr(160) Or r.Text = Chr$(13)

Dear Helmut,
I am rather more interested in the first, the simpler, of your macros
which, when pasted into the VBE, worked fine initially.
But after introducing your amendment about Chr(160) Word just hangs.
The VBE indicates "running" and never identifies any fault for me to
trace.

It did occur to me (newbie that I am) that perhaps "Chr(160)" should be
"Chr$(160)", but amending the code made no difference. And now, Word
always hangs when this macro is called, even when in its initial,
unmodified form.
Of course, since Word has done several emergency shut-downs, I don't
know what state the Normal template is now in.

I'd rather like to keep your Sub as it would be very useful to me.  Can
you shed any light on what's happening, please?

Many thanks.

Robert
Helmut Weber - 09 Oct 2005 16:41 GMT
Hi Robert,

don't know what so say.

The original simple macro has been in use for years,
with more than 500,000 docs.

You may send me a problematic doc,
and I'll do whatever I can.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Helmut Weber - 10 Oct 2005 16:43 GMT
All revised and improved, hopefully.

There are quite some years between my first attempt and now.

;-)

Public Sub PurgeDocEnd2005()
Dim s As String
With ActiveDocument
If Len(.Range) = 1 Then Exit Sub
  s = .Characters.Last.Previous
  While s = " " Or s = Chr(13)
     .Characters.Last.Previous = ""
     s = .Characters.Last.Previous
  Wend
End With
End Sub

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Robert - 10 Oct 2005 18:02 GMT
Hello again Helmut,

This leaner version has a certain aesthetic beauty and, I am happy to
say, works perfectly on my machine.  I have even added the non-breaking
space, Chr$(160) , to the characters to be pruned from the end of the
doc and this works too.

Many thanks for your kind and helpful suggestions.

Robert
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.