BindToMoniker Gets Two Windows then Windows(wb.Name).Visible Fails

1

I'm trying to get an Excel file using BindToMoniker. For this question I'm starting with the no running instance of Excel (no workbooks open). It opens one Excel instance with two Excel windows shown. The one is entitled Excel and the other is titled with my workbook name. Neither of them show any open workbook.

Using Windows 10 64 bit with Excel 2016.

Dim wb As Excel.Workbook = System.Runtime.InteropServices.Marshal.BindToMoniker(PathName)

Dim xlApp As Object = wb.Parent
xlApp.Visible = True

'error here
xlApp.Windows(wb.Name).Visible = True

I should be able to unhide the window using xlApp.Windows(wb.Name).Visible = True instead I get a COMException: Invalid index. (Exception from HRESULT: 0x8002000B (DISP_E_BADINDEX)) Just to verify I click on either window and in the View tab click Unhide. It shows the exact wb.name. I click OK to unhide and it does so just fine with the second window disappearing. Yet if I try to do that in the code it throws an error. So why does my code throw an error and/or how can I fix it?

EDIT: This also throws an error "invalid index"

xlApp.Windows(1).Visible = True

EDIT 2:

This works

  1. I break the code at xlApp.Windows(1).Visible = True
  2. Click on the Excel window entitled with wb.name
  3. Execute the line xlApp.Windows(1).Visible = True

My workbook is unhidden and all is well. But if I click on the other window entitled "Excel" then it doesn't find any windows and my workbook stays hidden. I don't understand why it's opening two windows. That seems to be a problem.

I tried restarting my computer; no change.

So why am I getting two windows and why can't it find the hidden workbook to unhide it? What's the solution?

vb.net
excel
vba
asked on Stack Overflow Dec 5, 2016 by D_Bester • edited Jul 9, 2018 by Community

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0