开发者

how to stop macros running when opening a Word document using OLE Interop?

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 file开发者_JAVA百科s 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?


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


For c# you can use

(_wordApp.WordBasic as dynamic).DisableAutoMacros();

The whole code I'm using is:

using Word = Microsoft.Office.Interop.Word;
private Word.Application _wordApp;
...
                    _wordApp = new Word.Application
                {
                    Visible = false,
                    ScreenUpdating = false,
                    DisplayAlerts = Word.WdAlertLevel.wdAlertsNone,
                    FileValidation = MsoFileValidationMode.msoFileValidationSkip
                };

                _wordApp.Application.AutomationSecurity = MsoAutomationSecurity.msoAutomationSecurityForceDisable;
                (_wordApp.WordBasic as dynamic).DisableAutoMacros();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜