how to stop macros running when opening a Word doc

2019-02-23 08:42发布

问题:

As the title suggests, I have a .Net application which uses interop to open documents in Word. I have set

app.AutomationSecurity = Microsoft.Office.Core.MsoAutomationSecurity.msoAutomationSecurityForceDisable

before opening the document. According to the documentation, thhis "Disables all macros in all files opened programmatically, without showing any security alerts"

However, when I attempt to open one specific document I get a dialog box on the screen that says "could not load an object because it is not available on this machine". It's a customer document but I believe it contains a macro with references to a COM object which I don't have installed.

Am I doing something stupid? is there any way to actually disable macros when opening a Word document?

回答1:

Try:

WordBasic.DisableAutoMacros 1

Bizarrely, this relies on a throwback to pre-VBA days, but still seems to be the most-reliable way to ensure that no auto macros are triggered (in any document - you may want to turn it back using the parameter "0").

I recently had a project where I had to process 6,000 Word templates (yes, templates, not documents) many of which had oddball stuff like macros, etc. I was able to process all but 6 using this technique. (I never did figure out what the problem was with those 6).


EDIT: for a discussion of how to call this from C#, see: http://www.dotnet247.com/247reference/msgs/56/281785.aspx