CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Threaded View

  1. #1
    Join Date
    Apr 2010
    Posts
    1

    Question error in creating a pivot table from windows application using vb.net

    Hi,
    I have been receiving the error "Exception from HRESULT: 0x800A03EC" near (***) in the code below,

    Code:
    Imports Microsoft.Office.Interop.Excel
    Imports System.Runtime.InteropServices
    Imports System.Data.OleDb
    Public Class excel
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim conString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\New Folder\Lusail_Test_DB.accdb;Jet OLEDB:Database Password=Lusail@HTI_HTV"
    Dim myConnection As New System.Data.OleDb.OleDbConnection(conString)
    myConnection.Open()
    Dim str_from As String = "select * from record"
    Dim ds As New DataSet
    Dim da As New OleDbDataAdapter(str_from, myConnection)
    da.Fill(ds, "Record")
    Dim xlApp As Application
    Try
    xlApp = Marshal.GetActiveObject("Excel.Application")
    Catch ex As COMException
    xlApp = New Application
    End Try
    dim xlWBook As Workbook = xlApp.Workbooks.Add(XlWBATemplate.xlWBATWorksheet)
    Dim xlWSheet As Worksheet = CType(xlWBook.Worksheets(1), Worksheet)
    Dim xlRange As Range = CType(xlWSheet, Worksheet).Range("B2")
    Dim ptCache As PivotCache = xlWBook.PivotCaches.Add(SourceType:=XlPivotTableSourceType.xlExternal)
    With ptCache
    .Connection = conString
    .CommandText = str_from
    .CommandType = XlCmdType.xlCmdSql
    End With
    Dim ptTable As PivotTable = xlWSheet.PivotTables.Add(PivotCache:=ptCache, TableDestination:=xlRange, TableName:="Record")
    With ptTable
    .ManualUpdate = True
    .PivotFields("Construction package").Orientation = XlPivotFieldOrientation.xlRowField
    .PivotFields("Region").Orientation = XlPivotFieldOrientation.xlRowField
    .PivotFields("Trade").Orientation = XlPivotFieldOrientation.xlRowField
    .PivotFields("Duration").Orientation = XlPivotFieldOrientation.xlDataField
    .Format(XlPivotFormatType.xlReport2)
    .ManualUpdate = False
    End With
    xlWBook.SaveAs("c:\Report.xls")
    With xlApp
    .Visible = True
    .UserControl = True
    End With
    ptTable = Nothing
    ptCache = Nothing
    xlWSheet = Nothing
    xlWBook = Nothing
    xlApp = Nothing
    End Sub
    Private Sub excel_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub
    End Class
    Can anyone please provide me the coorect code to create a pivot table from vb.net?
    Last edited by HanneSThEGreaT; April 7th, 2010 at 11:10 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured