I am attempting to change the Source
property of a query in an Excel file via PowerShell. I have found the Connections
properties, but am unable to locate the specific property that contains the Source
reference to the filename.
Example of the current configuration:
We want the filename to be able to be respective of the current datetime, so there's a need to change the File.Contents
FQPN on a daily basis. I am familiar with replacing a property's data, I just can't find the location where this is stored.
I have also checked the properties within QueryTable
on the sheets as well as the properties of Connections
in the Workbook, but I am still missing where this Source
is available to modify.
Small sample, wherein the missing section I am not clear about is marked as #
comments, prior to refreshing all data sources.
Add-Type -ASSEMBLY "Microsoft.Office.Interop.Excel" | Out-Null
$xlObject = New-Object -ComObject Excel.Application
$fileOpen = 'C:\TEST\PATH\DATAREFRESHFILE.XLSX'
$wbObject = $xlObject.Workbooks.Open($fileOpen)
$shDataSh = $wbObject.Worksheets.Item('Data')
#here we would locate the aforementioned string and adjust the 'Source' filename prior to doing a refresh
$wbObject.RefreshAll()
= Excel.Workbook(File.Contents("C:\TEST\PATH\FILE " & Date.ToText(Date.From(DateTime.LocalNow()),"yyyy-MM-dd") & ".xlsx"), null, true)
for todays date, or= Excel.Workbook(File.Contents("C:\TEST\PATH\FILE " & Date.ToText(Date.AddDays(Date.From(DateTime.LocalNow()),-1),"yyyy-MM-dd") & ".xlsx"), null, true)
for yesterdays date (change the -1 to change the date).