I need to run automatically an excel macro that PDFs a monthly report and then saves and closes the excel file. My proposed solution is that using Windows Task Scheduler it runs a windows batch print file that runs a VBScript which then runs the excel file. The excel file when it is open then runs a macro that PDFs what I need. All of this I have tested many times locally on my PC and all of it works fine. Now this all needs to run on a remote desktop that is a virtual machine. This is where the issue happens. Everything runs fine up until it opens the excel file, the excel file opens in the background and its icon on the task bar flashes orange, because of this nothing else happens. The macro breaks and does not print, it doesn't even start in the first place. Is there any ideas on how to fix this? Here is the coding for the VBScript that opens the excel file.
Dim args, objExcel, openExcel
Set args = wScript.Arguments
Set objExcel = CreateObject("Excel.Application")
objExcel.Workbooks.Open args(0)
objExcel.Visible = True
objExcel.Run "ReportPrint"
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close(0)
objExcel.Quit
Related
I have the following simple VBScript for updating an Excel workbook.
Dim objExcel
Set objExcel = CreateObject("Excel.application")
objExcel.DisplayAlerts = False
objExcel.Visible = False
objExcel.Workbooks.Open "path\myworkbook.xlsx"
objExcel.ActiveWorkbook.RefreshAll
objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
When I use path to local excel file like C:\Users\sbrbot\Documents\myworkbook.xlsx the script works and updates my excel workbook fine. But when I use the remote path like \\sharepoint\library\myworkbook.xlsx then script executes without errors (so path is correct) but the workbook is not really updated! (SharePoint version 2013). Even if I map SharePoint's remote server path \\sharepoint\library to logical M:\ drive, and my path is M:\myworkbook.xlsx the same is hapening, it does not update workbook. Seems like SharePoint refuses to update file. Path is correct, that's not the problem.
I have to mention one strange bahaviour with SharePoint. When I manually open Excel file from SharePoint remote directory, make some changes in it, and save the file - in Excel's titlebar it is reported as Saved. Immediately after this when I go to close this Excel file, Excel asks me to save it again although I did not change anything inside the file.
I have a large Excel file that uses PowerPivot to connect to several external data sources. The Excel file is stored in a SharePoint folder. I would like to be able to refresh the connections automatically overnight. To do this, I am using Windows Task Scheduler to trigger a VBScript each day at 2AM.
Previously, I had the script run a macro within the workbook to refresh all. This worked successfully until my organization recently updated to Microsoft Office 365 ProPlus. I am now struggling with the new read-only default setting for SharePoint. Following the update, whenever I open an Excel file from SharePoint, I see a yellow bar reading "Read-Only: We opened this workbook read-only from the server. Edit Workbook". When working with files manually, this has to be clicked before changes can be saved. I tried to research the programmatic workaround for this and found the recommendation to use the method .LockServerFile.
To streamline the trouble-shooting, I have tried to move the entire action into the VBScript script, rather than running a macro within the workbook. My code is below.
When I watch the process, the yellow "Read-Only" bar appears to pop up immediately before the save. I can run the process once with no apparent errors, but if I then manually open the file, I don't see the option to "Edit Workbook". If I manually change the file, save, and then run the script again, I get the error 800A03EC.
It seems that somehow the script is failing to correctly "release" the lock on the server file after it finishes.
Is there a way to fix this?
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("[[The Sharepoint Path]]")
objExcel.Application.DisplayAlerts = False
objExcel.Application.Visible = True
objExcel.ActiveWorkbook.LockServerFile
objExcel.ActiveWorkbook.EnableConnections
objExcel.ActiveWorkbook.Sheets(1).Range("P2").Value = Date
objExcel.ActiveWorkbook.RefreshAll
objExcel.ActiveWorkbook.Save
objExcel.ActiveWorkbook.Close false
objExcel.Application.Quit
set objworkbook = Nothing
set objExcel = Nothing
WScript.Quit
Under this line of code, Workbooks.Open(previous_absolute_path) freezes Excel most of the time, giving no error message:
Set current_workbook = Workbooks(get_file_name(current_absolute_path))
The surounding code:
If IsWorkBookOpen((current_absolute_path)) = False Then
If previous_absolute_path <> "" And previous_absolute_path <> current_absolute_path Then
Workbooks(get_file_name(previous_absolute_path)).Close True
End If
Set current_workbook = Workbooks.Open(current_absolute_path)
previous_absolute_path = current_absolute_path
Else
Set current_workbook = Workbooks(get_file_name(current_absolute_path)) 'Causes Fail!
End If
current_absolute_path is a valid file path that is absolute. It seems every time the code runs there are windows at least being opened. However, in the newly opened workbook, no lines of code are ever executed in itsworkbook_open() from what I can tell from putting a message box as the first line of code ofworkbook_open() and the message box never appears.
The code does work, if and only if I have the workbook running this macro open, and before I run that macro, I manually open the workbook at current_absolute_path. I close that file and run the macro and it works without any problems.
current_absolute_path, is a confirmed valid file path. The line of code can open other workbooks within the same folder. Excel allows all macros and "trusts" files from the internet at this point. The folder of the workbook being opened is not protected.. and I do not have the skills yet to know where the problem is. What must be done?
EDIT: It's not manually opening the workbook that causes the Excel macro to run successfully the next time, specifically its manually closing it.
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...
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.