Opening Excel in a more stable manner

Kemidan2014

Board Regular
Joined
Apr 4, 2022
Messages
229
Office Version
  1. 365
Platform
  1. Windows
I am having issues with this code working and not completely working. its a 50 50 chance that the file remains open. Sometimes when executing the code via button click in access it executes WITH OUT excel remaining visible and thus puts one of those faded ~"filename" icons on my desktop signifying that excel is open but not visible even those i am specifying it int he code to be visible.

is there a slightly different way i should be opening excel in order to perform this? the transferspeadsheet works perfectly and does not overwrite the other tabs in the file i just wish it would open reliably.

VBA Code:
Private Sub Qualitymetrics_Click()
'Declare stuff
Dim excelapp As Object
Dim wb As Object
Dim openpath As String
openpath = "C:\users\desktop\ComplaintMetricsQuery.xlsx" 'Set file path
Set excelapp = CreateObject("Excel.Application")
 DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12, "ComplaintMetricsQuery", "C:\users\desktop\ComplaintMetricsQuery.xlsx", True

Set wb = Excel.Workbooks.Open(openpath)
excelapp.Visible = True
'wb.worksheets("QueryDump").Range("A1:K100").ClearContents
wb.Save
'wb.Close


End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
Not sure why you are mixing variable and strings?, always potential for mistakes there? :(

I would have thought this would not even work?

Set wb = Excel.Workbooks.Open(openpath) as the object is ExcelApp ??
 
Upvote 0
It will work but it will create an implicit Excel.Application variable which is quite likely at least part of the problem. ;)
 
Upvote 0
I guess im still a little ignorant as to the difference between late binding and early binding. but i did switch it up a bit and added a message box and excel opens each time now but to Rory's point it does appear to leave Excel as an open process. when i add
'Excelapp = Nothing' at the end of the code the code errors out at "wb= ..." saying theres nothing there.

what other way to open an existing file is there?
 
Upvote 0
As suggested, you should be using:

VBA Code:
Set wb = excelapp.Workbooks.Open(openpath)

and not:

VBA Code:
Set wb = Excel.Workbooks.Open(openpath)
 
Upvote 0
Solution
That works, however in my task manager it opens the file and i have 2 listings of excel 1 in the "Apps" and 1 as a background process. closing the file opened just closes the "Apps" listing.

if i open any other excel file as you normally would, i dont see the appearance of excel as a background process
 
Upvote 0
It shouldn't do, unless you have other similarly unqualified code that you haven't shown. (I'm assuming you already killed any previously created background processes from your original code)
 
Upvote 0
Sorry Rory, I am blind and missed the correct line that needed to be changed, The post i marked as solution did the trick!
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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