Opening Excel Spreadsheet from Access Macro

herbc0704

Board Regular
Joined
Jun 22, 2009
Messages
100
I created a macro that runs a query and transfers the information to an excel file. How can I have this macro open the excel file once the transfer has ben completed.
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
This is a pretty basic example, but you can fill it out as you need.

Function OpenExcelFromAccess()
Dim MyXL As Object

Set MyXL = CreateObject("Excel.Application")
MyXL.Workbooks.Open "C:\SomeFolder\SomeFile.xls"

End Function
 
Upvote 0
Andiemac,

Thanks. However when I run this. The excel file does not open. If I manually open the file it says its in use. What am I missing. I am using Excel 2007 and Access 2007. The excel file to be opened is in .xlsx format.
 
Upvote 0
You will need to change the path and file name to exactly where and what to open. The C:\SomeFolder\SomeFile.xls" was a sample to go by. Since you have in your Access a function that transfers your information to an excel file, use that same path and name, and end it with xlsx since that the file type.
 
Upvote 0
I believe the application instance you have created with CreateObject is hidden.

Code:
Function OpenExcelFromAccess()
    Dim MyXL As Object

    Set MyXL = CreateObject("Excel.Application")
    With MyXL
        [B].Application.Visible = True[/B]
        .Workbooks.Open "C:\SomeFolder\SomeFile.xls"
    End With
End Function
 
Upvote 0
Another way could be to use Shell function:

Code:
Public Function(byVal strFile as String)
    VBA.Shell "EXCEL.EXE " & Chr$(34) & strFile & Chr$(34), vbNormalFocus
End Function
 
Upvote 0

Forum statistics

Threads
1,224,545
Messages
6,179,432
Members
452,915
Latest member
hannnahheileen

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top