I have an excel file that I’m trying to open, refresh all of the cells (which pull data from an excel plug-in) and then save the file down as a new file. I’m trying to run it a few times a day though and overwrite the existing new file.
Is there a way to overwrite the existing file, without Excel prompting me that the file already exists and I have to click through the prompt? I’m planning on using Task Scheduler to run this code 3x a day at recurring times.
Also, does PowerShell have some sort of wait function that allows the sheet to fully update before it is saved? The original file has 10,000 pieces of data being pulled, so it takes a while to update.
code below:
$file = 'original path'
$x1 = New-Object -ComObject "Excel.Application"
$x1.Visible = $false
$x1.DisplayAlerts - $false
$enddate = (Get-Date).tostring("dd-MM-yy")
$filename = 'newpath'+.'xlsx'
$wb = $x1.workbooks.Open($file)
$wb.refreshAll()
$wb.SaveAs($filename)
$wb.Close()
$x1.Quit()
Remove-Variable wb,x1