I have a workbook I created that has an external connection to another XLSX file. I created it by going to the Data tab, then New Query->From File->From Workbook. It created this as a Workbook connection rather than a Query table. By that I mean in VBA ActiveSheet.QueryTables.Count=0, but ThisWorkbook.Connections.Count=1.
Further, when I look at the Connection Properties definition, the Connection string is:
Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=page;Extended Properties=""
The command Type is SQL, and the Command text is SELECT * FROM [page]
The problem I have is this - when I copy both workbooks (the one with the external reference, and the one it is referencing) to another location (another computer), it cannot find the linked file, which is understandable. Normally, I would create a macro when the workbook opens to change the Connection string. However, I am not familiar with how this works for a OLEDB connection. The string says Data Source=$Workbook$, and no matter where I look in VBA, I cannot seem to find where that string is held.
I know it is saved with the workbook somewhere, because when I try to refresh, it tells me it cannot find the file and it displays the full path/name of the original location it was linked to.
I would have assumed the following VBA would work, but it only returns an empty string:
Is there anyone who can tell me how to use VBA to reset the file path & name for this connection?
Thanks in advance!
Further, when I look at the Connection Properties definition, the Connection string is:
Provider=Microsoft.Mashup.OleDb.1;Data Source=$Workbook$;Location=page;Extended Properties=""
The command Type is SQL, and the Command text is SELECT * FROM [page]
The problem I have is this - when I copy both workbooks (the one with the external reference, and the one it is referencing) to another location (another computer), it cannot find the linked file, which is understandable. Normally, I would create a macro when the workbook opens to change the Connection string. However, I am not familiar with how this works for a OLEDB connection. The string says Data Source=$Workbook$, and no matter where I look in VBA, I cannot seem to find where that string is held.
I know it is saved with the workbook somewhere, because when I try to refresh, it tells me it cannot find the file and it displays the full path/name of the original location it was linked to.
I would have assumed the following VBA would work, but it only returns an empty string:
Code:
ThisWorkbook.Connections(1).OLEDBConnection.SourceDataFile
Is there anyone who can tell me how to use VBA to reset the file path & name for this connection?
Thanks in advance!