#1
|
|||
|
|||
Range won't delete on some computers
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 |
#2
|
||||
|
||||
There have been some issues lately with MS updates to Office 365. nevertheless, that's a horribly inefficient way to go about the task! Try:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[ ]{1,}^13" .Replacement.Text = "^p" .Forward = True .Format = False .Wrap = wdFindContinue .MatchWildcards = True .Execute Replace:=wdReplaceAll End With End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#3
|
|||
|
|||
Thanks.
What I posted is part of a method which performs many tasks on the document, and at the time, it was easy to think about adding the code to do this into that method. Looking at it now, it does make more sense to do the find/replace separately. Sometimes when you're in one frame of mind, it doesn't occur to do something a different way. Plus, I don't do so much coding with VBA in word, so I may often end up using a method that isn't as efficient as possible. I had checked the Office build versions on each of the computers, and I believe that they were all the same, both on computers that worked and those that didn't, so I wasn't sure if it was a bug in that version or not. Thanks again. |
Thread Tools | |
Display Modes | |
|
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 |