使用 Interop.Excel 检查 Excel 文件是否包含 VBA 宏
Using Interop.Excel to check if Excel file contains VBA Macros
在我的应用程序中,我必须检查 excel-文档是否包含 vb-宏。所以我写了下面的方法来检查 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。
91: 对象变量或 With 块变量未设置
我调试了它,才发现消息出现在对 excelApplication.Workbooks.Close();
的调用中。如果我删除这行代码,但相同的 excel-消息出现在 excelApplication.Quit();
.
的调用中
我需要做什么才能正确关闭 excel-sheet 并防止 excel 显示此消息?
与您的任务相关,您可以参考以下精炼的代码片段,它利用了.NET/C#、Microsoft.Office.Interop.Excel
对象库和Runtime.InteropServices.Marshal
对象:
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);
}
}
Notice nullable bool?
type:在此上下文中,函数返回的null
表示错误(即结果未定), true
/false
值表示被测 Excel 文件中任何 VBA 宏的 presence/absence。
希望这可能有所帮助。
在我的应用程序中,我必须检查 excel-文档是否包含 vb-宏。所以我写了下面的方法来检查 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。
91: 对象变量或 With 块变量未设置
我调试了它,才发现消息出现在对 excelApplication.Workbooks.Close();
的调用中。如果我删除这行代码,但相同的 excel-消息出现在 excelApplication.Quit();
.
我需要做什么才能正确关闭 excel-sheet 并防止 excel 显示此消息?
与您的任务相关,您可以参考以下精炼的代码片段,它利用了.NET/C#、Microsoft.Office.Interop.Excel
对象库和Runtime.InteropServices.Marshal
对象:
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);
}
}
Notice nullable bool?
type:在此上下文中,函数返回的null
表示错误(即结果未定), true
/false
值表示被测 Excel 文件中任何 VBA 宏的 presence/absence。
希望这可能有所帮助。