Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are my excel workbooks invisible in Excel 2010?

I've been running an excel export function with .xlsx files. Occasionally when loading an excel file I use the System.Runtime.InteropServices.Marshal.BindToMoniker() method. When the file in question is not open, but exists, i get the following results:

Excel will start, but the workbook will not load, and neither will any worksheets. Excel appears blank.

When opening the file outside of the program that performs the exporting, it loads in exactly the same way. Excel is open, it says Microsoft Excel at the top of the window, but there are no visible sheets. Excel looks like there is nothing open at all.

Going into the options also suggests that there are no workbooks open. The file definitely has content though, as I can see when I open it in Notepad++ The document has enough detail for me to see that there are sheets in the workbook, but nothign shows up.

I'm not sure how to prevent this corruption from happening programmatically, or how to fix it. Any suggestions or thoughts would be appreciated!

To avoid the problem I never use BindToMoniker on closed Excel files (by checking whether the file is locked or not, if it's unlocked, I just use the open function)

Here is the code that runs when i get this problem. It will only happen if the file exists and is not open:

    Public Function OpenExcelFile( _
    ByRef pobjExcel As Excel.Application, _
    ByRef pwkbWorkBook As Excel.Workbook, _
    ByVal pstrFileName As String) As Boolean 

...

pwkbWorkBook = BindToMoniker(pstrFileName)
            pobjExcel = pwkbWorkBook.Parent
            pobjExcel.Visible = True 'Temp; I use this to see the state of the file
like image 526
meltdownmonk Avatar asked Dec 18 '25 09:12

meltdownmonk


2 Answers

Could it be that the workbook has been hidden? On the view tab, if Unhide is enabled you can reveal your hidden workbooks.

Also, if you think a workbook should be visible you can reveal it by updating the Windows visible property.

pobjExcel.Windows(workbookName).Visible = True
like image 146
Daniel Avatar answered Dec 21 '25 03:12

Daniel


Just add in

pobjExcel.Windows(1).Visible = True

right after where you set Excel to visible. I believe that you are making the application visible, but the workbook still is not (at least this is the problem I have had before). This should fix it.

like image 36
APrough Avatar answered Dec 21 '25 05:12

APrough



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!