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

前端 未结 2 506
情话喂你
情话喂你 2020-12-21 01:49

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.MsoAut

相关标签:
2条回答
  • 2020-12-21 02:32

    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

    0 讨论(0)
  • 2020-12-21 02:44

    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 讨论(0)
提交回复
热议问题