Hi All,
I'm struggling to find the correct syntax for creating a Pivot table in
an excel file via VB . Net 2005
The Excel help file is not particularly helpful where parameters are
concerned (It rarely mentiones the required data types etc)
The following is as far as I can get;
Dim excelApp As Excel.Applicati on
Dim wkBook As Excel.Workbook, wkSheet As Excel.Worksheet
excelApp = New Excel.Applicati on
excelApp.Visibl e = False
wkBook = excelApp.Workbo oks.Add()
wkSheet = wkBook.Workshee ts("Sheet1")
Dim connectionStr as String = ...
Dim tableName as String = "myDb.dbo.Table Name"
wkBook.Connecti ons.Add("MyConn ection", "Descriptio n", connectionStr,
tableName, 3)
wkBook.Connecti ons("MyConnecti on").Refresh( )
Dim pvtCache As Excel.PivotCach e =
wkBook.PivotCac hes.Create(Exce l.XlPivotTableS ourceType.xlExt ernal,
wkBook.Connecti ons("MyConnecti on"), _
Excel.XlPivotTa bleVersionList. xlPivotTableVer sion11)
' The above part works fine
' but creating the Pivot Table throws up a ComException .. if I play with
the parameters I get a very unhelpful ArgumentExcepti on telling me "The
argument is incorrect" .. but which one ?
Dim pvtTable As Excel.PivotTabl e
' This version throws an ArgumentExcepti on
pvtTable = wkSheet.PivotTa bles.Add(PivotC ache:=pvtCache,
TableDestinatio n:=Excel.XlPara meterType.xlRan ge, TableName:="Piv otTable1")
' This version (and multiple others) throw a ComException
pvtTable = wkSheet.PivotTa bles.Add(PivotC ache:=pvtCache,
TableDestinatio n:=wkSheet.Rang e("A3"), TableName:="Piv otTable1") ' Throws a
ComException
Can anyone help with the correct syntax please ?
Thanks
Gary
I'm struggling to find the correct syntax for creating a Pivot table in
an excel file via VB . Net 2005
The Excel help file is not particularly helpful where parameters are
concerned (It rarely mentiones the required data types etc)
The following is as far as I can get;
Dim excelApp As Excel.Applicati on
Dim wkBook As Excel.Workbook, wkSheet As Excel.Worksheet
excelApp = New Excel.Applicati on
excelApp.Visibl e = False
wkBook = excelApp.Workbo oks.Add()
wkSheet = wkBook.Workshee ts("Sheet1")
Dim connectionStr as String = ...
Dim tableName as String = "myDb.dbo.Table Name"
wkBook.Connecti ons.Add("MyConn ection", "Descriptio n", connectionStr,
tableName, 3)
wkBook.Connecti ons("MyConnecti on").Refresh( )
Dim pvtCache As Excel.PivotCach e =
wkBook.PivotCac hes.Create(Exce l.XlPivotTableS ourceType.xlExt ernal,
wkBook.Connecti ons("MyConnecti on"), _
Excel.XlPivotTa bleVersionList. xlPivotTableVer sion11)
' The above part works fine
' but creating the Pivot Table throws up a ComException .. if I play with
the parameters I get a very unhelpful ArgumentExcepti on telling me "The
argument is incorrect" .. but which one ?
Dim pvtTable As Excel.PivotTabl e
' This version throws an ArgumentExcepti on
pvtTable = wkSheet.PivotTa bles.Add(PivotC ache:=pvtCache,
TableDestinatio n:=Excel.XlPara meterType.xlRan ge, TableName:="Piv otTable1")
' This version (and multiple others) throw a ComException
pvtTable = wkSheet.PivotTa bles.Add(PivotC ache:=pvtCache,
TableDestinatio n:=wkSheet.Rang e("A3"), TableName:="Piv otTable1") ' Throws a
ComException
Can anyone help with the correct syntax please ?
Thanks
Gary
Comment