How do I close the Excel background process with VBA? - excel

I have looked at other questions similar to mine and it seems like I am missing something. I have isolated my code to isolate the problem. My code is below. I am simply opening an Excel workbook and closing it. For some reason, the background Excel process in Task Manager lingers and I cannot close it with VBA code and have to do it manually. I have stepped through the code and the background process opens after line 3 is executed. When I have lines 4-6 commented out, the background process closes fine. I have seen lines of code that opens a command prompt to kill the process but that closes all Excel workbooks open as opposed to just the active workbook.
Dim oExcel As Object
Dim oWB As Object
Set oExcel = CreateObject("Excel.Application")
Set oWB = oExcel.Workbooks.Open("C:\Automation\IO List.xlsm")
oWB.Close True
Set oWB = Nothing
oExcel.Quit
Set oExcel = Nothing
Task manager processes

Related

External data does not load in excel when scheduled from scheduled task

The problem is I made a script that opens and refreshes data in my excel.
The data is external data from a PHD server I get this data with a plugin installed on excel.
All works fine untill I try to schedule the script with task scheduler. when I schedule it as "run only when user is logged on" it workes fine. but when I try to schedule it as " run wheter user is logged on or not" it does not update the data.
(it does open the excel and saves it but no changes in the data)
It is set on a VM that acts as a desktop and the updates need to be done even when i'm logged of.
The code of the script: (that works fine when i manualy launch it)
Dim oExcel
Set oExcel = CreateObject("Excel.Application") 'launch excel.
oExcel.Visible = True ' makes the aplication visible (if not set to true the data won't be updated)
oExcel.DisplayAlerts = False' disables all excel allerts.
oExcel.AskToUpdateLinks = False 'now excel will not ask you to update links.
oExcel.AlertBeforeOverwriting = False 'excel will not display an alert before overwriting data in a cell.
code for the plugin to update data
Dim addIn
addIn = COMAddIn
Dim automationObject
automationObject = Object
Set addIn = oExcel.COMAddIns("ExcelCompanion")
Set automationObject = addIn.Object
automationObject.UNIF_workbook_refresh
code to save and close excel
oWorkbook.RefreshAll 'refreshes the workbook
oWorkbook.Save 'saves the updated workoob
oWorkbook.Final = True 'makes the file read-only
oExcel.Quit 'exits excel
Set oWorkbook = Nothing ' destroy the object (minimises damage if the object goes out of scope)
Set oExcel = Nothing ' destroy the object (minimises damage if the object goes out of scope)
these 3 block of code form the script.
Hencky!
Which version of Windows do you have? I had the same problem about a week ago, and I came across a very strange solution... :)
Try adding a folder into C:\Windows\SysWOW64\config\systemprofile, simply named "Desktop"... If you run a 32-bit version of Windows, do the same thing except the path must contain system32 instead of SysWOW64...
The "run wheter user is logged on or not" will now do the work... If you had a logging function in your script, you would've seen that the script stops somewhere arround Excel commands... I don't know why is that so, it simply is...

Unknown runtime error when running macro

I needed a way to schedule automatically opening an Excel file, refresh the contents then save and close it.
I have done this before but I can no longer open the file as opening it causes the macro that refreshes then saves and closes the file to run.
I considered VBScript after trying several ways of doing it. I found this code on here.
Dim objExcel, objWorkbook
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("C:\...\Finances.xlsm")
objExcel.Visible = True
objExcel.Run "Refresh"
objExcel.Quit
Set objWorkbook = Nothing
Set objExcel = Nothing
WScript.Quit
The VBScript code opens the Excel file and triggers the macro Refresh inside. The macro refreshes the data then saves and closes the file.
But I'm getting the following error despite the code seeming to run ok.
If you don't want anything to run automatically when you open the workbook, put the line
objExcel.EnableEvents = False
before you open the workbook. You could set it back to True later if you need to. You should still be able to run Refresh. Whether or not that will fix your unknown error, I don't know.
You are quitting the excel.application object without closing the objWorkbook open workbook. Saved or not, this is going to generate an error crashing out of the open workbook.

Open Excel and Place on top of windows using VBS

I am currently using the below script to open Excel. The script does as it suppose to but what i need it to do is place excel on top of all windows. I currently had a script within the excel project to place userforms ontop of all windows but when i get to a certain point within my project it causes excel to crash, so i had pinpointed it to the script within the excel project. here is the script i am using to open excel file:
Dim AppExcel
Set AppExcel = CreateObject("Excel.application")
AppExcel.Workbooks.Open "C:\Users\****\Desktop\ App Tool\App.xlsm"
AppExcel.Visible = False
Based on comment to minimize every open window and then opening Excel.
Set shell = wscript.CreateObject("Shell.Application")
Shell.MinimizeAll ''Comment me if you don't want to minimize everything
'Then rest of your code
Dim AppExcel
Set AppExcel = CreateObject("Excel.application")
AppExcel.Workbooks.Open "C:\Users\****\Desktop\ App Tool\App.xlsm"
AppExcel.Visible = True
'And in the end Release the Shell object
Set shell = Nothing

Refreshing an excel pivot table using windows scheduler

I am responsible for updating an Excel spreadsheet which pulls its information from an Access database on a daily basis. All the data that i need for my excel spreadsheet is available for me and all that i need to do is open the document, provide the password, enable to content and click the refresh button.
The database is very large and updating this during normal working hours causes problems as it slows down other users on the network. How would i use Windows Scheduler to do this for me outside of working hours? I'm not sure how to set up my script to follow my steps required.
I've had to do something quite similar to this recently, and with the help of this forum I've found something that works for me, and by the sounds of it may work for you too!
I created a notepad file with the following .vbs script
Dim oExcel
Set oExcel = CreateObject("Excel.Application")
oExcel.Visible = True
oExcel.DisplayAlerts = False
oExcel.AskToUpdateLinks = False
oExcel.AlertBeforeOverwriting = False
Set oWorkbook = oExcel.Workbooks.Open("Full Path of your file.xlsx")
oWorkbook.RefreshAll
oWorkbook.Save
oExcel.Quit
Set oWorkbook = Nothing
Set oExcel = Nothing
What this does, it opens the file, refreshes any data connections, then saves the file and exits.
I then put this as a scheduled task to run at an off peak time, so that when the user opens the workbook, it's up to date.
I hope this helps!
I managed to achieve this through the VBA
hit Alt - F11
right click ThisWorkbook and click view code.
the code is as follows:
Private Sub Workbook_Open()
Workbooks.Open ("location of your workbook"), Password:="whatever your password is"
ThisWorkbook.RefreshAll
End Sub
i save this document and ask the task scheduler to run it at a specific time.

VB script unable to access file

I am trying to schedule a vba script. I am working through the company server and am wondering if I can not access this due to administrative restrictions? here is the code:
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("\\Computer\denfs1\data\1EpUSRgltry\Permitting - Wells & Sundries\WEST (CO-MT-ND)\Schedules & Status\Planning and Permiting Spread sheet(West) Permit and Planning spread sheet.xlsm", 0, True)
xlApp.Run "Click"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
Here is the error I am getting
What does the macro "Click" do? If it make changes, close it without saving (change to True if you want to save changes by the Click macro).
xlBook.Close False
You may also want to debug from Excel by putting xlApp.Visible = True before opening the .xlsm workbook.
There might be a hidden Excel.exe locking that same file from previous script execution (say you press Ctrl-C before the macro "Click" finishes). Close all visible Excel windows and ensure there are no Excel.exe running in task manager.
If you keep pressing Cancel when Excel asks you to save changes to it, it will stuck in background even the vbs did not pick up any error.
The problem was a syntax error with the name of the file... Make sure you have the whole string accurate :/

Resources