gpt4 book ai didi

.net - 如何以编程方式判断 Word 宏是否是从 VB.Net/C# 签名的

转载 作者:行者123 更新时间:2023-12-04 12:35:36 25 4
gpt4 key购买 nike

我有一个 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"

    Trust Center

    设置后,如果运行 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

    关于.net - 如何以编程方式判断 Word 宏是否是从 VB.Net/C# 签名的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9612437/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com