PDA

View Full Version : Deleting any whitespace preceding last text and end of document



gmaxey
02-20-2013, 02:45 PM
Before doing other processing, I need to make sure that there is no whitespace between the last text character in a document and the end of document. It seems that somewher I saw an elegant solution for doing this, but I can't find it.

I've come up with this. Anybody no why it won't work to delete Chr(32) like the rest of them? If I put it with the rest and comment out the Case 32 section, the thing goes into a continuous loop.:

Sub StripTrailingWhiteSpace()
Dim bLoop As Boolean
On Error GoTo Err_Empty
Do
Select Case Asc(ActiveDocument.Characters.Last.Previous)
Case 9, 12, 13, 14, 160
ActiveDocument.Characters.Last.Previous.Delete
Case 32
ActiveDocument.Characters.Last.Previous.Select
Selection.Collapse wdCollapseStart
Selection.Delete
Case Else
Exit Do
End Select
Loop
Err_Empty:
Exit Sub
End Sub

fumei
02-20-2013, 04:03 PM
Odd.

Sub StripTrailingWhiteSpace()
Dim bLoop As Boolean
On Error GoTo Err_Empty
Do
Select Case Asc(ActiveDocument.Characters.Last.Previous)
Case 9, 12, 13, 14, 160, 32
ActiveDocument.Characters.Last.Previous.Delete
' Case 32
' ActiveDocument.Characters.Last.Previous.Select
' Selection.Collapse wdCollapseStart
' Selection.Delete
Case Else
Exit Do
End Select
Loop
Err_Empty:
Exit Sub
End Sub

works for me. All tabs (etc.) AND spaces before the very last paragraph mark are deleted. Sorry, I can not use the VBA tags as my keyboard will no longer give me either the closing slash, or the closing bracket.

It must be one of those new-fangled 2007-2010-2013 things. Har-de-har. I tried all sorts of combinations, and Chr(32) - well Asc(32) - are all deleted. Oh...and no loop either.

fumei
02-20-2013, 04:19 PM
Oooooops! It works FINE if there is no text that needs to be kept. A whole bunch of tabs, spaces, paragraph marks...the code deletes everything except the last paragraph mark. No loop.

If there is any text that is NOT part of the delete Case, yup...endless loooooop.

Hmmmmmm. Interesting.

fumei
02-20-2013, 04:26 PM
Hmmmm, even more weirdness. Stepped through, it works fine, no loop. As soon as it reaches the Case Else, it exits properly.

Reset the same combinations of tabs, spaces, paragraph marks...it does NOT loop. It exists properly.

Reset AGAIN - with the SAME combinations - it DOES endless loop.

Say what.

fumei
02-20-2013, 04:36 PM
Repeat hmmmm. With many tries, and stepping through, I can not get it to loop.

gmaxey
02-20-2013, 08:04 PM
Gerry,

Nothing to do with 2007/2010 or 2013. The problem is there in Word 2003 as well. But I know what the problem is now. It is Tools>Options>Edit and Smart Cut and Paste checked.

fumei
02-20-2013, 08:53 PM
Ah, glad you found it.

Yes, once I found I was getting the same result I knew it had nothing to do with versions...not that I seriously thought it was...I was just giving you a bit of a hard time.

macropod
02-21-2013, 11:20 PM
The following works for me, with or without 'smart cut & paste' active:
Sub StripTrailingWhiteSpace()
With ActiveDocument.Characters.Last
While .Previous Like "[" & Chr(9) & "-" & Chr(14) & Chr(32) & Chr(160) & "]"
.Previous.Text = vbNullString
Wend
End With
End Sub

gmaxey
02-22-2013, 08:02 PM
Paul,

Thanks. vbNullString was the elusive elegance that I had seen before. This will work for me:

Sub StripTrailingWhiteSpace()
With m_oScope.Characters.Last
While .Previous Like "[" & Chr(9) & Chr(12) & Chr(13) & Chr(14) & Chr(32) & Chr(160) & "]"
.Previous.Text = vbNullString
Wend
End With
lbl_Exit:
Exit Sub
End Sub

macropod
02-22-2013, 09:17 PM
So what's with:
lbl_Exit:
Exit Sub
You don't reference 'lbl_Exit' anywhere and, even if you did, the code would exit from there without the 'Exit Sub'.

fumei
02-23-2013, 12:45 AM
I think it is a leakage, from a standard setup used by Frosty (Jason).

gmaxey
02-23-2013, 05:25 AM
Paul,

It is personal style and yes copied from Frosty. While there is really nothing with it, I believe that without it the code would End and not Exit.

macropod
02-23-2013, 06:21 AM
I believe that without it the code would End and not Exit.
Maybe I'm missing something, but I can't see any benefit in that or what it achieves that 'End'ing normally doesn't.

gmaxey
02-23-2013, 12:00 PM
Paul,

I'm not saying that there is any benefit. It is just a personal style. I use PhraseExpress to create new macro procedures. My standard procedure is:

Sub ScracthMacro()

lbl_Exit:
Exit Sub
End Sub.

If I later add an error handler, it goes after the Exit Sub line. If I later add a GoTo lbl_Exit: then the label is already there. If not I leave it.