Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I've got an existing pivot table that performs many calculations within various columns that uses an external data source (.csv file). I would like to programmatically change the source file to another .csv file. I know that I need to create a WorkbookConnection, but I haven't had any luck yet finding an example on the web.

Any help you can provide would be appreciated.

What I have tried:

Have not found an example yet.
Posted
Updated 13-Jul-23 6:17am

1 solution

Quote:
I know that I need to create a WorkbookConnection, but I haven't had any luck yet finding an example on the web.


Not sure what you searched for, I found tons of examples when I searched for vb9 create a WorkbookConnection to a csv file[^]

One such an example is How to dynamically change a PivotTable's source file location in VB.NET?[^]

Microsoft also explains the WorkbookConnection object[^] in detail.
 
Share this answer
 
Comments
BladesAero 11-Jul-23 16:35pm    
The first link was a question I posted on Stack Overflow. The answer wasn't really helpful. It helped me identify what connections already existed in the sheet, but didn't provide more info on changing that connection.

The second link does provide background on the properties, but doesn't give an example for changing the connection.
Andre Oosthuizen 12-Jul-23 3:29am    
Your message conflicts with your question - Q = I need to create a connection, Com = It helped me identify what connections already existed, meaning that you have a connection, you don't know how to change it - change it to what, where, how, when?

It is like me telling you that I drive a blue car, what brand is it? Share more code in your question, not in the comments, share your code so we don't have to guess what you mean.
BladesAero 13-Jul-23 9:37am    
I appreciate your help on this. Currently I have a power PivotTable with an external connection to a csv file. I want to programmatically change that connection to different file on my network. I just used ChatGPT to get started again on this problem. This is what it suggested:

Dim connection As Microsoft.Office.Interop.Excel.WorkbookConnection = wb.Connections(MPPivotTable.PivotCache().WorkbookConnection.Name)
Dim newConnectionString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\l46633\Downloads;Extended Properties='Text;HDR=YES;FMT=Delimited(|)'"
connection.OLEDBConnection.Connection = newConnectionString
Andre Oosthuizen 13-Jul-23 12:16pm    
You're welcome. What happened when you ran the ChatGPT code?
BladesAero 13-Jul-23 13:24pm    
It threw an exception on:
Dim connection As Microsoft.Office.Interop.Excel.WorkbookConnection = wb.Connections(MPPivotTable.PivotCache().WorkbookConnection.Name)

0x800A03EC

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900