vba 自动执行 AutoOpen Word 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25161454/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 04:06:49  来源:igfitidea点击:

Auto Execution of AutoOpen Word Macro

vbams-wordword-vba

提问by user3733735

I was having requirement to run the Macro when we open the document. For that purpose i have created .Dotm template with AutoOpen method. Problem is it doesn't execute. When i add the AutoOpen macro in Normal file it works perfectly.

当我们打开文档时,我需要运行宏。为此,我使用 AutoOpen 方法创建了 .Dotm 模板。问题是它不执行。当我在普通文件中添加 AutoOpen 宏时,它运行良好。

We don't want to add the AUtoOpen code in Normal.dotm file. How can we achieve the same using different Template file.

我们不想在 Normal.dotm 文件中添加 AUtoOpen 代码。我们如何使用不同的模板文件实现相同的效果。

采纳答案by niton

"For that purpose i have created .Dotm template with AutoOpen method."

“为此,我使用 AutoOpen 方法创建了 .Dotm 模板。”

Create new documents with this NotNormalWithAutoOpenMethod.Dotm with File | New.

使用此 NotNormalWithAutoOpenMethod.Dotm with File 创建新文档 | 新的。

For an existing document, create a new document based on NotNormalWithAutoOpenMethod.Dotm. Copy the contents of the existing document into the new blank document. Save and Close. Upon reopening the new document, the code should run.

对于现有文档,基于 NotNormalWithAutoOpenMethod.Dotm 创建一个新文档。将现有文档的内容复制到新的空白文档中。保存并关闭。重新打开新文档后,代码应该会运行。

回答by Mark

I had exactly the same problem. I had 2 separate .dotms with AutoOpen modules that were not auto opening on XP or Win7 x64 both with Office 2010. I subsequently noticed that all my Subs in the AutoOpen module were Private. Hmmmmm. Made my Main Sub Public, left all other Subs Private, and both of them now AutoOpen. Maybe this will help.

我遇到了完全相同的问题。我有 2 个单独的 .dotms 和 AutoOpen 模块,它们在 XP 或 Win7 x64 上都没有在 Office 2010 上自动打开。我随后注意到我在 AutoOpen 模块中的所有 Subs 都是私有的。嗯嗯。将我的 Main Sub Public 设为 Public,将所有其他 Subs 设为 Private,并且它们现在都是 AutoOpen。也许这会有所帮助。

回答by user13803

Wasn't me, but I'm guessing you got downvoted because you haven't said what you've tried, what version of Word you're using, or what OS you're on. All of those things are useful in diagnosing the issue.

不是我,但我猜你被否决了,因为你没有说你尝试过什么,你使用的是什么版本的 Word,或者你使用的是什么操作系统。所有这些都有助于诊断问题。

I'm also guessing that you can notsimply save the macro to whatever document you want to run it in, because that should be trivial. Am I correct?

我还猜测您不能简单地将宏保存到您想要运行它的任何文档中,因为这应该是微不足道的。我对么?

If so, I am trying to do something very similar using Word 2010 on Windows 7 64-bit, but I can't get it to work yet either. The document I need to open is auto-generated from pandoc and is opened in compatibility mode, so perhaps that's my issue. If that's not the case for you, some of the things I've tried might help you.

如果是这样,我正在尝试在 Windows 7 64 位上使用 Word 2010 做一些非常相似的事情,但我也无法让它工作。我需要打开的文档是从 pandoc 自动生成的,并以兼容模式打开,所以也许这就是我的问题。如果您不是这种情况,我尝试过的一些方法可能会对您有所帮助。

  1. Use the /t option from the command line: start winword /tmacros.dotm document.docx
  2. If that doesn't work, try adding the /m option as well: start winword /tmacros.dotm /mAutoOpen document.docx.
  3. Instead of naming the macro AutoOpen, try AutoExecor Document_Open.
  1. 从命令行使用 /t 选项: start winword /tmacros.dotm document.docx
  2. 如果不工作,尝试添加/ m选项,以及:start winword /tmacros.dotm /mAutoOpen document.docx
  3. 不要命名宏AutoOpen,而是尝试AutoExecDocument_Open

Note: there is no space between the /t or /m options and the filenames. I've also tried the full path to winword.exe instead of start winwordbut that didn't make a difference.

注意: /t 或 /m 选项和文件名之间没有空格。我还尝试了 winword.exe 的完整路径,而不是,start winword但这并没有什么区别。

Hope this helps, good luck! And please post the solution back here if you figure it out on your own.

希望这有帮助,祝你好运!如果您自己解决了,请将解决方案发回此处。

Sources:

资料来源:

Command line switch: http://support.microsoft.com/kb/290961

命令行开关:http: //support.microsoft.com/kb/290961

AutoExec/AutoOpen: http://support.microsoft.com/kb/286310

AutoExec/AutoOpen:http: //support.microsoft.com/kb/286310