开发者

Programmatically configuring MS-Word's Trust Center settings using C#

I have developed a simple C# Winforms application that loads MS-Word 2007 documents via COM automation.

This is all very simple and straight forward, however depending on the document I need to programamtically Enable or Disable Macros as well as ActiveX controls.

There is probably a way to store this in the registry, but I want to control these settings on an instance by instance basis as multiple concurrent requests may be running at a time.

So my question is 'how do I configure the trust center settings using COM automation'.

I have Googled for hours, but all I have been able to find is the Application.AutomationSecurity property, but this only accepts the following values:

  • MsoAutomationSecurity.msoAutomationSecurityLow
  • MsoAutomationSecurity.msoAutomationSecurityForceDisable
  • MsoAutomationSecurity.msoAutomationSecurityByUI

The Word 2007 Trust Center however exposes the following settings:

Macro Settings:

  • Disable all macros without notification (matches msoAutomationSecurityForceDisable)
  • Disable all macros with notifications (I don't need this one)
  • Disable all macros except digitally signed macros (No equivalent)
  • Enable all macros (matches msoAutomationSecurityLow)

Programmatically configuring MS-Word's Trust Center settings using C#

(source: visguy.com)


ActiveX controls (configured separately, I 开发者_运维技巧have not found any way to control these, note that according to the screenshot these settings are shared between all applications)

  • Disable all controls without notification
  • Prompt me before enabling UFI controls....
  • Prompt me before enabling all controls with minimal erstrictions
  • Enable all controls without restrictions

Programmatically configuring MS-Word's Trust Center settings using C#

I have tried the old trick of recording an MS-Word macro while changing these settings, but none of these steps are recorded.

Update: I have found the following entries for the ActiveX controls settings in the registry. Looks like ActiveX settings are indeed global and cannot be specified for a single MS-Word instance unless someone proves me wrong.

ActiveX Disabled

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Security] "DisableAllActiveX"=dword:00000001 "UFIControls"=dword:00000002

ActiveX Enabled with safe mode

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Security] "DisableAllActiveX"=dword:00000000 "UFIControls"=dword:00000002

ActiveX Enabled without safe mode

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Security] "DisableAllActiveX"=dword:00000000 "UFIControls"=dword:00000001

Still keen to resolve the macro settings problem


Looks like I am going to answer my own question.

I have tested it and can confirm the mappings are as follows:

Macro Settings:

  • msoAutomationSecurityForceDisable = Disable all macros without notification

  • msoAutomationSecurityByUI = Disable all macros except digitally signed macros

  • msoAutomationSecurityLow = Enable all macros

To the best of my knowledge the global ActiveX settings can only be configured by directly editing the registry

ActiveX Disabled

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Security] "DisableAllActiveX"=dword:00000001 "UFIControls"=dword:00000002

ActiveX Enabled with safe mode

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Security] "DisableAllActiveX"=dword:00000000 "UFIControls"=dword:00000002

ActiveX Enabled without safe mode

[HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Security] "DisableAllActiveX"=dword:00000000 "UFIControls"=dword:00000001

I have left a comment in the relevant section of the MSDN website


I know this thread is quite old, but I had to figure it out today so after a quick research I found this registry for the Trust Center Settings:

This applies to Word version 2010 (and probably 2007, but with 12.0 instead of 14.0)

Programmatically configuring MS-Word's Trust Center settings using C#

Or in text:

Registry location:

HKEY_CURRENT_USER\Software\Microsoft\Office\14.0\Word\Security

Macro Settings:

Name: VBAWarnings

Data:

Disable all macros without notification - 4

Disable all macros with notification - 2

Disable all macros except digitally signed macros - 3

Enable all macros (...) - 1

Developer Macro Settings:

Name: AccessVBOM

Data:

Unchecked - 0

Checked - 1


For the setting for ActiveX controls in office 2010

to DisbaleActiveX without safe mode you only need ...

"HKEY_CURRENT_USER\Software\Microsoft\Office\Common\Security" /v UFIControls /t REG_DWORD /D 1 /F


I've spent a couple of days trying the same thing and finally discovered a very simple way of opening an .xls file containing macros, without messing with the registry or Excel's trust settings. In C#:

Application aXL = new Application();
aXL.FileValidation = 
    Microsoft.Office.Core.MsoFileValidationMode.msoFileValidationSkip;
try {
     Workbook aBook = aXL.Workbooks.Open("K:\\Work\\ExcelTest\\BrokenMacro.xls"
                    , 0
                    , true
                    , Type.Missing
                    , Type.Missing
                    , Type.Missing
                    , true
                    , Type.Missing
                    , Type.Missing
                    , false
                    , false
                    , Type.Missing
                    , false
                    , false
                    , Type.Missing
                    /*,false*/);
            }
            catch (Exception e) {
                Console.WriteLine(e);
            }

See MSDN for details.

My Excel trust center setting were all set to default values - "Disable All Macros with Warnings", and "Do Not trust access to the VBA object model. Without the msoFileValidationSkip option an exception was thrown. With the msoFileValidationSkip option the file opened fine.

It seems to me that this is the way to go, since it allows a program to open files with macros, but doesn't open up the Excel application virus ridden spreadsheets.

Note that I'm running Office 2010. I don't know on which version of Office this option was introduced.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜