Ok to call Application.get_Selection()?

D

David Thielen

Hi;

We have a problem where we get a selection changed event when a user double
clicks on a mis-spelled word in Word 2007 and we then call get_Selection().
We get an exception that says COMException (0x800A1759): Cannot create a
Selection object when this dialog is active.

How can I determine if it's ok to call get_Selection?

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

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

Jialiang Ge [MSFT]

Hello Dave,
We have a problem where we get a selection changed event when a user
double clicks on a mis-spelled word in Word 2007 and we then call
get_Selection(). We get an exception that says COMException (0x800A1759)
: Cannot create a Selection object when this dialog is active.

I am not sure if I understand the steps correctly. In order to reproduce
the issue on my side, I created an Office COM add-in (C#) in VS2005. In the
Addin's OnConnection event handler, I added the line:
((Word.ApplicationClass)application).WindowSelectionChange += new
Microsoft.Office.Interop.Word.ApplicationEvents4_WindowSelectionChangeEventH
andler(Connect_WindowSelectionChange);
to register Word 2007 Selection changed event. Then in the event handler, I
printed the selected text and the exception message if any:

void Connect_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection
Sel)
{
try
{
if (!string.IsNullOrEmpty(applicationObject.Selection.Text))
Debug.Print(applicationObject.Selection.Text);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}

After the add-in was built and installed into Word 2007, I debugged it
inside VS2005. I double clicked some mis-spelled word in Word 2007, but
none exception message is popped up. I also tried double clicking texts in
some Field codes, and double clicking when some Office dialogs (e.g Find
dialog) is showing, unfortunately, I still cannot reproduce the error on my
side.

Would you help to check if my understanding of the steps is correct? Is
there any special Office dialog/menu showing when the error occurs on your
side? I have queried the error "Cannot create a Selection object when this
dialog is active" in our internal database, but did not find relevant issue
report. Therefore, I need more information about the problem. Would you
paste some codes or send me a small and reproducible sample so that I can
have a clearer picture of the issue?

Thanks
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
For MSDN subscribers whose posts are left unanswered, please check this
document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Dave,

Thank you for the information. I am still not clear about several points:

It was mentioned in the first message that, the exception is thrown when
users double click on a mis-spelled word. But in your last reply, I noticed
that users move their cursor on a mis-spelled word ("not selected") and do
spell check, then the error occurs.

Can I understand that the error occurs *EVERY TIME* when
1. users double click on a mis-spelled word
- or -
2. users move their cursor on a mis-spelled word ("not selected") and do
spell check?

When it is said that "users move their cursor on a mis-spelled word ("not
selected") and do spell check", can I understand that the cursor is inside
the word, like "Newsg|roup" ( | means cursor)?

Please help me confirm the informaiton above. I am trying more tests to
reproduce the issue, and will get back to you as soon as possible.

Thanks
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
D

David Thielen

To reproduce please do the following:

1) Open MS Word 2007
2) Make sure spell check is on, also spell check as you type option.
3) Start typing
4) Type a word with incorrect spelling.
5) When red underline appears, right click on the word and click
"Spelling....." to launch spell check window.
6) When the window launches the error will occur.


--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

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

Jialiang Ge [MSFT]

Hello Dave,

Thank you very much for your effort to figure out the detailed steps. I
have reproduced the issue on my side. Based on my test, it occurs in all
versions of the Office system (2007, 2003, XP, etc).

I checked the stack of Office when the exception is thrown:
winword!HrGetActiveSelection returns this error (eidVBANoSelDlg), and gives
an error message like:
"Error 5977: Cannot create a [Selection] object when this dialog is active"

Where name in brackets is the object being created. In this case a
Selection object is created for the current cursor when we call
Application.Selection.
It is thrown because the focus is in the dialog and not any specific
document.

To work-around the problem, we can call

applicationObject.ActiveWindow.Selection

rather than

applicationObject.Selection.

The difference between the two is that

applicationObject.ActiveWindow.Selection returns the selection of the
currently active window. The window may not be the document window. It can
be any Office dialog window that currently get focus. In contrast,
applicationObject.Selection only returns the selection of the document
window. If none document window is active, it throws the exception: "Cannot
create a [***] object when this dialog is active.

Hope it helps.
If you have any other concern or need anything else, please feel free to
let me know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Jialiang Ge [MSFT]

Hello Dave,

Another workaround is to use the Selection object passed in as the
SelectionChange event parameter instead:

void Connect_WindowSelectionChange(Microsoft.Office.Interop.Word.Selection
Sel)

If you have any other concerns or questions, please feel free to let us
know.

Regards,
Jialiang Ge ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
When responding to posts, please "Reply to Group" via your newsreader
so that others may learn and benefit from your issue.
=================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

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