Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 11-12-2017, 12:14 PM
gunner359 gunner359 is offline Word hangs when MoveUntil is used and Comment present Windows 10 Word hangs when MoveUntil is used and Comment present Office 2016
Novice
Word hangs when MoveUntil is used and Comment present
 
Join Date: Oct 2017
Posts: 6
gunner359 is on a distinguished road
Question 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
Reply With Quote
  #2  
Old 11-12-2017, 01:37 PM
gunner359 gunner359 is offline Word hangs when MoveUntil is used and Comment present Windows 10 Word hangs when MoveUntil is used and Comment present Office 2016
Novice
Word hangs when MoveUntil is used and Comment present
 
Join Date: Oct 2017
Posts: 6
gunner359 is on a distinguished road
Post

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!
Reply With Quote
  #3  
Old 11-13-2017, 08:46 AM
gunner359 gunner359 is offline Word hangs when MoveUntil is used and Comment present Windows 10 Word hangs when MoveUntil is used and Comment present Office 2016
Novice
Word hangs when MoveUntil is used and Comment present
 
Join Date: Oct 2017
Posts: 6
gunner359 is on a distinguished road
Default

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.
Reply With Quote
  #4  
Old 11-15-2017, 10:08 PM
gunner359 gunner359 is offline Word hangs when MoveUntil is used and Comment present Windows 10 Word hangs when MoveUntil is used and Comment present Office 2016
Novice
Word hangs when MoveUntil is used and Comment present
 
Join Date: Oct 2017
Posts: 6
gunner359 is on a distinguished road
Default 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.
Reply With Quote
  #5  
Old 11-15-2017, 11:17 PM
macropod's Avatar
macropod macropod is offline Word hangs when MoveUntil is used and Comment present Windows 7 64bit Word hangs when MoveUntil is used and Comment present Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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
Replace 'Emphasis' with your Style name.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #6  
Old 11-16-2017, 02:36 AM
slaycock slaycock is offline Word hangs when MoveUntil is used and Comment present Windows 7 64bit Word hangs when MoveUntil is used and Comment present Office 2016
Expert
 
Join Date: Sep 2013
Posts: 256
slaycock is on a distinguished road
Default

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.
Reply With Quote
  #7  
Old 11-19-2017, 08:52 PM
gunner359 gunner359 is offline Word hangs when MoveUntil is used and Comment present Windows 10 Word hangs when MoveUntil is used and Comment present Office 2016
Novice
Word hangs when MoveUntil is used and Comment present
 
Join Date: Oct 2017
Posts: 6
gunner359 is on a distinguished road
Default

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.
Reply With Quote
  #8  
Old 11-19-2017, 08:56 PM
gunner359 gunner359 is offline Word hangs when MoveUntil is used and Comment present Windows 10 Word hangs when MoveUntil is used and Comment present Office 2016
Novice
Word hangs when MoveUntil is used and Comment present
 
Join Date: Oct 2017
Posts: 6
gunner359 is on a distinguished road
Default

Thanks, Slaycock. I always use Option Explicit, but did not know about the problems that can occur if you don't. Lesson learned. Thanks.
Reply With Quote
Reply

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
Word hangs when MoveUntil is used and Comment present How to make Office (Word) 2010 stick to the present task pintree3 Office 5 04-14-2014 02:27 PM
Word hangs when MoveUntil is used and Comment present VBA equivalent to selecting present word KevinJ Word VBA 2 11-05-2012 01:27 PM
Word hangs when MoveUntil is used and Comment present 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

Other Forums: Access Forums

All times are GMT -7. The time now is 03:45 PM.


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