Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open word document in Access 1

Status
Not open for further replies.

aradha

Technical User
Dec 4, 2002
15
0
0
AU
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.
 
Hi aradha,

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 &quot;word1&quot; '<---my first word doc listing in the listbox

Set oApp = CreateObject(&quot;Word.Application&quot;) '<---create Word object
oApp.Visible = True '<---make Word visible
oApp.Activate '<---Activate Word Doc
'Open document
oApp.Documents.Add Template:=&quot;\\gaf5142tnscs001\shared\electronic interfaces\ESCMAD\letters\Agreement Cover Letter.dot&quot; '<---this is the location of your documents. mine are on a share drive.
'your documents could be on your local drive &quot;c:\word\test.doc&quot;

Set oApp = Nothing <---close word app

'see notes above and duplicate below for each word doc

Case &quot;word2&quot;

Set oApp = CreateObject(&quot;Word.Application&quot;)
oApp.Visible = True
oApp.Activate
'Open document
oApp.Documents.Add Template:=&quot;\\gaf5142tnscs001\shared\electronic interfaces\ESCMAD\letters\suspend.dot&quot;
Set oApp = Nothing

End Select 'end your select statement

End Sub
'*******end code*****

Hope this helps. Let me know. Have A Great Day!!!,

Nathan
Senior Test Lead
 
Hi nathan1967

Thank a lot. I will try it and let you know how it goes

aradha
 
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.

Thanks a lot once again.

Radha.
 
Hi Radha,

I was curious how it was going. I guess now I know. Lets see what I can do to help.

I need to see a few things so I can &quot;see&quot; what you are doing.

Please post your code, the name of your list box, and what names you have in the list box.

I will be glad to take a look at it.

Have A Great Day!!!,

Nathan
Senior Test Lead
 
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 &quot;Nutpan + Label Format.dot&quot; '<---my first word doc listing in the listbox
Set oApp = CreateObject(&quot;Word.Application&quot;) '<---create Word object
oApp.Visible = True '<---make Word visible
oApp.Activate '<---Activate Word Doc
'Open document
oApp.Documents.Add Template:=&quot;S:\Admin\AP\Analytical\AReports Templates\Nutpan + Label Format.dot&quot; '<---this is the location of your documents. mine are on a share drive.
'your documents could be on your local drive &quot;c:\word\test.doc&quot;
Set oApp = Nothing '<---close word app


Case &quot;Amylose.dot&quot;

Set oApp = CreateObject(&quot;Word.Application&quot;)
oApp.Visible = True
oApp.Activate
'Open document
oApp.Documents.Add Template:=&quot;S:\Admin\AP\Analytical\AReports Templates\Amylose.dot&quot;
Set oApp = Nothing

End Select 'end your select statement

End Sub


Thanks a lot

Radha.
 
Hi Radha,

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 &quot;Nutpan + Label Format.dot&quot; '<---my first word doc listing in the listbox
msgbox &quot;NutPan!&quot; '<---says nutpan if the select statement picks up my first choice
Set oApp = CreateObject(&quot;Word.Application&quot;) '<---create Word object
oApp.Visible = True '<---make Word visible
oApp.Activate '<---Activate Word Doc
'Open document
oApp.Documents.Add Template:=&quot;I:\electronic interfaces\ESCMAD\letters\Nutpan + Label Format.dot&quot; '<---this is the location of your documents. mine are on a share drive.
'your documents could be on your local drive &quot;c:\word\test.doc&quot;
Set oApp = Nothing '<---close word app


Case &quot;Amylose.dot&quot;
msgbox &quot;Amy!&quot; '<---messagebox for second choice
Set oApp = CreateObject(&quot;Word.Application&quot;)
oApp.Visible = True
oApp.Activate
'Open document
oApp.Documents.Add Template:=&quot;I:\electronic interfaces\ESCMAD\letters\Amylose.dot&quot;
Set oApp = Nothing

Case Else '<---lets you know if none of your choices above were used by the select statement

msgbox &quot;oops. try again.&quot;

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.

Let me know what you are seeing.

Have A Great Day!!!,

Nathan
Senior Test Lead
 
Hi Nathan

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.

Radha.
 
Hi aradha,

I am very glad I could help you. [smile]

Have A Great Day!!!,

Nathan
Senior Test Lead
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top