【问题标题】:Check whether an Excel workbook has macros using C#使用 C# 检查 Excel 工作簿是否具有宏
【发布时间】:2017-05-29 00:29:19
【问题描述】:

我需要使用 C# 客户端浏览数百个 Excel 文件才能找到具有宏的文件。

我已尝试使用此处接受的答案中的代码:

Using Interop.Excel to check if Excel file contains VBA Macros

但是,此代码首先打开文件。即使在添加_appExcel.DisplayAlerts = false 后,许多文件仍会弹出 Windows 和 VBA 错误消息

它也很慢。有没有更好的方法来解决这个问题,最好不要先打开 Excel 文件?

我在 MSDN 上查找了 Excel 互操作文档:HasVBProject,但没有可以使用的示例。

【问题讨论】:

  • 是 .XLS 还是 .XLSM ?
  • 有 XLSM 和 XLSB 文件.cheers
  • 可惜有 XLSB。 XLSM 只是简单的 .zip 文件,您可以直接使用它...

标签: c# excel


【解决方案1】:

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

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

【讨论】:

  • 谢谢,我试试看。
【解决方案2】:

试试这个

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

【讨论】:

  • 嗨,谢谢,但这看起来像 VBA.im 使用 C# 客户端枚举文件。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-01
  • 2015-06-02
相关资源
最近更新 更多