Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 331
Derek Tomes Avatar asked Mar 08 '12 03:03

Derek Tomes


People also ask

How do I sign a macro in Visual Basic?

Open the file that contains the macro project that you want to sign. On the Developertab, in the Codegroup, click Visual Basic. Note: If the Developer tab is not available: Click the Filetab. Click Options. Click Customize Ribbon.

How do I find macros in the VBA editor?

Press Alt+F11 to find macros in the VBA Editor. To verify the issue is resolved, click File > Info > Check for Issues, and click Inspect Document.

How do I digitally sign a MACRO project?

On the Toolsmenu, click Internet Options, and then click the Contenttab. Click Certificates, and then click the Personaltab. Digitally sign a macro project in Excel, PowerPoint, Publisher, Visio, Outlook, or Word

When should I sign macros?

It is recommended that you sign macros only afteryour solution has been tested and ready for distribution: when code in a signed macro project is changed in any way, its digital signature is removed.


1 Answers

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

like image 171
Micah Armantrout Avatar answered Sep 19 '22 05:09

Micah Armantrout