为什么使用Office Automation从Delphi打开文档时,Document_open宏不在单词中运行

发布于 2025-01-31 10:29:16 字数 378 浏览 3 评论 0原文

我正在尝试使用Delphi Tokyo的办公自动化打开Word文档(使用Word 2016)。该文档具有Document_open宏,该宏可提示用户可选地运行另一个宏。

当我运行以下代码时: -

Doc := IDispatch(WordApp.Documents.Open([*filename*])) as _Document;

文档打开,但是document_open宏未运行。 ,如果我尝试手动运行第二个(主)宏,我会遇到一个错误,说“该项目中的宏已禁用...”

另外 预期,因此显然没有残疾。

VBA项目是数字签名的,单词设置为“禁用除数字签名宏以外的所有宏”。

从Delphi内打开文档时,如何使宏可以运行?

I am trying to use Office Automation in Delphi Tokyo to open a Word document (using Word 2016). The document has a Document_Open macro that prompts the user to optionally run another macro.

When I run the following code:-

Doc := IDispatch(WordApp.Documents.Open([*filename*])) as _Document;

the document opens, but the Document_Open macro does not run. Also, if I try to run the second (main) macro manually, I get an error saying "The macros in this project are disabled ..."

However, if I open the same document from the Word file menu, the macros run as expected, so they are clearly not disabled.

The VBA project is digitally signed and Word is set to "Disable all macros except digitally signed macros".

How do I get my macros to run when opening the document from within Delphi?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

喜爱皱眉﹌ 2025-02-07 10:29:16

Braknicku带领我找到了答案。

我发现,除非您设置WordApp。

因此,我问题的答案是: -

WordApp := GetActiveOleObject('Word.Application');

//store AutomationSecurity setting
LogonAutomationSecurity := WordApp.AutomationSecurity;

try
  //set AutomationSecurity
  WordApp.AutomationSecurity := msoAutomationSecurityByUI;
  Doc := IDispatch(WordApp.Documents.Open([*filename*])) as _Document;
  ...

finally
  WordApp.AutomationSecurity := LogonAutomationSecurity;
end;

然后,只要用户的信任设置允许宏运行,它将!

我希望这对某人有帮助。

BrakNicku led me to the answer.

I have found that unless you set WordApp.AutomationSecurity to msoAutomationSecurityByUI, macros are disabled irrespective of the settings in Word.

So the answer to my question is:-

WordApp := GetActiveOleObject('Word.Application');

//store AutomationSecurity setting
LogonAutomationSecurity := WordApp.AutomationSecurity;

try
  //set AutomationSecurity
  WordApp.AutomationSecurity := msoAutomationSecurityByUI;
  Doc := IDispatch(WordApp.Documents.Open([*filename*])) as _Document;
  ...

finally
  WordApp.AutomationSecurity := LogonAutomationSecurity;
end;

Then, provided the user's Trust Settings allow the macro to run, it will!

I hope this helps someone.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文