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

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

  • Creates an RTF document
  • Open it up in Microsoft Word
  • 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"
    

    信任中心

    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?


    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链接

    链接地址: http://www.djcxy.com/p/58102.html

    上一篇: 避免用多种不同的语言编写相同的算法

    下一篇: 如何以编程方式告诉您是否从VB.Net/C#签名了一个Word宏