Why ExcelApplication.Workbooks.Count is showing zero even when I have an Excel workbook open
Asked Answered
M

1

1

ExcelApplication.Workbooks.Count is showing count = 0 even when I have a document open.

ExcelApplication.Workbooks.Count shows count = 1 when the document opened is not in protected view. But when the opened document is PROTECTED VIEW the ExcelApplication.Workbooks.Count shows 0.

Why the workbook count is 0 in case of protected view excel?

Monomolecular answered 18/10, 2019 at 21:5 Comment(1)
What have you tried so far ? Paste your code please.Hecht
N
3

Because:

A workbook displayed in a Protected View window is not a member of the Workbooks collection.

If you need to get a handle on a protected workbook:

using (var protectedWorkbook = ExcelApplication.ProtectedViewWindows(1).Workbook)
{
    // do stuff 
}

A robust solution would account for the possible fault condition of multiple ProtectedViewWindows. The above code assumes there is one and only one.

Nicol answered 18/10, 2019 at 21:14 Comment(5)
This will result in COM object leak since you can't free the ProtectedViewWindow COM instance that you acquiredDromedary
Feel free to revise, I'm on mobile.Nicol
Thanks David, Link helped. Also observed that in PROTECTED VIEW if I try to set Application.Interactive property to false. It throws generic error Exception from HRESULT: 0x800A03EC I looked into the docs learn.microsoft.com/en-us/office/vba/api/… there's nothing explicitly mentioned that not allowed to set the interactive property when document is in PROTECTED VIEWMonomolecular
Is this presumption correct the it's not allowed to set Interactive property for protected view document?Monomolecular
Seems reasonable maybe but I'm not sure.Nicol

© 2022 - 2024 — McMap. All rights reserved.