我有一个 VB.Net/C# 应用程序,它以编程方式:

  • 创建 RTF 文档
  • 在 Microsoft Word 中打开它
  • 使用如下代码运行 Word 模板中存在的 Word 宏:

  • 代码:
    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 
    

    这已经愉快地工作了多年。

    我现在有一个新的要求;我的应用程序只需要运行 SIGNED Word 宏。

    这很容易在 Word 用户界面中完成,如下所示:
    File > Options > Trust center > Macro Settings 
    Select "Disable all macros except digitally signed macros" 
    



    设置后,如果运行 Word 的人显示“宏”对话框,则不会列出任何未签名(或已签名但不受信任)的宏。这一切正如我所料。

    但是,我打开 Word 应用程序的 VB.Net 代码可以绕过这个。当我运行此代码时,它将运行一个未签名的宏:
    With mobjWordApp.Dialogs.Item(Word.WdWordDialog.wdDialogToolsMacro) 
        .Name = MacroName 
        .Run = True 
        .Execute() 
    End With 
    

    我需要知道的是:

    有没有办法让我的代码在运行之前识别宏是否已签名(并受信任)?

    请您参考如下方法:

    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


    评论关闭
    IT干货网

    微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!