I can not use VBA in Access to close an Excel background process that appears under certain circumstances..

Terry Richter 1 Reputation point
2021-12-28T13:42:26.237+00:00

I want to edit an Excel file using VBA in Access. I can open, save and close the Excel file with no problem. But if, for example, I add a sheet, although it works, I am left with a background Excel process that should not be there. So long as that process remains I will get errors when rerunning my code. I am trying to upload a docx file to show clearly my problem but the Upload botton does not seem to do anything.

0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 47,711 Reputation points
    2021-12-28T16:11:01.463+00:00

    When you're using the Office API you're working with COM and COM needs to be properly cleaned up. Even then there is a delay in case you start using it again. It sounds like you might not be cleaning up your COM objects properly. Can you post the code you're using?


  2. Mark Boucher 1 Reputation point
    2022-05-20T20:16:27.057+00:00

    Using a .bat called .vbs script, I was calling more than one .xlsm file and running up to two VBA macros with each workbook opening. This was done via one .bat file that was started from Task Scheduler. So... task scheduler starts the .bat which starts a .vbs which opens an .xlsm file and runs a couple macros that refresh data in the .xlsm and export to multiple .pdfs in a loop, then open another .xlsm and run some macros in it too and export more .pdfs.

    The EXCEL.EXE processes would stack up requiring manual or admin rights executed command window or .bat file "taskkill /F /IM excel.exe" commands. Nothing suggested in many forums seemed to get me out of the quagmire.

    All this was done in the background except I still see a "Publishing..." window when the exports to .pdfs occurs. I'm living with that. These process run hourly 24/7.

    This would not happen if I ran each macro manually. The EXCEL.EXE process would complete then disappear from Task Manager when I ran the macros or individual .vbs scripts.

    Solution:

    I put a

    timeout /s 5
    

    in the .bat file between the .vbs calls and

     newHour = Hour(Now())
     newMinute = Minute(Now())
     newSecond = Second(Now()) + 5
     waitTime = TimeSerial(newHour, newMinute, newSecond)
     Application.Wait waitTime
    

    in the macros between "big lifts" such as refreshing imported data or exporting to multiple .pdfs. All issues went away. (Crossing my fingers).

    My theory is that one Windows background processes would not quite complete before the next macro or .vbs call was started and this somehow kept Excel from closing.

    0 comments No comments