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

回眸只為那壹抹淺笑 提交于 2020-02-05 08:24:47

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/58458188/why-excelapplication-workbooks-count-is-showing-zero-even-when-i-have-an-excel-w

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!