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

查看:343
本文介绍了使用Interop.Excel检查Excel文件是否包含VBA宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我必须检查Excel文档是否包含vb-macros。所以我写了以下方法来检查excel文档:

In my application I have to check if a excel-document contains vb-macros. So I've written the following method to check the excel-document:

internal static bool ExcelContainsMacros(string pathToExcelFile)
{
    bool hasMacros = true;
    Microsoft.Office.Interop.Excel._Application excelApplication = new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook workbooks = null;
    try
    {       
        object isReadonly = true;
        workbooks = excelApplication.Workbooks.Open(
            pathToExcelFile, missing, isReadonly, missing, missing, missing,
            missing, missing, missing, missing, missing, missing,
            missing, missing, missing);
        hasMacros = workbooks.HasVBProject;     
        LogHasMacros(hasMacros);
    }
    catch (Exception exception)
    {
        LogError(exception);
    }
    finally
    {
        excelApplication.Workbooks.Close();
        excelApplication.Quit();
    }
    return hasMacros;
}

使用一些excel文件,我从excel中收到一条运行时错误的消息91。

With some excel-files I get a message from excel with a runtime-error 91.

91:对象变量或未设置块变量

I调试它,只是意识到消息出现在调用 excelApplication.Workbooks.Close(); 。如果我删除这行代码,但是在 excelApplication.Quit(); 的调用中出现相同的excel消息。

I debugged it and just realized that the message appears at the call to excelApplication.Workbooks.Close();. If I remove this line of code but the same excel-message appears at the call of excelApplication.Quit();.


推荐答案

Pertinent对于您的任务,您可以参考以下精简的代码片段,它使用.NET / C#, Microsoft.Office.Interop.Excel 对象库和 Runtime.InteropServices.Marshal 对象:

Pertinent to your task, you may refer to the following refined code snippet, which utilizes .NET/C#, Microsoft.Office.Interop.Excel object library and Runtime.InteropServices.Marshal object:

internal static bool? ExcelContainsMacros(string pathToExcelFile)
{
    bool? _hasMacro = null;
    Microsoft.Office.Interop.Excel._Application _appExcel = 
        new Microsoft.Office.Interop.Excel.Application();
    Microsoft.Office.Interop.Excel.Workbook _workbook = null;
    try
    {
        _workbook = _appExcel.Workbooks.Open(pathToExcelFile, Type.Missing, true);
        _hasMacro = _workbook.HasVBProject;

        // close Excel workbook and quit Excel app
        _workbook.Close(false, Type.Missing, Type.Missing);
        _appExcel.Application.Quit(); // optional
        _appExcel.Quit();

        // release COM object from memory
         System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_appExcel);
        _appExcel = null;

        // optional: this Log function should be defined somewhere in your code         
        LogHasMacros(hasMacros);
        return _hasMacro;
    }
    catch (Exception ex)
    {
        // optional: this Log function should be defined somewhere in your code         
        LogError(ex);
        return null;
    }
    finally 
    {
        if (_appExcel != null)
        {
            _appExcel.Quit();
            // release COM object from memory
            System.Runtime.InteropServices.Marshal.FinalReleaseComObject(_appExcel);
        }
    }

注意可空 bool? type:在这个上下文中,函数返回的 null 表示错误(换句话说,结果未确定) true / false 值表示在待测Excel文件中存在/不存在任何VBA宏。

Notice nullable bool? type: in this context, null returned by function indicates the error (in other words, the result is undetermined), true/false values indicate presence/absence of any VBA macro in Excel file under test.

希望这可能有帮助。

这篇关于使用Interop.Excel检查Excel文件是否包含VBA宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆