I have to credit "vbslammer" with the following, which worked great for me with respect to opening an Excel Document from Access (although I am still struggling with working with an already open Excel document).
Public Sub OpenExcel()
On Error Resume Next
Dim xl As Excel.Application
Dim wb As Excel.Workbook
Dim sht As Excel.Worksheet
Set xl = New Excel.Application 'instantiate Excel
xl.Visible = True
' use Excel's built-in open dialog
xl.Dialogs(xlDialogOpen).Show "C:\*.xls"
Set wb = xl.ActiveWorkbook
' put appropriate sheet name/index here.
Set sht = wb.Sheets(1)
' tinker with range properties
With sht
With .Range("E20", "G20"
.Merge
.Value = "Merged Cells"
.HorizontalAlignment = xlHAlignCenter
.Font = "Comic Sans"
.Font.Size = 18
.Font.Bold = True
.Interior.ColorIndex = 27
End With
.Activate
End With
I use the statement below to open the spreadsheet; I have queries on my spreadsheet that are set to refire and refresh data when it is open, I suppose you can do the same with a macro.
BY GEORGE I'VE GOT IT...
The easy way to do this is to create a button with a hyperlink to the excel file you want to open. Then make visible-false, then if using a macro that runs a bunch of queries like I'm doing to update a linked excel spreadsheet, then add a GOTO CONTROL for that button created and SENDKEYS=~. After all the update queries have run it will go to that button then enter(activates it) and it will then open up that excel spreadsheet,
Be careful with the sendkeys. If you are not active on the program this will not work.
Say you click on the command button to run your code, while this is running you click over to outlook to read your email, when the code hits the spot to sendkeys it will send them to the application that is active.
I was reading your response and notice you stated "although I am still struggling with working with an already open Excel document". Here is an example that first tries to use the open Excel application, if Excel is not open, it will create a new instance of it.
'*****example*****
Set objXL = GetObject(, "Excel.Application"
If Err.Number <> 0 Then
Set objXL = CreateObject("Excel.Application"
End If
'****end example****
This will allow u to use an Excel file that is already open.
try using "docmd.TransferSpreadsheet" function. Go to the VBA help for this function to get arguments. The only downside is you have to know the name and or path of the xls book
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.