Please help me. I have lot of templates in word which I would like to open when selected from a list box. I am new to access and would like help in writing the code for this. Thank you very much in advance.
You didnt mention how you want to launch the templates so I am assuming a command button.
Once the user makes their selection in your list box, they will push the command button to launch the selected Word template.
So, here is a way to do this.
Behind the command button, in a OnClick event, we need to put some code like this: (I am noting the code as I write it)
'****begin code*****
Private Sub Command2_Click()
Dim oApp As Object '<--declare variable
Select Case Me.List0 '<--using the Select Case is easier to me than using a bunch of If...Then statements
'Me.List0 is my demo listbox
Case "word1" '<---my first word doc listing in the listbox
Set oApp = CreateObject("Word.Application" '<---create Word object
oApp.Visible = True '<---make Word visible
oApp.Activate '<---Activate Word Doc 'Open document
oApp.Documents.Add Template:="\\gaf5142tnscs001\shared\electronic interfaces\ESCMAD\letters\Agreement Cover Letter.dot" '<---this is the location of your documents. mine are on a share drive.
'your documents could be on your local drive "c:\word\test.doc"
Set oApp = Nothing <---close word app
'see notes above and duplicate below for each word doc
Case "word2"
Set oApp = CreateObject("Word.Application"
oApp.Visible = True
oApp.Activate
'Open document
oApp.Documents.Add Template:="\\gaf5142tnscs001\shared\electronic interfaces\ESCMAD\letters\suspend.dot"
Set oApp = Nothing
End Select 'end your select statement
End Sub
'*******end code*****
Hope this helps. Let me know. Have A Great Day!!!,
Hi Nathan
I copied the code into the command button and named the list box as list0. Definetely I didn't forget to change the path of the file but still when I click the button theword template does not open. Instead if I comment out the select statement in the code it works. Can you please help me with this.
Hi Nathan,
Thanks for getting back to my question. Following is the copy of the code that I inserted into the Onclick property of the OpenReport command button. I named the list box list0 and currently I have two cases in my list box.
(1) nutpan + Label Format.dot and
(2) amylose.dot
When I select either (1) or (2) and click on Open Report button it is supposed to open word template for either one of them but it does not. I tried commenting the line
Set oApp = Nothing in the following code but still it does not work. Please ... help me.
Private Sub OpenReport_Click()
'****begin code*****
Dim oApp As Object '<--declare variable
Select Case Me.List0 '<--using the Select Case is easier to me than using a bunch of If...Then statements
'Me.List0 is my demo listbox
Case "Nutpan + Label Format.dot" '<---my first word doc listing in the listbox
Set oApp = CreateObject("Word.Application" '<---create Word object
oApp.Visible = True '<---make Word visible
oApp.Activate '<---Activate Word Doc
'Open document
oApp.Documents.Add Template:="S:\Admin\AP\Analytical\AReports Templates\Nutpan + Label Format.dot" '<---this is the location of your documents. mine are on a share drive.
'your documents could be on your local drive "c:\word\test.doc"
Set oApp = Nothing '<---close word app
Case "Amylose.dot"
Set oApp = CreateObject("Word.Application"
oApp.Visible = True
oApp.Activate
'Open document
oApp.Documents.Add Template:="S:\Admin\AP\Analytical\AReports Templates\Amylose.dot"
Set oApp = Nothing
I am not sure whats up with your code. It works for me. I built a scenario using your code. The only thing I changed was the path to the template.
One way to find out whats happening is to use some good 'ol debugging. Insert debug.print in a few locations of the code so you can see what is being sent over. Also, when I have problems, I use messageboxes to at least tell me if the code is trying to be read.
Here is my code:
'****begin code*****
Debug.Print Me.List0.Value '<--- tells me what the list box is passing to the code
Dim oApp As Object '<--declare variable
Select Case Me.List0 '<--using the Select Case is easier to me than using a bunch of If...Then statements
'Me.List0 is my demo listbox
Case "Nutpan + Label Format.dot" '<---my first word doc listing in the listbox
msgbox "NutPan!" '<---says nutpan if the select statement picks up my first choice
Set oApp = CreateObject("Word.Application" '<---create Word object
oApp.Visible = True '<---make Word visible
oApp.Activate '<---Activate Word Doc
'Open document
oApp.Documents.Add Template:="I:\electronic interfaces\ESCMAD\letters\Nutpan + Label Format.dot" '<---this is the location of your documents. mine are on a share drive.
'your documents could be on your local drive "c:\word\test.doc"
Set oApp = Nothing '<---close word app
Case "Amylose.dot"
msgbox "Amy!" '<---messagebox for second choice
Set oApp = CreateObject("Word.Application"
oApp.Visible = True
oApp.Activate
'Open document
oApp.Documents.Add Template:="I:\electronic interfaces\ESCMAD\letters\Amylose.dot"
Set oApp = Nothing
Case Else '<---lets you know if none of your choices above were used by the select statement
msgbox "oops. try again."
End Select 'end your select statement
'*****end code****
If you havent used the debug window, it can be activated by ctrl+G or View>Immediate window. This is where the debugger will print what you have asked.
Thanks a lot for your help. Your suggestion about the debugging tools and your code has really helped me go a long way with my database. When I added the code Debug.Print the immediate window showed me 1. So I changed the code to Case 1 and Case 2 and it works now. Once again thanks a lot, you have been a great help to me.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.