Der*_*mes 7 .net vb.net vba ms-word
我有一个以编程方式的VB.Net/C#应用程序:
码:
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
Run Code Online (Sandbox Code Playgroud)
多年来,这一切都很愉快.
我现在有了新的要求; 我的应用程序只需要运行SIGNED Word宏.
这在Word用户界面中很容易做到,如下所示:
File > Options > Trust center > Macro Settings
Select "Disable all macros except digitally signed macros"
Run Code Online (Sandbox Code Playgroud)
设置此项后,如果运行Word的人显示"宏"对话框,则不会列出任何未签名(或已签名但不可信)的宏.这就像我期望的那样.
但是,打开Word应用程序的VB.Net代码可以绕过这个.当我运行此代码时,它将运行无符号宏:
With mobjWordApp.Dialogs.Item(Word.WdWordDialog.wdDialogToolsMacro)
.Name = MacroName
.Run = True
.Execute()
End With
Run Code Online (Sandbox Code Playgroud)
我需要知道的是:
有没有办法让我的代码在运行之前识别宏是否已签名(和受信任)?
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
Run Code Online (Sandbox Code Playgroud)