#1
|
|||
|
|||
Word hangs when MoveUntil is used and Comment present
Hi All,
Summary: MoveUntil VBA command hangs Word 2016 when a Comment is within range of a negative or wdBackward Count parameter. MoveEndUntil and MoveStartUntil also fail. Further testing indicates that setting Count positive or wdForward works. See details below. First time poster. Platform is Windows 10 and Office 2016. My customer has a slew of Word documents that they pass around and edit. They wanted a macro that allows them to insert text strings into the document that are highlighted white text on a green background and hidden. Easy enough. I have the user type a + sign followed by the string. The macro is invoked by the keyboard shortcut Alt-m; the string from the insertion point (IP) back to the + sign is given a style I created and the + sign removed. All is working great. In my macro, I use the VBA commands: Dim i As Long ... i = Selection.MoveUntil(Cset:="+", Count:=wdBackward) to find the + sign and the length of the string (-i-1). Works very well. When the + sign is missing, the MoveUntil command returns 0 and the macro is conditioned to handle this case. But here is the rub. When the user inserts a Comment in the document and types Alt-m prior to the Comment, the MoveUntil command returns 0. Good. When the IP is after the Comment and Alt-m is typed, Word VBA stops on the VBA command i = Selection.MoveUntil(Cset:="+", Count:=wdBackward) and the cursor spins looking like a wheel. No error, no nothing. When I change the Count to -100: i = Selection.MoveUntil(Cset:="+", Count:=-100) all works well when Alt-m is typed and the IP is > 100 characters after the Comment. When the IP is between the Comment and the 100th character after the Comment, the MoveUntil command hangs and Word hangs (Not Responding). Word has to be restarted. Sounds like MoveUntil is confused when it runs into a Comment. Any ideas? I'm trying not to use Find as the setup a little tedious and Find would take much longer that MoveUntil (I think). Any help would be appreciated. Thank you. Last edited by gunner359; 11-12-2017 at 09:28 PM. Reason: Further testing |
#2
|
|||
|
|||
Hiding comments by saving markup and view status, then restoring markup and view status as follows:
Dim intRevisionsMarkup As Integer Dim intRevisionsView As Integer ... intRevisionsMarkup = ActiveWindow.View.RevisionsFilter.Markup intRevisionsView = ActiveWindow.View.RevisionsFilter.View ActiveWindow.View.RevisionsFilter.Markup = wdRevisionsMarkupNone ... i = Selection.MoveUntil(Cset:="+", Count:=-100) ... ActiveWindow.View.RevisionsFilter.Markup = intRevisionsMarkup ActiveWindow.View.RevisionsFilter.View = intRevisionsView does not help. Word still hangs! |
#3
|
|||
|
|||
Instead of using Selection as the required expression, use a range object, i.e.,
Dim rngDoc as Range ... Set rngDoc = ActiveDocument.Words(1) i = rngDoc.MoveUntil(Cset:="+", Count:=wdBackward) Seems like ActiveDocument.Words(1) would eliminate Comments in its Range. |
#4
|
|||
|
|||
How to reproduce MoveUntil problem
This looks like a definite bug in the VBA command MoveUntil.
Could anyone corroborate for me please? How to reproduce the problem. Create a new Word document and type about 10 words of text. Open VBE and type in the following module code: Sub Macro1() Dim i As Long i = Selection.MoveUntil(Cset:="+", Count:=wdForeward) MsgBox i & " character positions were moved" End Sub Put your insertion point at the end of the text. Run Macro1. Message box displays "0 character positions moved" Hover the cursor over one of the words and right click New Comment. Set focus in the document and leave cursor at the end of the text. Run Macro1. Word hangs with spinning cursor. Change Count to wdForward and perform steps in the opposite direction and the MoveUntil works fine. |
#5
|
||||
|
||||
Try:
Code:
Sub Demo() Dim Rng As Range: Set Rng = Selection.Range With Selection With .Find .ClearFormatting .Replacement.ClearFormatting .Replacement.Text = "" .Format = False .Forward = False .Wrap = wdFindContinue .MatchWildcards = False .Text = "+" .Execute End With If .Find.Found Then Rng.Start = .Start Rng.Characters.First.Delete Rng.Style = "Emphasis" End If End With End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
You need to be careful with the word 'forward' and 'backward'. With Moveuntil 'Forward' means towards the end of the document, 'Backwards' vice versa.
Hence if you place the cursor at the end of the text and search forwards it will move 0 characters because it cannot go any further 'Forward'. The other point to consider is if you were using 'Option explicit' or not. If you weren't then wdForeward would be created as a new word variable of type variant and assigned the value 0 rather than being treated as typographical error (. Hence your use of wdForeward is a request to move forward 0 characters which is exactly the action you got. Additionally, if you'd checked the token wdForeward in the object browser you would have found it didn't exist. Lesson learned 1. Always always always use option explicit 2. Read the VBA word object model help pages very very carefully. |
#7
|
|||
|
|||
Very nice, Paul. Works very well. I had worked out a solution using InstrRev, but I like your solution better. It seems that any commands or loops that I write that move the insertion point character by character causes the cursor to spin for a short while. Thanks so much for the code.
|
#8
|
|||
|
|||
Thanks, Slaycock. I always use Option Explicit, but did not know about the problems that can occur if you don't. Lesson learned. Thanks.
|
Tags |
comment, moveuntil, word hangs |
Thread Tools | |
Display Modes | |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Word hangs completely when editing punctuation | cydevil | Word | 4 | 11-24-2016 04:16 PM |
How to make Office (Word) 2010 stick to the present task | pintree3 | Office | 5 | 04-14-2014 02:27 PM |
VBA equivalent to selecting present word | KevinJ | Word VBA | 2 | 11-05-2012 01:27 PM |
How to insert a (balloon) comment and how to disable comment feature in Word2007? | pstein | Word | 2 | 03-31-2012 05:31 AM |
Hyperlink Look In Hangs Word 2010 | drumms | Word | 0 | 02-24-2012 11:35 AM |