We currently use pivot tables in Excel 2003 to do our reporting. These pivot tables use the inbuilt "Import External Data" Excel functionality to feed the reports from SQL (SQL server 2008 to be precise).
The reports currently point at our UK database, but we'd now like to make a copy of each report that point at our new USA database (which has the same schema as the UK database).
Rather than painstakingly go through nearly 100 spreadsheets I was hoping that there would be a nice bit of COM automation that I could use to change the connection strings in each of the spreadsheets.
Does anyone know of a way to change the external data source connection string from COM?
I'm using .Net (specifically C#) but I'd be grateful for any help regardless of language or method (it doesn't have to be COM).
After looking at various VBA examples and the MSDN COM documentation I've figured out how to do it.
The important part is that Connection strings are kept in one of two places depending on how you created your worksheet.
If you've used the pivot table wizard then the connection strings will be stored in the collection returned by the
Workbook.PivotCaches()
function (the PivotCache objects returned have aConnection
property which contains the connection string).If you used "Import External Data" the connection strings will be stored in the collection returned by the
Worksheet.QueryTables
property (the QueryTable objects returned have aConnection
property which contains the connection string).There may be more places that Connection strings can be stored, these are the only two that I'm aware of so far. If you know of any more please leave some information in the comments and I'll add to the answer.
Here's a nicely commented full working C# example to help anyone else that comes across this problem: