Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/frozleaf/article/details/91567675
problem:
Because there are macro code and documentation problems, leading vba pop-up error message box when you open a document, resulting code is blocked
solve:
c#
static void Main(string[] args)
{
Console.WriteLine("word文件:");
var file = Console.ReadLine();
Application app = null;
Document doc = null;
try
{
app = new Microsoft.Office.Interop.Word.ApplicationClass();
// 屏蔽宏
app.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
doc = app.Documents.Open(file);
doc.SaveAs(file + ".html", Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML);
}
finally
{
doc.Close();
app.Quit();
}
}
powershell:
$Word=NEW-Object –ComObject Word.Application;
# $Word.WordBasic.DisableAutoMacros(1); 使用这个也可以
$Word.AutomationSecurity=3;
$Document=$Word.documents.open("d:\1.doc");
$Document.Close();
$Word.Quit();
MsoAutomationSecurity enumeration (Office)
Name | Value | Description |
---|---|---|
msoAutomationSecurityByUI | 2 | Uses the security setting specified in the Security dialog box. |
msoAutomationSecurityForceDisable | 3 | Disables all macros in all files opened programmatically without showing any security alerts. |
msoAutomationSecurityLow | 1 | Enables all macros. This is the default value when the application is started. |
reference:
https://docs.microsoft.com/zh-CN/office/vba/api/office.msoautomationsecurity