Microsoft Office Forums Range won't delete on some computers

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-21-2018, 01:06 PM
Cosmo Cosmo is offline Range won't delete on some computers Windows Vista Range won't delete on some computers Office 2007
Competent Performer
Range won't delete on some computers
 
Join Date: Mar 2012
Posts: 220
Cosmo is on a distinguished road
Default 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
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.
Reply With Quote
  #2  
Old 09-21-2018, 03:19 PM
macropod's Avatar
macropod macropod is offline Range won't delete on some computers Windows 7 64bit Range won't delete on some computers Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 19,525
macropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to beholdmacropod is a splendid one to behold
Default

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
[MS MVP - Word]
Reply With Quote
  #3  
Old 09-24-2018, 05:57 AM
Cosmo Cosmo is offline Range won't delete on some computers Windows Vista Range won't delete on some computers Office 2007
Competent Performer
Range won't delete on some computers
 
Join Date: Mar 2012
Posts: 220
Cosmo is on a distinguished road
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Range won't delete on some computers 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
Range won't delete on some computers Delete Range After Bookmark? VBA_Elfe Word VBA 3 04-05-2013 05:05 AM
Range won't delete on some computers 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


All times are GMT -7. The time now is 12:21 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2019, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2019 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft