vba 打开模板文件时自动显示表单,来自资源管理器的 dotm

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24488916/
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 03:38:45  来源:igfitidea点击:

Auto displaying form on opening a template file, dotm from explorer

vbams-wordword-vbaword-2013

提问by fraz

I have written a form based document generation macro (in VBA) for distribution to a sales team.

我编写了一个基于表单的文档生成宏(在 VBA 中),用于分发给销售团队。

For their ease of use, I want to provide a self-contained file which will display the form as soon as the document is opened.

为了便于使用,我想提供一个自包含文件,该文件将在打开文档后立即显示表单。

Using AutoOpen I can get the form to display as intended if word is already open and the dotm file is opened within. However, if I double click the file from within explorer, nothing happens and I have to launch the macro manually. I thought AutoExec might allow this but no luck there. I've spent considerable time trying to get this to work through googling etc. but I'm not getting anywhere.

如果 word 已经打开并且 dotm 文件在其中打开,我可以使用 AutoOpen 使表单按预期显示。但是,如果我从资源管理器中双击该文件,则不会发生任何事情,我必须手动启动宏。我认为 AutoExec 可能允许这样做,但没有运气。我花了相当多的时间试图通过谷歌搜索等方式使其工作,但我一无所获。

How can I make the form display even when the file is opened with a double click? Is it possible to do this without having to change normal.dotm for each user?

即使双击打开文件,如何使表单显示?是否可以在不必为每个用户更改 normal.dotm 的情况下执行此操作?

For further background, I am using Word 2013 with macros fully enabled during testing. The dotm file is stored in a trusted location.

对于进一步的背景,我使用 Word 2013 并在测试期间完全启用宏。dotm 文件存储在受信任的位置。

I am using a macro to launch the form like this...

我正在使用宏来启动这样的表单......

Public Sub AutoOpen()
    StartPage.Show
End Sub

I have tried using AutoExec as well to no avail.

我也尝试过使用 AutoExec 也无济于事。

回答by Sean W.

In the "generator.dotm" file got to Visual Basic and go in to the "ThisDocument" Microsoft Word Object.

在“generator.dotm”文件中进入 Visual Basic 并进入“ThisDocument”Microsoft Word 对象。

At the top of the Visual Basic Editor select "Document" in the left hand side and then click on "New" on the right hand side. Private Sub Document_New()method will appear for you to be able to edit. Then you can call your userform in there. Similar to:

在 Visual Basic 编辑器的顶部,选择左侧的“文档”,然后单击右侧的“新建”。 Private Sub Document_New()将出现方法供您编辑。然后你可以在那里调用你的用户表单。相似:

Private Sub Document_New()

    Dim myForm As UserForm1
    Set myForm = New UserForm1

    myForm.Show

End Sub

Save your Generator.dotm and double click it through Windows explorer and you should get the results that you would like.

保存您的 Generator.dotm 并通过 Windows 资源管理器双击它,您应该会得到您想要的结果。