I have an Excel file that want to update and save automatically with out having to open it or manually interact with. Manually, I open the file up and hit data refresh which goes and does a SQL query and then hit F9 for the formulas to update and then I just close/save.

(I then would mail the file out to people using a perl script or use SAS JMP to run some numbers/charts and also mail them out. Basically I need to script some things which require the XLS file to be updated.)

link|improve this question

80% accept rate
feedback

4 Answers

My work-around is to record a Macro in the Excel file (so you have to use .xlsm file extension) :

Sub AutoUpdate()
'
' AutoUpdate Macro
' data refresh from MSQuery connection

'
    Sheets("Feuill1").Select
    Selection.ListObject.QueryTable.Refresh BackgroundQuery:=False
End Sub

Then i use a Powershell script that do the trick by : opening the Excel file, calling the macro, save and close the file. In this example i make a copy of the original file for security/backup purpose, but of course you could save the original file itself.

$ObjetExcel = new-object -c excel.application
$ObjetExcel.displayAlerts = $false # don't prompt the user
#$ObjetExcel.visible = $True;
$Classeur = $ObjetExcel.workbooks.open([source_filepath], $null, $true)
$ObjetExcel.run("AutoUpdate")
# $ObjetExcel.run("RemoveODBC") # another custom macro for removing data connexion
$Classeur.saveas([destination_filepath])
$Classeur.close($false)
#$ObjetExcel.visible = $False;
$ObjetExcel.quit()
spps -n excel

So my Excel report is refreshed on a daily basis, without any manual intervention, by a Windows Planified task who called the above script.

link|improve this answer
feedback

You can manipulate Excel files with AutoIt, here is a library for doing so. You might even be able to use AutoIt to automate the SQL query and pass the data to the excel file (depending on the database engine you use). AutoIt can even do the email as well.

link|improve this answer
feedback
up vote 0 down vote accepted

I ended up finding Win32::Excel::Refresh and it fits the bill. It's a perl mod but it also comes with a windows executable that you can run if you don't know/want to deal with Perl.

link|improve this answer
feedback

Found this code on another site but it's very useful and thought I'd post it here. Just save the code below as a .VBS file and then you can execute it like an exe.

http://stackoverflow.com/questions/9796180/refresh-excel-using-ssis-script-task

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.