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.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
fraz
  • 73
  • 1
  • 1
  • 3
  • Do you mean you want to open a browser which enables the user to select dotm files only? – user3165438 Jun 30 '14 at 12:01
  • 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. – fraz Jun 30 '14 at 12:47
  • Maybe it is a Word addin that performs actions on events. Nice idea anyway. – user3165438 Jun 30 '14 at 12:58
  • 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. – fraz Jun 30 '14 at 13:24

1 Answers1

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.

Sean W.
  • 863
  • 5
  • 14
  • Thank you so very much!! This worked perfectly and I appreciate you taking the time to help me with such a basic question. :) – fraz Jun 30 '14 at 15:08
  • Why do you need those first two lines of code? Why can't you just run `UserForm1.Show`?? – Steve S Jun 08 '17 at 20:38
  • @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. – SandroKSG Aug 02 '18 at 08:04