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
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 SharePoint 2010 and Office365.
I want an Excel spreadsheet, which has data from a SharePoint list to auto refresh every 1 min.
I tried to follow the procedure from the following blog
Here is the VBS code that I'm using:
Set xl = CreateObject("Excel.Application")
set wb = xl.WorkBooks.open("\\SPREADSHEET LOCATION\Sharepoint Pivot.xlsx",,TRUE)
xl.DisplayAlerts = False
WScript.Sleep 1000
wb.RefreshAll
wb.Save
wb.Close
xl.Quit
Then I created a task in Task Scheduler to run every minute.
Monitoring the progress in Task Manager, I can see that the file is being opened and it seems to work, but the data is not getting refreshed.
I have a feeling that this is because when the file is opened from SharePoint it's opening with the "Read-Only" message.
I thought that the code is taking care of that, but maybe it's not.
I wanted to debug the code by running one step at a time but I dont have access to cscript.exe
Am I doing something wrong here? I've looked around trying to find a solution but everyone seems to have the same code as above.
Thank you
I had a similar problem but found a solution - using .LockServerFile. See example code:
set xl = CreateObject("Excel.Application")
xl.Application.DisplayAlerts=False
set wb = xl.WorkBooks.open("location of excel file goes here")
WScript.Sleep 1000
xl.ActiveWorkbook.LockServerFile (only use if location is in Sharepoint to turn off edit mode)
wb.RefreshAll
wb.Save
xl.Application.DisplayAlerts=True
wb.Close
xl.Quit
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 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.
I've got a project where I'm reading data from an Excel worksheet and saving it in Access tables (not a direct import--see this question if you're interested). My current problem is that any time I run my "import" and then try to open the workbook in Excel, it's "locked for editing" unless/until I close Access. I don't think this should be the case. My process is
Open the workbook with Automation.
Build a collection of sheet names.
Release the Automation objects.
If there's more than one sheet, get user input on which to process.
Open an ADO recordset on a specific range & read some data.
Release the recordset & connection.
Open an ADO recordset on a different specific range & read a bunch o' data.
Release the recordset & connection.
Close the controling form.
Steps 1 - 4 and 9 live in the form file, the remainder in a module.
Am I missing something? I think I've released all the references to the workbook....
If your step #1 includes something like this:
Dim objExcel As New Excel.Application
And later releasing the object is this:
Set objExcel = Nothing
Trying including this line just before you set the object variable to Nothing:
objExcel.Quit
It also helps to make the Excel application instance visible after starting it so you're less likely to leave Excel running unseen:
objExcel.Visible = True