How can you programmatically determine if a Word macro is signed from VB.Net/C#

I have a VB.Net/C# application that programmatically:

  • Creates an RTF document
  • Open it in Microsoft Word
  • Runs the Word macro that exists in the Word template using the following code:

The code:

Protected mobjWordApp As Word.Application = Nothing ' ' lots more code snipped for clarity ' With mobjWordApp.Dialogs.Item(Word.WdWordDialog.wdDialogToolsMacro) .Name = MacroName .Run = True .Execute() End With 

It worked for a long time.

Now I have a new requirement; My application is only required to run SIGNED Word Macros.

This is easily done in the Word user interface as follows:

 File > Options > Trust center > Macro Settings Select "Disable all macros except digitally signed macros" 

Trust center

Once this is established, if the person launching Word displays the Macros dialog box, any unsigned (or signed but unreliable) macros are not listed. This is all as I expected.

However, my VB.Net code that opens the Word application can work around this. When I run this code, it will run an unsigned macro:

 With mobjWordApp.Dialogs.Item(Word.WdWordDialog.wdDialogToolsMacro) .Name = MacroName .Run = True .Execute() End With 

I need to know:

Is there a way for my code to determine if a macro is signed (and trusted) before it runs?

+7
source share
1 answer
 Dim ap As New Application() Dim doc As Document = ap.Documents.Open("C:\Doc1.rtf", [ReadOnly] := False, Visible := False) doc.Activate() 'returns a boolean on if the VBA is signed doc.VBASigned 

MSDN Link

+4
source

All Articles