I have to present some statistical information on SharePoint using Excel Web Access web part. After a whole week of data consolidation from several different external sources, I have realized that the data connections does not get refreshed when SharePoint loads the web part. All the refreshing options that can be set in Excel (Excel 2007 in my case) does not help but giving more errors.
After hours of Google researching, many suggested to follow the steps set out in this article:
Plan external data connections for Excel Services
Surely it’s probably the proper way to do it, but I just don’t have the time to go through the million steps. Since I am more of a developer than a site administrator, I have decided to write a script to refresh the data connections in the Excel workbook instead. The script will be run on a daily basis which works for me in this case.
Here is the VBScript that I have created:
' VB Script Document
option explicit
refreshPivotTable("[file path][excel file name]")
Sub refreshPivotTable(strExcel)
' Load Excel object
Dim objExcel
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = False
objExcel.Application.DisplayAlerts = False
objExcel.Workbooks.Open strExcel
' Refresh PivotTables
Dim pt
Dim i
Dim nShCount
nShCount = objExcel.ActiveWorkbook.Sheets.Count
For i = 1 To nShCount
For Each pt In objExcel.ActiveWorkbook.Sheets(i).PivotTables
pt.RefreshTable()
Next
Next
' Close Excel object
objExcel.ActiveWorkbook.Save
objExcel.Quit
Set objExcel = Nothing
Schedule It
You can now schedule to run this using the Windows task scheduler. Run it as “cscript vbExcelScrip.vbs”
Data Source File
One thing to note that you might have to unlink the physical data source file with your Excel. A data source file is created by Excel (normally saved on your My Document > My Data Sources) when you set up a data connection. However, the connection information is in fact saved in your Excel, the connection file is used as a backup and reusable by other workbooks.
Depending on what user you are using to run the scheduled task, it might not have access to the data connection file on your My Document folder. To unlink the data connection file from your Excel workbook:
- go to Data > Connections
- Click the “Properties…” button
- Under the “Definition” tab
- Uncheck “Always use connection file”
Hopefully this issue is addressed in SharePoint 2010 and Office 2010. 🙂
1 comment on “Refreshing data connection in Excel using VBScript”