Making a document open a userform in the originating template

N

Neil Humphries

I have a template which opens the save as dialog box and then opens a
userform. I want the documents created from the template to also open the
userform when the documents are opened. How do I do this? Do I need to insert
an autoopen macro into the document when it is created?
 
J

Jay Freedman

Neil said:
I have a template which opens the save as dialog box and then opens a
userform. I want the documents created from the template to also open
the userform when the documents are opened. How do I do this? Do I
need to insert an autoopen macro into the document when it is created?

No, you don't put any macros in any document (unless you like dealing with
Word's macro virus warning popups). You put the AutoOpen (or Document_Open)
code in the template. See
http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
N

Neil Humphries

Jay Freedman said:
No, you don't put any macros in any document (unless you like dealing with
Word's macro virus warning popups). You put the AutoOpen (or Document_Open)
code in the template. See
http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
I already had code in the ThisDocument object, but nothing was happening. I
have now discovered other code that worked when in a .docm file, but failed
when in a .dotm file. It was not a VBA issue, just the test that was being
evaluated to determine if the user was opening the master document from the
original location or a saved as copy. I fixed the code and now all works fine.

Without your response, I would have wasted a lot of time puzzling over how
documents relate to their templates rather than looking elsewhere for the
problem. Thanks.
 
N

Neil Humphries

Jay Freedman said:
No, you don't put any macros in any document (unless you like dealing with
Word's macro virus warning popups). You put the AutoOpen (or Document_Open)
code in the template. See
http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
If I double click on my template in windows explorer, everything works as
expected. If I open the file by clicking on a hyperlink in eXcel I get an
error message:
Run-time error '4248'
This command is not available because no document is open.

Pressing the Debug button takes me to the first line of the ThisDocument
procedure below.

Private Sub Document_Open()
IsTemplate = ActiveDocument.Path Like "*Templates*"
If IsTemplate Then
Application.Run MacroName:="FileSaveAs"
End If
Application.ActiveDocument.Activate
Application.ScreenRefresh
Main
End Sub

Why does it behave differently depending on how it is opened, and why is the
Document_Open procedure running? I expected the Document_New procedure to run.
 
J

Jay Freedman

If I double click on my template in windows explorer, everything works as
expected. If I open the file by clicking on a hyperlink in eXcel I get an
error message:
Run-time error '4248'
This command is not available because no document is open.

Pressing the Debug button takes me to the first line of the ThisDocument
procedure below.

Private Sub Document_Open()
IsTemplate = ActiveDocument.Path Like "*Templates*"
If IsTemplate Then
Application.Run MacroName:="FileSaveAs"
End If
Application.ActiveDocument.Activate
Application.ScreenRefresh
Main
End Sub

Why does it behave differently depending on how it is opened, and why is the
Document_Open procedure running? I expected the Document_New procedure to run.

The behavior of Word templates is different in Windows Explorer and in
Internet Explorer (and other browsers and anything else that uses a
hyperlink). The default action in Windows Explorer is "New", while the
default action for hyperlinks is "Open". This is (sort of) documented
in http://support.microsoft.com/kb/278627.

The Document_Open procedure runs because the template itself is
opening in Word.

That KB article and
http://www.addbalance.com/word/templatesmenu.htm#Hyperlinks explain a
workaround: Make a shortcut to the template, and create the hyperlink
to point to the shortcut instead of pointing to the template itself.
 
N

Neil Humphries

Jay Freedman said:
The behavior of Word templates is different in Windows Explorer and in
Internet Explorer (and other browsers and anything else that uses a
hyperlink). The default action in Windows Explorer is "New", while the
default action for hyperlinks is "Open". This is (sort of) documented
in http://support.microsoft.com/kb/278627.

The Document_Open procedure runs because the template itself is
opening in Word.

That KB article and
http://www.addbalance.com/word/templatesmenu.htm#Hyperlinks explain a
workaround: Make a shortcut to the template, and create the hyperlink
to point to the shortcut instead of pointing to the template itself.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all may benefit.
.
I followed the links you provided and read the information but could not get
the hyperlink to work in Excel. I got a Cannot Find the Specified File error.

Then I changed the name of the shortcut in the Excel hyperlink by appending
..LNK to it. Now the hyperlink finds the shortcut, but opens the template for
editting rather than creating a new document from the template.

So I am back to needing to get an Excel hyperlink to open a new document
based on a template.
 
D

Doug Robbins - Word MVP

If I include the following AutoOpen macro in a template

Sub AutoOpen()
Dim myForm As frmLetterForm

Set myForm = New frmLetterForm
myForm.Show vbModal
Unload myForm
Set myForm = Nothing

End Sub

and then create a hyperlink to that template in an Excel spreadsheet and
then click on that link, after clicking on the Yes button in response to the
question "Do you want to continue?" in the Microsoft Office Excel Security
Notice message that appears, the template is opened and the user form that
is in it is displayed.

Of course that is not desirable, because I would not really be wanting to
edit the template which is what would happen.

What you should be doing is putting a Button in the spreadsheet that calls a
macro in Excel that automates Word and then uses the

Documents.Add("templatepath\name")

command so that a new document is created from the template and then the
AutoNew command in the template will run.

See the article "Control Word from Excel†at:
http://www.word.mvps.org/FAQs/InterDev/ControlWordFromXL.htm




--
Hope this helps,

Doug Robbins - Word MVP

Please reply only to the newsgroups unless you wish to obtain my services on
a paid professional basis.
 

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