VB script unable to access file - excel

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 :/

Related

How do I close the Excel background process with VBA?

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

VBS to Open an Excel Workbook Not Displaying

I am trying to run a small VBS script to open an Excel workbook, but every time I run my script, Excel will only load as a background application (confirmed in Task Manager) but will not display. I feel like I am missing something very silly, but I have browsed form after form talking about VBS scripts launching Excel, and I can't seem to get any of them to work. Any input you might have would be greatly appreciated, thanks :)
Option Explicit
Dim xlApp, xlBook
Set xlApp = CreateObject("Excel.Application")
xlApp.DisplayAlerts = False
Set xlBook = xlApp.Workbooks.Open("FilePath", 0, True)
Application.Caption = "OnMax"
*Note I replaced the actual file path with just Filepath to make sure it fit and one line and no one had to scroll, but incase it matters the file path is: M:\Projects\Onmax II\Documents\R&D\Standalone App\VB Script Test\Test.xlsm
The script is doing exactly what you asked it to do it started an instance of Excel.
You just didn't tell it yet to make the application visible.
Adding the following code should help
xlApp.Visible = true

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.

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.

How can I use the task scheduler to run Excel Macro w. Windows 7?

I know there are other posts similar to this but I cannot seem to get this to work. I just want to create a .vbs file to use with the scheduler to kick off a macro in Excel on monthly basis. Here is what I have been trying:
Option Explicit
Dim xlApp
Dim xlBook
Set xlApp = CreateObject("Excel.Application")
Set xlBook = xlApp.Workbooks.Open("C:\Test\TEST.xlsm", 0, True)
xlApp.Run "Macro1"
xlBook.Close
xlApp.Quit
Set xlBook = Nothing
Set xlApp = Nothing
WScript.Quit
I just get a warning message every time saying that TEST.xlsm already exists and do I want to overwrite it. When I click Yes, the changes never take effect.
I don't want to get any message at all, and I don't know why the changes from the macro are not occurring.
I've tried to use
xlApp.SaveAs
and I cannot get that to work either. Plus the workbook appears to not be closing correctly.
Thanks in advance, I am new to this so maybe I am just missing something stupid.

Resources