VBA ACCESS opening an Excel file

glaya Posted messages 2 Status Membre -  
 VBA Beginner XX -
Hello everyone,

here is my problem:
in my database I would like to open an Excel file from a button
what is the code that allows me to do this

Thank you for your help
Glaya

7 réponses

michelh Posted messages 16 Status Membre 25
 
To respond to Fred:
First, you need to add a reference to Excel to have access to Excel objects. To do this, you need to open a code module and then use the Tools menu, References, and search in the list for: Microsoft Excel 8 (for example).

Then, here is an example of code (for the button cmdOpenExcel):

Private Sub cmdOpenExcel_Click()
Dim xls As Excel.Application
On Error GoTo errHnd
Set xls = CreateObject("Excel.Application")
xls.Workbooks.Open "c:\toto.xls"
xls.Visible = True
Exit Sub
errHnd:
MsgBox "Error No. " & Err.Number & vbLf & Err.Description, , Err.Source
End Sub
18
glaya Posted messages 2 Status Membre 2
 
Thank you for your help
your response will be very useful to me
2
marinaetsonchat
 
Hello,
The subject is old and I don't know if I will get a response.
The code above works well, except that it opens the Excel file twice...

Private Sub Bascule65_Click() Dim xls As Excel.Application On Error GoTo errHnd Set xls = CreateObject("Excel.Application") xls.Workbooks.Open "c:\AvisCom\Trimestriel.xls" xls.Workbooks("Trimestriel.xls").Sheets("Feuil2").Activate xls.Visible = True Exit Sub errHnd: MsgBox "Error No. " & Err.Number & vbLf & Err.Description, , Err.Source End Sub


Does anyone have an idea?
1
Polux31 Posted messages 7219 Status Membre 1 204
 
Hello,

Here is a method that can be used:
Private Sub Bascule65_Click() Dim xls As Excel.Application Dim wk As Excel.Workbook Dim ws As Excel.Worksheet On Error GoTo errHnd Set xls = CreateObject("Excel.Application") Set wk = xls.Workbooks.Open("C:\AvisCom\Trimestriel.xls") Set ws = wk.Sheets("Feuil2") ws.Activate xls.Visible = True Exit Sub errHnd: MsgBox "Error No. " & Err.Number & vbLf & Err.Description, , Err.Source End Sub 


Don't forget to reference: Microsoft Excel xx.x Object Library in Tools > References ...

;o)
--
“What is well conceived is clearly expressed, And the words to say it come easily.”
Nicolas Boileau
1
michelh Posted messages 16 Status Membre 25
 
Hello
There is a very simple way: create a button using the assistant: it’s a button from the Application category and the action is Run Microsoft Excel. The assistant will create the code to open Excel for you.
For the assistant to be active, the second button on the toolbar must be pressed down (the one after the arrow).
You can obviously do much better by opening a specific file or controlling Excel from ACCESS with VBA code. This is called OLE Automation.

Good luck
Michel
0
fred
 
Yes, your approach is good for opening an Excel workbook, but how do you open a specific file like toto.xls from Access?

Thanks in advance

fred
0
marinaetsonchat
 
This code works just as well but it still opens the Excel file twice.

In fact, the problem comes from the fact that I was using the click event of the toggle button. By using the 'Mouse Down' event, it only opens once.
0
VBA Beginner XX
 
I understood! A reference needed to be added (in tools) for Excel objects to be recognized from Access!
0