Pull up Excel or Word Doc

S

Supe

I am working in a database that someone else had created where if you select
a customer from a list and hit a button it bring up that file for you. From
the code I have attached he had it set up to pull Excel files. Is there a
way to change this code to where it would pull either Excel or Word files?

Private Sub cmdStoreList_Click()
On Error GoTo errhandler
Dim oBook As Object
If cnt = 0 Then
If cust = "" Then
MsgBox "File Error. Please select a customer, then try again."
Exit Sub
Else
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open("S:\Stardust Planner\Store
Lists\" & cust & ".xls")
oExcel.Visible = True
cnt = 1
End If
Else
oExcel.Quit
If cust = "" Then
MsgBox "File Error. Please select a customer, then try again."
Exit Sub
Else
Set oExcel = CreateObject("Excel.Application")
Set oBook = oExcel.Workbooks.Open("S:\Stardust Planner\Store
Lists\" & cust & ".xls")
oExcel.Visible = True
cnt = 1
End If
End If
errhandler:
If Err.Number = 1004 Then
MsgBox "File is not available at the moment. Please make sure that
it is present and try again.", vbInformation, "Information"
End If
End Sub
 
D

Douglas J. Steele

If all you're trying to do is open the workbook or document, there's no need
to use Automation.

A simple

Private Sub cmdStoreList_Click()
On Error GoTo errhandler
If cnt = 0 Then
If cust = "" Then
MsgBox "File Error. Please select a customer, then try again."
Exit Sub
Else
Application.FollowHyperlink "S:\Stardust Planner\Store Lists\" &
cust & ".xls"
cnt = 1
End If
Else
If cust = "" Then
MsgBox "File Error. Please select a customer, then try again."
Exit Sub
Else
Application.FollowHyperlink "S:\Stardust Planner\Store Lists\" &
cust & ".xls"
cnt = 1
End If
End If
errhandler:
If Err.Number = 1004 Then
MsgBox "File is not available at the moment. Please make sure that
it is present and try again.", vbInformation, "Information"
End If
End Sub

Pointing to a Word document works exactly the same way.

Incidentally, there appears to be no difference in your code whether cnt is
0 or not. Why bother checking?
 
S

Supe

I know nothing about coding so not sure why they appear to be the same.

From what I get from his code it is set up where if I pick Cub West, it
looks for the Excel file named Cub West in folder S:\Stardust Planner\Store
Lists and pulls that up. If I were to pick Shoppers from the list, it would
look for the Excel filed named Shoppers in that folder and pull that up. I'm
getting some Store Lists in Word so it won't pull them up since the code is
set up to look upx .xls files.
 
S

Supe

I know nothing about coding so not sure where he has that in there twice.

His code appear to be set up where when you click on a customer name, it
looks for the file that has that Customer Name.xls. So the report for the
customer Cub West would have a report Cub West.xls. The customer Shoppers
would have a report Shoppers.xls and so on. Sure when he set this up he only
had Excel reports. Now I'm starting to get Word reports as well so need it
to be able to view those too.
 

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