![]() |
|
#1
|
|||
|
|||
|
I have a function which, among other things, removes extra spaces from the end of all paragraphs. It works properly, except on some machines it goes into an endless loop. I found out that it's not deleting a range.
My code loops WHILE the last character is a space. If there are multiple spaces at the end of a paragraph, it happily deletes all of them except for the last one it encounters, and then it continues to loop since the last character will always be a space. I partially solved the problem by counting the # of spaces at the end, then deleting that range; it doesn't delete the last space, but it doesn't run into an endless loop. The computers that seem to have the problem I believe all are running Windows 10 and Office 365. I think there are some which have the same setup but do work, but I'm not positive. Here's the code that's causing the problem Code:
For Each oPara In oDoc.Paragraphs
Set oRng = oPara.Range
Set orng2 = oPara.Range
Call oRng.MoveEnd(Unit:=wdCharacter, Count:=-1)
While (Right(oRng.Text, 1) = " ")
orng2.Start = oRng.Start
orng2.End = oRng.End
Call orng2.Collapse(wdCollapseEnd)
Call orng2.MoveStart(Unit:=wdCharacter, Count:=-1)
If (Len(orng2.Text) > 0) Then
orng2.delete ' This FAILS on some machines!
End If
Wend
Next oPara
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Name a Range in a Word Document and then copy that range to the end of the doc w button click
|
DanNatCorning | Word VBA | 1 | 04-29-2016 10:47 PM |
| Find and Delete Rows based on a range | damaniam | Excel Programming | 2 | 03-12-2014 06:06 AM |
Delete Range After Bookmark?
|
VBA_Elfe | Word VBA | 3 | 04-05-2013 05:05 AM |
Word VBA: Cannot Edit Range (Delete characters except the first in a table cell)
|
tinfanide | Word VBA | 3 | 04-27-2012 09:48 AM |
| Can't delete range - error 5904 | expatriate | Word VBA | 1 | 06-03-2011 12:12 AM |