Help Needed - Placement of AutoOpen code

C

Charles Warner

I've done my Usenet homework on this, tried every method I found
there, including many that were contradictory to each other ... but
remain as confused as ever.

Here's my objective:

When I create a new document, I want to incorporate an AutoOpen macro
in it that executes code in Module1 (which code maps the F3 key to
open a function within Module1, the function opening a UserForm).

When the document is saved, I want to create a situation in which
whenever that document is opened again on a workstation attached to
our network, the AutoOpen macro will run again, again invoking the
same code as above.

I've tried and failed any number of approaches to this, including
creating an add-in in the Startup folder containing AutoOpen, module1
and the UserForm.

I really need some help with this ...

Thanks
 
J

Jean-Guy Marcil

Hi Charles,

How does it fail?
Can you post the code from your AutoOpen procedure?

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Charles Warner

Jean-Guy ...

Thank you for your reply. My code is as simple as it can be:

In the module "AutoOpen," I have this:

Public Sub Main()
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro,
KeyCode:=(BuildKeyCode(wdKeyF3)), Command:="NewAutoText"
End Sub

This code *does* work ... but only if I paste it (using the VB Editor)
into "ThisDocument" in the Project Explorer. After that, it will go
on working wherever and whenever the document is opened.

My goal is to find a place to put it, along with the code it invokes,
(Module1) and the UserForm ... in a "place" where every document that
gets created contains AutoOpen, Module1, and the UserForm. I want it
to stay there, so that every time the document is opened after that,
AutoOpen will invoke code in Module1, open the UserForm etc.

I need to know that "place."

Please ignore the fact that this works when I paste the code into
"ThisDocument." That is incidental, I think ... just a part of my
desperate troubleshooting. What I really want to know is how to do
this (please see "My goal...) above ... as a Word VB professional
would ... basically ... if you wanted to do this, how would you
accomplish this?

Perhaps the answer to my question is so simple I can't see "the forest
for the trees" That's entirely possible, since I'm new to this.

Regards,

Charles
 
J

Jean-Guy Marcil

Hi Carles,

Normally,
Open the Template from which your documents will be created;
Go to the ThisDocument module in the VBE;
Create a ThisDocment_Open procedure (Above the code window there are two
dropdown lists, from the one on the left, select "Docment" then
Private Sub Document_New()

End Sub
will be added to the code window, ignore that, you can delete it later if
you don't need it, go to the dropdown list on the right and select Open, at
which point
Private Sub Document_Open()

End Sub
will be added to the code window).
In the Sub Document_Open() procedure, put the code that invoke the Module1
code (from the same Template), which, if I understand you correctly will
call the Userform (also stored in the same template).

By the way, in the code you posted, I do not see any calls to a Module1 or a
Userform...

HTH

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Charles Warner

Thank you Jean-Guy ... especially for your explicit instructions,
which I needed very badly.

Your solution worked perfectly.

Your question, which may require some additional instruction to me on
"where to put things":

I have, in Project Explorer:

TemplateProject (MyTemplateName)
(The template I selected in the dialog when using File/New to create
the document).

I have 3 folders under that:

"Microsoft Word Objects", which contains ThisDocument
"Forms", which contains my UserForm
"Modules", which contains Module1

I've placed the code as you suggested:

Private Sub Document_Open()

KeyBindings.Add KeyCategory:=wdKeyCategoryMacro,
KeyCode:=(BuildKeyCode(wdKeyF3)), Command:="NewAutoText"

End Sub

The code invoked by F3 is in Module1. It's a function that accepts
as an arguements a Selection from the document, and opens the
UserForm.

From the nature of your question, it's quite possible that I have some
of the above entities in the wrong place(s).

Please let me know ...

Regards,

Charles
 
C

Charles Warner

Jean-Guy,

Please bear with me ... one final question:

We have legacy documents that were *not* created with the template
we've been working on ... but require the same set of actions when
they are opened. How do I manage that?

Regards,

Charles
 
J

Jean-Guy Marcil

Hi Charles,

Easiest way:
Open the said documents;
From the menu Tools > Models & Add-ins;
Attach the document to the desired template using the top part of the dialog
box that pops up;
Click OK;
Save the document.

--
Cheers!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
C

Charles Warner

Many thanks Jean-Guy, for all your help with this and for providing
the very detailed explanations on how to do it.

I'll have a nice weekend now, knowing that my last few problems have
been solved.

Regards,

Charles
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top