I have an VB.Net/C# application that programmatically:
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"
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?
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.
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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With