How to create PivotTable in VSTO

E

Ed White

I'm using VS 2008 with Excel 2007. Apparently, I cannot figure out the
format for the TableDestination in the last line of the code below, as it
throws an error when it is executed (and the error message is useless, as is
the documentation for PivotTables):

Dim pt As PivotTable, pc As PivotCache
Me.Columns.Delete()

pc =
Globals.ThisWorkbook.PivotCaches.CreateXlPivotTableSourceType.xlDatabase,
Globals.ThisWorkbook.Connections("IntraDay_Pivot"),
Version:=XlPivotTableVersionList.xlPivotTableVersion12)

pt = pc.CreatePivotTable("[RLSP anal.xlsm]Sheet7!R7C1")

It only throws the error on the last line (pt = ). A pivottable was
manually created using the same "IntraDay_Pivot" connection, so I do not
think that is the problem. What is going wrong here?
 
J

\Ji Zhou [MSFT]\

Hello Ed White,

Thanks for contacting Microsoft Managed Newsgroup Support!

I can reproduce the issue with the codes you post. In fact, to create a
PivotTable with an embedded connection, there are two places need
modifications in the codes,

1.We need to create the PivotCache XlPivotTableSourceType.xlExternal
instead of XlPivotTableSourceType.xlDataBase. Actually, from this MSDN
document http://msdn.microsoft.com/en-us/library/bb238847.aspx, we can know
that ,

The SourceType can be one of these xlPivotTableSourceType constants:
xlConsolidation, xlDatabase, or xlExternal.(xlPivotTable and xlScenario are
not supported when creating a PivotCache using this method). And the The
SourceData argument is required if SourceType isn't xlExternal. It can be a
Range object (when SourceType is either xlConsolidation or xlDatabase) or
an Excel Workbook Connection object (when SourceType is xlExternal). In our
case, our data source is an Workbook connection, so we need to use the
xlExternal.

2.The first parameter of CreatePivotTable should look like "'[RLSP
anal.xlsm]Sheet7'!R7C1" instead of "[RLSP anal.xlsm]Sheet7!R7C1". The
latter one cannot be understood by Excel object model.

I have tested the following codes on my side and it works fine. Please let
me know if you have any future questions or concerns on this issue. And I
will try my best to help.

Private Sub Sheet1_Startup(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Startup
Globals.ThisWorkbook.Connections.Add("IntraDay_Pivot", _
"", _
"OLEDB;Provider=SQLOLEDB.1;Password=iLoveyan888;Persist Security
Info=True;User ID=sa;Data Source=.\SQLEXPRESS;Use Procedure for Prepar" +
"e=1;Auto Translate=True;Packet Size=4096;Workstation ID=JIZHOU;Use
Encryption for Data=False;Tag with column collation when poss" +
"ible=False;Initial Catalog=Test", _
"""Test"".""dbo"".""lanes""", _
3)
Dim pt As PivotTable, pc As PivotCache
Me.Columns.Delete()
pc =
Globals.ThisWorkbook.PivotCaches.Create(XlPivotTableSourceType.xlExternal,
Globals.ThisWorkbook.Connections("IntraDay_Pivot"),
Version:=XlPivotTableVersionList.xlPivotTableVersion12)
pt = pc.CreatePivotTable("'[RLSP anal.xlsm]Sheet7'!R7C1")
End Sub

Good day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://support.microsoft.com/select/default.aspx?target=assistance&ln=en-us.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

\Ji Zhou [MSFT]\

Hello Ed White,

I am writing to check the status of the issue on your side. Could you
please let me know if the suggestion works for you or not? If you have any
questions or concerns, please feel free to let me know. I will be more than
happy to be of assistance.

Have a great day!

Best regards,
Ji Zhou ([email protected], remove 'online.')
Microsoft Online Community Support

=================================================
Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
=================================================
 
E

Ed White

Sorry for not responding earlier--I wasn't notifiied by email of your
response, even though I thought I had checked the box.

I had in the meantime figured it out through trial and error. The key was
that I should not have used xlDatabase as you mentioned (I am in fact
accessing the data from SQL Server, not another workbook, which is why I used
that option). xlExternal works with SQL Server. Also, your comments on the
destination are useful. However, I got it to work by just using "R7C1"
without the prefix. I'll try it with your suggestion (adding the
apostrophe--easy to overlook). Thanks,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top