Excel - refresh the dataset and save from VBS

A client called me today and asked for something that I'd not considered before - he had Excel spreadsheets that are connected to a data source, he wants to refresh them and email them out nightly 

*** the email recipients are outside the domain and will not have a connection to the data souce

 

The article below describes how I did this

Just like anything else, it was hours to research and try different solutions, but in the end it was just a few lines of code. 

I tried initially to do this from a macro in Excel, but I could never get it to save the data. (yes, I coded 'refresh' and 'save'... it just didn't work)

This code works. Save it as a 'vbs' (vbscript) file and run it. 

Also, be sure to uncheck 'enable background refresh' on ALL the connections

Set xl  = CreateObject("Excel.Application")
xl.Visible = True
 
Set wb = xl.Workbooks.Open("c:\temp\Book1.xlsx")
wb.RefreshAll
wb.Save
wb.Close
 
 
xl.Quit

RealWorldCode gives developers practical, real‑world solutions with clean, working code — no fluff, no theory, just answers.
Links
Home
Knowledge Areas
Sitemap
Contact
Et cetera
Privacy Policy
Terms and Conditions
Cookie Preferences