Excel-DNA / Excel-DNA/ExcelDna

GetWindowHandle15 throws InvalidComObjectException

Open
#208 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
1.5k
Forks
292
Avg merge
20d 12h
Merged PRs (30d)
1

Description

In a ribbon handler, I would like to set the owner of my dialog to the Excel window handle. I think I'm doing this right, here's my entire helper function to show a dialog (F#):

```
let showDialog (window: Window) =
let helper = WindowInteropHelper window
helper.Owner <- ExcelDnaUtil.WindowHandle
window.ShowInTaskbar <- false
window.ShowDialog().Value
```

This works fine on my development machine (Windows 7 + Office 2010), but on at least one customer machine, accessing the WindowHandle property throws an exception:

> System.Runtime.InteropServices.InvalidComObjectException: COM object that has been separated from its underlying RCW cannot be used.
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at ExcelDna.Integration.ExcelDnaUtil.GetWindowHandle15()
at Utils.showDialog(Window window)

Looking at the code for GetWindowHandle15(), I see it's a bit tricky to correctly get the Excel window handle (thanks, Microsoft), and if it fails, it throws InvalidOperationException - presumably in that case it really is an invalid operation. InvalidComObjectException here seems to be completely unexpected.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.