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
EDIT: I forgot that the original computer that was having this problem was running Windows 8, so it's not a Windows 10 issue. The only common denominator that I can find is that it only happened with Office 365. Most of the computers with the same specs as the first one didn't exhibit this issue.