简体   繁体   English

使用C#检查Excel工作簿是否具有宏

[英]Check whether an Excel workbook has macros using C#

I need to go through hundreds of Excel files to find those that have macros, using a C# client. 我需要使用C#客户端浏览数百个Excel文件,以查找具有宏的文件。

I have tried using the code in the accepted answer here: 我尝试在此处接受的答案中使用代码:

Using Interop.Excel to check if Excel file contains VBA Macros 使用Interop.Excel检查Excel文件是否包含VBA宏

However, this code opens the file first. 但是,此代码将首先打开文件。 Many of the files are opening pop up Windows and VBA error messages even after adding _appExcel.DisplayAlerts = false 即使添加_appExcel.DisplayAlerts = false许多文件_appExcel.DisplayAlerts = false打开Windows和VBA弹出错误消息

Its also very slow. 它也很慢。 Is there a better way to go about this, ideally without opening the Excel file first? 有没有更好的方法来进行此操作,理想情况下无需先打开Excel文件?

I have looked for documentation on MSDN for Excel interop here: HasVBProject but there are no examples I can work with. 我在这里寻找了有关Excel互操作的MSDN文档: HasVBProject,但是没有可以使用的示例。

try this 尝试这个

f = Dir("*.xls*")
While f <> ""
On Error Resume Next
    Set wb = Workbooks.Open(f)
    If wb.HasVBProject Then
       Debug.Print wb.Name & " has macro"
    Else
        Debug.Print wb.Name & "does not have macro"
    End If
    wb.Close
    f = Dir
Wend

如果工作簿事件是问题,那么您可以通过AutomationSecurity属性使用Interop.Excel命名空间禁用所有宏:

MyExcel.Application.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM