Caching the selection in Word

D

David Thielen

Hi;

When the ribbon is updated I have a bunch of different methods that
get called, one for each item in the ribbon. For most of them, their
enabled/disabled is dependent on what is presently selected in the
document.

So I implemented some code where it saves the Start & End of the
Selection Range. Then if I am asked again what is selected, if the
Start/End is the same and it is under 1/2 second since the last query
- just return the previous query.

Doing this is critical because my code, if it is asked what is
selected, needs to go through all fields, ContentControls,
InlineShapes, & Shapes as well as the selected text. The cursor
bounces between hourglass and arrow about 20 times in 2 seconds.

Ok, so all great until... the selected item in the document is a
bitmap and it is changed between InlineShape and Shape (either way).
The Start/End haven't changed, but it's now a different object and the
old object is no longer in the document.

Even worse, the old object is still good and if it is selected, it is
placed back in the document.

So, any suggestions on knowing when the selection has changed?

thanks - dave

david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
S

Scott McPhillips [MVP]

David Thielen said:
So, any suggestions on knowing when the selection has changed?

thanks - dave


There's an event for that: ApplicationEvents2::WindowSelectionChange
 
D

David Thielen

Help! Any suggestions? This is a significant issue for us.

thanks - dave


If you change a bitmap from inline to positioned and/or back to inline
that event does not fire. I assume it does not because the Selection
Start/End does not change.

Anything else I can watch?

thanks - dave





david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
T

Tony Jollans

I've played with this a little and the case you describe is rather wierd,
and I think there is a bug in Word.

The fact that the start and end of the Selection remain unchanged when a
picture is changed from inline to wrapped seems wrong to me. It actually
seems to be the (newly-created) anchor that is wrong rather than the
Selection itself, but that is probably academic.

However, I note that the length of the Selection *does* change, so, maybe,
if as well as saving Start and End, if you saved, and later compared
against, Len(ActiveDocument.Range(Selection.Start,Selection.End), it would
serve you better.
 
D

David Thielen

There's no Len() in C# and when I get the length of the Text, that
comes out as 1 for both cases. Any other suggestions?

thanks - dave


I've played with this a little and the case you describe is rather wierd,
and I think there is a bug in Word.

The fact that the start and end of the Selection remain unchanged when a
picture is changed from inline to wrapped seems wrong to me. It actually
seems to be the (newly-created) anchor that is wrong rather than the
Selection itself, but that is probably academic.

However, I note that the length of the Selection *does* change, so, maybe,
if as well as saving Start and End, if you saved, and later compared
against, Len(ActiveDocument.Range(Selection.Start,Selection.End), it would
serve you better.


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 
T

Tony Jollans

Interesting. If, after having changed a selected image from inline to
wrapped, the length of the selection is 1 (VBA returns 0) what is the
(single character of) text? Prior to the change it should be "/" - the
placeholder character for the image.
 
D

David Thielen

Oops - ran it again because you are rarely wrong - and you were
correct. Get a length of 1 for inline and a null Text for positioned.

thanks - dave


Interesting. If, after having changed a selected image from inline to
wrapped, the length of the selection is 1 (VBA returns 0) what is the
(single character of) text? Prior to the change it should be "/" - the
placeholder character for the image.


david@[email protected]
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm
 

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