User form and macro to run on open

J

jacqueline.fulton

I'm hoping someone can help me here. I am a complete beginner with VBA
and have been asked to carry out a task. I need to create a macro
which will run each time a new doc is created based on a template.
When this doc is opened I want to have a user form/box which asked the
user which document they want attached to this one, they will have a
choice of two. They will then choose and one of the two docs will be
attached to the end of the new doc being opened. Is this clear? I
need this to run when the new doc is created.

Any pointers on the best way to go about this?

Thanks in anticipation.

Jacqueline
 
J

Jonathan West

I'm hoping someone can help me here. I am a complete beginner with VBA
and have been asked to carry out a task. I need to create a macro
which will run each time a new doc is created based on a template.

Put the macro into the template and give it the name AutoNew.
When this doc is opened I want to have a user form/box which asked the
user which document they want attached to this one, they will have a
choice of two. They will then choose and one of the two docs will be
attached to the end of the new doc being opened. Is this clear? I
need this to run when the new doc is created.

Any pointers on the best way to go about this?

These articles will point you in the right direction

How to create a Userform
http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm

Running a macro automatically when a document is created, opened or closed
http://www.word.mvps.org/FAQs/MacrosVBA/DocumentEvents.htm
 
G

Graham Mayor

While Jonathan has given you the more elegant solution, user forms are a bit
of a stretch for a vba beginner.

A simpler solution is to use an ask field and an includetext field in the
document template eg

{ ASK Docname "Insert Document A or Document B?" \d A }
{ INCLUDETEXT "D:\\My Documents\\Test\\Versions\\Even\\{IF { Docname
\*Upper} = "A" "First.doc" "Second.doc"} }

The ASK field prompts the user for Document A or Document B. The Includetext
conditionally inserts one of two documents based on the response to the ASK
field

You will need an autonew macro in the template to fire the ASK field when a
new document is created

Sub AutoNew()
ActiveDocument.Fields.Update
End Sub

OR

Use an Input box to make the choice.

Sub AutoNew()
Dim sChoice As Variant
sChoice = InputBox("A - Document Name 1" & vbCr & _
"B - Document Name 2" & vbCr & vbCr & _
"Enter A or B", "Choose document", "A")
Selection.EndKey Unit:=wdStory
If UCase(sChoice) = "A" Then
Selection.InsertFile FileName:="D:\My
Documents\Test\Versions\Even\First.doc", Link:=False
Else
Selection.InsertFile FileName:="D:\My
Documents\Test\Versions\Even\Second.doc", Link:=False
End If
End Sub

In both cases you can modify the paths filenames and prompts to suit your
requirement. The ASK field is limited to what can be fit on one line. The
InputBox does not have that limitation.

http://www.gmayor.com/installing_macro.htm


--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
J

jacqueline.fulton

While Jonathan has given you the more elegant solution, user forms are a bit
of a stretch for a vba beginner.

A simpler solution is to use an ask field and an includetext field in the
document template eg

{ ASK  Docname "Insert Document A or Document B?" \d A }
{ INCLUDETEXT "D:\\My Documents\\Test\\Versions\\Even\\{IF { Docname
\*Upper} = "A"  "First.doc" "Second.doc"} }

The ASK field prompts the user for Document A or Document B. The Includetext
conditionally inserts one of two documents based on the response to the ASK
field

You will need an autonew macro in the template to fire the ASK field whena
new document is created

Sub AutoNew()
ActiveDocument.Fields.Update
End Sub

OR

Use an Input box to make the choice.

Sub AutoNew()
Dim sChoice As Variant
sChoice = InputBox("A - Document Name 1" & vbCr & _
                  "B - Document Name 2" & vbCr & vbCr &_
                  "Enter A or B", "Choose document", "A")
Selection.EndKey Unit:=wdStory
If UCase(sChoice) = "A" Then
    Selection.InsertFile FileName:="D:\My
Documents\Test\Versions\Even\First.doc", Link:=False
Else
    Selection.InsertFile FileName:="D:\My
Documents\Test\Versions\Even\Second.doc", Link:=False
End If
End Sub

In both cases you can modify the paths filenames and prompts to suit your
requirement. The ASK field is limited to what can be fit on one line. The
InputBox does not have that limitation.

http://www.gmayor.com/installing_macro.htm

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor -  Word MVP

My web sitewww.gmayor.com
Word MVP web sitehttp://word.mvps.org
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>








- Show quoted text -

Thanks very much both of you. Yes, user forms are a bit challenging
for a beginner like me and luckily someone else has created this for
me. I did the Autonew macro bit and it works perfectly. I haven't
explored the Ask field side of things and I will spend some time
looking at this. Thanks again for all your help both of you.
 

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