Word hands on selection.MoveEndWhile with comments

A

Alex

Happened with Word 2003.

Create the following Word document

AAA<p>
<p>
<p>

where <p> is the paragraph marker.

Select the whole document and do:

selection.MoveEndWhile Chr(13), wdBackward

Works fine.

Now, select the 1st line (the AAA) and add a comment to it.

Again, select the whole document and do:

selection.MoveEndWhile Chr(13), wdBackward

Word hangs.


How can I overcome that?
 
D

Doug Robbins - Word MVP

Avoid using the Selection. What is it that you are actually trying to do?

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

Happened with Word 2003.

Create the following Word document

AAA<p>
<p>
<p>

where <p> is the paragraph marker.

Select the whole document and do:

selection.MoveEndWhile Chr(13), wdBackward

Works fine.

Now, select the 1st line (the AAA) and add a comment to it.

Again, select the whole document and do:

selection.MoveEndWhile Chr(13), wdBackward

Word hangs.


How can I overcome that?
 
A

Alex

Doug Robbins - Word MVP said:
Avoid using the Selection.

Pardon?

I was using the selection to illustrate, the actual code uses a range.
What is it that you are actually trying to do?

I am trying to strip empty paragraphs from the beginning and end of a range (using MoveEndWhile and MoveStartWhile).

This is a part of a Word add-in that processes and transforms marked parts of a user-created document into a format that one of our applications can digest.

My current "solution" is something like (translated to VBA):

Sub trim(rng As Range)
rng.MoveStartWhile vbCr, wdForward
If rng.Comments.Count > 0 Then
While rng.Characters.Last.Text = vbCr
rng.MoveEnd wdCharacter, -1
Wend
Else
rng.MoveEndWhile vbCr, wdBackward
End If
End Sub

but it looks ugly.
 
C

Cindy M -WordMVP-

Hi Alex,

Well, FWIW I'm able to duplicate the problem you describe. From what I can see in the VBA-Editor, it's not so much that Word crashes, but that the macro goes
into an unending loop. I'm guessing it gets into a comment range and can't get out... So your solution, as ugly as it seems, is probably as good as anything
else.
I am trying to strip empty paragraphs from the beginning and end of a range (using MoveEndWhile and MoveStartWhile).

This is a part of a Word add-in that processes and transforms marked parts of a user-created document into a format that one of our applications can digest.

My current "solution" is something like (translated to VBA):

Sub trim(rng As Range)
rng.MoveStartWhile vbCr, wdForward
If rng.Comments.Count > 0 Then
While rng.Characters.Last.Text = vbCr
rng.MoveEnd wdCharacter, -1
Wend
Else
rng.MoveEndWhile vbCr, wdBackward
End If
End Sub

but it looks ugly.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply in the newsgroup and not by e-mail :)
 
T

Tony Jollans

When I first saw this, I tried it quickly and where there should have been a
chr(5) for the comment, I found a zero length character (range); I vowed to
come back and look later. Then sometime while I was out, my computer crashed
(USB problem unrelated to Word). Since restarting and restarting Word I am
completely unable to reproduce it. In normal circumstances I would put it
down to some sort of corruption but if you get it repeatedly there must be
circumstances which cause it - at the moment I don't know what they may be.

--
Enjoy,
Tony

Cindy M -WordMVP- said:
Hi Alex,

Well, FWIW I'm able to duplicate the problem you describe. From what I can
see in the VBA-Editor, it's not so much that Word crashes, but that the
macro goes
into an unending loop. I'm guessing it gets into a comment range and can't
get out... So your solution, as ugly as it seems, is probably as good as
anything
parts of a user-created document into a format that one of our applications
can digest.
Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister


This reply is posted in the Newsgroup; please post any follow question or
reply in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top