Derek Tomes Derek Tomes - 7 months ago 31
Vb.net Question

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

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


  1. Creates an RTF document

  2. Open it up in Microsoft Word

  3. Runs a Word macro that exists in the Word Template using code like this:



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


This has worked happily for years.

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

This is easy enough to do 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 set, if the person running Word displays the Macros dialog, any unsigned (or signed but untrusted) macros are not listed. This is all as I would expect.

However, my VB.Net code, which is opening the Word Application can bypass this. When I run this code it will run a unsigned macro:

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


What I need to know is:

Is there a way for my code to identify if a Macro is signed (and trusted) before I run it?

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