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

1

7

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

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.

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?

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

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.

Crustal answered 30/6, 2014 at 11:16 Comment(4)
Do you mean you want to open a browser which enables the user to select dotm files only?Iden
Hi @user3165438. No. I just want a form that I have designed to be displayed on opening the document whether opened using File...Open... or if it is opened from Windows either by double clicking the file.Crustal
Maybe it is a Word addin that performs actions on events. Nice idea anyway.Iden
Maybe there is a misunderstanding here? Generator.dotm contains buildingblocks, macros, forms and styles. Sub AutoOpen() is part of Generator.dotm and calls startpage.show. Startpage is a form which is also in generator.dotm. All I want is Startpage.show to be run automatically when generator.dotm is opened. If word is not open and generator.dotm is double clicked, the form will not display. If generator.dotm is opened within word, the form does display. I want it to display the form in both scenarios.Crustal
C
9

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

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:

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.

Cholecyst answered 30/6, 2014 at 14:47 Comment(3)
Thank you so very much!! This worked perfectly and I appreciate you taking the time to help me with such a basic question. :)Crustal
Why do you need those first two lines of code? Why can't you just run UserForm1.Show??Mouse
@SteveS these two lines of code open a new instance as opposed to opening the same instance. So if you open a second document the form of the second document is going to be new whereas if you do UserForm1.Show the form is going to be the same as in the first document which can cause trouble.Gaven

© 2022 - 2024 — McMap. All rights reserved.