Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Pasting to Excel sheet (getting desperate!)

I wrote a class in VB.NET to export the contents of a datagrid to Excel. It
works perfectly on my machine, but it fails on my customers' PCs that have
identical versions of Win XP (SP1) and Excel (SP1) installed.

The error is:
System.Runtime.InteropServices.COMException(0x800A 03EC): Exception from
HRESULT: 0x800A03EC.
at Microsoft.Office.Interop.Excel._Worksheet.Paste(Ob ject Destination,
Object Link)
at myProject.ExcelWriter.ExportToExcel(DataTable& tbl)
at myProject.frmMain.btnExportToExcel_Click(Object sender, EventArgs e)

My complete code for the class is below. Does anyone know what the problem
with the Paste() statement might be?

Thank you!

Eric


'''''''''''''''''' BEGIN CODE ''''''''''''''''''
Option Explicit On

Imports System.Text
Imports Microsoft.Office.Interop

Public Class ExcelWriter

Public Shared Sub ExportToExcel(ByRef tbl As DataTable)
' This routine copies the contents of a data table, named "dt"
(declared
' Private within this Public class), to the Windows Clipboard in a
tab-
' delimited format. It then creates an Excel spreadsheet and pastes
the
' contents of the Clipboard to the spreadsheet.

If tbl Is Nothing Then Exit Sub

Dim sb As New StringBuilder
Dim row, col As Integer

' Add the title.
Dim ReportTitle As String = tbl.TableName
If ReportTitle.Length < 1 Then ReportTitle = "New Report"
sb.Append(ReportTitle & vbNewLine & vbNewLine)

' Copy column headers.
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Columns.Item(col).ColumnName) Then
sb.Append(tbl.Columns.Item(col).ColumnName)
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)

' Copy rows.
For row = 0 To tbl.Rows.Count - 1
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Rows(row)(col)) Then
If TypeOf tbl.Rows(row)(col) Is DateTime Then
Dim d As DateTime = tbl.Rows(row)(col)
sb.Append(d.ToShortDateString)
Else
Dim NewLine As Char = ChrW(32)

If tbl.Rows(row)(0).ToString = "68" AndAlso _
tbl.Columns(col).ColumnName = "Comments" Then

Dim iii As Integer = 0
End If

' Account for tab and newline chars in string.
Dim rawString As String =
tbl.Rows(row)(col).ToString
rawString = rawString.Replace(vbTab, " "c)
rawString = rawString.Replace(N, " "c)
rawString = rawString.Replace(NewLine, " "c)
sb.Append(tbl.Rows(row)(col))
End If
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)
Next

Clipboard.SetDataObject(sb.ToString)

' Create Excel Objects
Dim ExcelApp As Excel.Application
Dim Book As Excel.Workbook
Dim Sheet As Excel.Worksheet
Dim Range As Excel.Range

' Start Excel and get Application object:
ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = False

' Add a new workbook
Book = ExcelApp.Workbooks.Add
Sheet = Book.ActiveSheet
' NOTE: Excel worksheet name max length = 31.
Sheet.Name = Left(ReportTitle, 31)
Application.DoEvents()

'''''''''''''''''' ERROR OCCURS HERE ''''''''''''''''''
' Paste the Clipboard contents.
Sheet.Paste()

' Format column headers.
Range = Sheet.Rows(3)
Range.Font.Bold = True

' AutoFit Columns
Range = Sheet.Range("A1", "IA1")
Range.EntireColumn.AutoFit()

' Format title.
Range = Sheet.Cells(1, 1)
Range.Font.Bold = True
Range.Font.Size = 14
Application.DoEvents()

' Add date/time created.
Range = Sheet.Cells(2, tbl.Columns.Count)
Range.Value = "Report Created: " & Now.ToString
Range.Font.Size = 8
Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight

' Center title across selection.
Dim cellStart As Excel.Range = ExcelApp.Range("A1")
Dim cellEnd As Excel.Range = _
DirectCast(Sheet.Cells(1, _
tbl.Columns.Count), Excel.Range)
Dim rng As Excel.Range = _
ExcelApp.Range(cellStart, cellEnd)
rng.Merge()
rng.HorizontalAlignment = _
Excel.XlHAlign.xlHAlignCenterAcrossSelection
Application.DoEvents()

ExcelApp.Visible = True
End Sub
End Class

'''''''''''''''''' END CODE ''''''''''''''''''



  #2   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Pasting to Excel sheet (getting desperate!)

Just a suggestion, you could remove the dependency on having Excel installed
on the machine you run your program and eleminate the possibility in
differences in the API versions by formatting your data from your datatable
into an html table, then just save the string as .xls, Excel will interpret
the html correctly when the file is opened.

"Eric" wrote:

I wrote a class in VB.NET to export the contents of a datagrid to Excel. It
works perfectly on my machine, but it fails on my customers' PCs that have
identical versions of Win XP (SP1) and Excel (SP1) installed.

The error is:
System.Runtime.InteropServices.COMException(0x800A 03EC): Exception from
HRESULT: 0x800A03EC.
at Microsoft.Office.Interop.Excel._Worksheet.Paste(Ob ject Destination,
Object Link)
at myProject.ExcelWriter.ExportToExcel(DataTable& tbl)
at myProject.frmMain.btnExportToExcel_Click(Object sender, EventArgs e)

My complete code for the class is below. Does anyone know what the problem
with the Paste() statement might be?

Thank you!

Eric


'''''''''''''''''' BEGIN CODE ''''''''''''''''''
Option Explicit On

Imports System.Text
Imports Microsoft.Office.Interop

Public Class ExcelWriter

Public Shared Sub ExportToExcel(ByRef tbl As DataTable)
' This routine copies the contents of a data table, named "dt"
(declared
' Private within this Public class), to the Windows Clipboard in a
tab-
' delimited format. It then creates an Excel spreadsheet and pastes
the
' contents of the Clipboard to the spreadsheet.

If tbl Is Nothing Then Exit Sub

Dim sb As New StringBuilder
Dim row, col As Integer

' Add the title.
Dim ReportTitle As String = tbl.TableName
If ReportTitle.Length < 1 Then ReportTitle = "New Report"
sb.Append(ReportTitle & vbNewLine & vbNewLine)

' Copy column headers.
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Columns.Item(col).ColumnName) Then
sb.Append(tbl.Columns.Item(col).ColumnName)
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)

' Copy rows.
For row = 0 To tbl.Rows.Count - 1
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Rows(row)(col)) Then
If TypeOf tbl.Rows(row)(col) Is DateTime Then
Dim d As DateTime = tbl.Rows(row)(col)
sb.Append(d.ToShortDateString)
Else
Dim NewLine As Char = ChrW(32)

If tbl.Rows(row)(0).ToString = "68" AndAlso _
tbl.Columns(col).ColumnName = "Comments" Then

Dim iii As Integer = 0
End If

' Account for tab and newline chars in string.
Dim rawString As String =
tbl.Rows(row)(col).ToString
rawString = rawString.Replace(vbTab, " "c)
rawString = rawString.Replace(N, " "c)
rawString = rawString.Replace(NewLine, " "c)
sb.Append(tbl.Rows(row)(col))
End If
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)
Next

Clipboard.SetDataObject(sb.ToString)

' Create Excel Objects
Dim ExcelApp As Excel.Application
Dim Book As Excel.Workbook
Dim Sheet As Excel.Worksheet
Dim Range As Excel.Range

' Start Excel and get Application object:
ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = False

' Add a new workbook
Book = ExcelApp.Workbooks.Add
Sheet = Book.ActiveSheet
' NOTE: Excel worksheet name max length = 31.
Sheet.Name = Left(ReportTitle, 31)
Application.DoEvents()

'''''''''''''''''' ERROR OCCURS HERE ''''''''''''''''''
' Paste the Clipboard contents.
Sheet.Paste()

' Format column headers.
Range = Sheet.Rows(3)
Range.Font.Bold = True

' AutoFit Columns
Range = Sheet.Range("A1", "IA1")
Range.EntireColumn.AutoFit()

' Format title.
Range = Sheet.Cells(1, 1)
Range.Font.Bold = True
Range.Font.Size = 14
Application.DoEvents()

' Add date/time created.
Range = Sheet.Cells(2, tbl.Columns.Count)
Range.Value = "Report Created: " & Now.ToString
Range.Font.Size = 8
Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight

' Center title across selection.
Dim cellStart As Excel.Range = ExcelApp.Range("A1")
Dim cellEnd As Excel.Range = _
DirectCast(Sheet.Cells(1, _
tbl.Columns.Count), Excel.Range)
Dim rng As Excel.Range = _
ExcelApp.Range(cellStart, cellEnd)
rng.Merge()
rng.HorizontalAlignment = _
Excel.XlHAlign.xlHAlignCenterAcrossSelection
Application.DoEvents()

ExcelApp.Visible = True
End Sub
End Class

'''''''''''''''''' END CODE ''''''''''''''''''




  #3   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
external usenet poster
 
Posts: 339
Default Pasting to Excel sheet (getting desperate!)


<Eric wrote in message ...
I wrote a class in VB.NET to export the contents of a datagrid to Excel.

It
works perfectly on my machine, but it fails on my customers' PCs that have
identical versions of Win XP (SP1) and Excel (SP1) installed.

The error is:
System.Runtime.InteropServices.COMException(0x800A 03EC): Exception from
HRESULT: 0x800A03EC.
at Microsoft.Office.Interop.Excel._Worksheet.Paste(Ob ject Destination,
Object Link)
at myProject.ExcelWriter.ExportToExcel(DataTable& tbl)
at myProject.frmMain.btnExportToExcel_Click(Object sender, EventArgs e)

My complete code for the class is below. Does anyone know what the

problem
with the Paste() statement might be?

Thank you!

Eric


'''''''''''''''''' BEGIN CODE ''''''''''''''''''
Option Explicit On

Imports System.Text
Imports Microsoft.Office.Interop

Public Class ExcelWriter

Public Shared Sub ExportToExcel(ByRef tbl As DataTable)
' This routine copies the contents of a data table, named "dt"
(declared
' Private within this Public class), to the Windows Clipboard in a
tab-
' delimited format. It then creates an Excel spreadsheet and

pastes
the
' contents of the Clipboard to the spreadsheet.

If tbl Is Nothing Then Exit Sub

Dim sb As New StringBuilder
Dim row, col As Integer

' Add the title.
Dim ReportTitle As String = tbl.TableName
If ReportTitle.Length < 1 Then ReportTitle = "New Report"
sb.Append(ReportTitle & vbNewLine & vbNewLine)

' Copy column headers.
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Columns.Item(col).ColumnName) Then
sb.Append(tbl.Columns.Item(col).ColumnName)
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)

' Copy rows.
For row = 0 To tbl.Rows.Count - 1
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Rows(row)(col)) Then
If TypeOf tbl.Rows(row)(col) Is DateTime Then
Dim d As DateTime = tbl.Rows(row)(col)
sb.Append(d.ToShortDateString)
Else
Dim NewLine As Char = ChrW(32)

If tbl.Rows(row)(0).ToString = "68" AndAlso _
tbl.Columns(col).ColumnName = "Comments" Then

Dim iii As Integer = 0
End If

' Account for tab and newline chars in string.
Dim rawString As String =
tbl.Rows(row)(col).ToString
rawString = rawString.Replace(vbTab, " "c)
rawString = rawString.Replace(N, " "c)
rawString = rawString.Replace(NewLine, " "c)
sb.Append(tbl.Rows(row)(col))
End If
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)
Next

Clipboard.SetDataObject(sb.ToString)

' Create Excel Objects
Dim ExcelApp As Excel.Application
Dim Book As Excel.Workbook
Dim Sheet As Excel.Worksheet
Dim Range As Excel.Range

' Start Excel and get Application object:
ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = False

' Add a new workbook
Book = ExcelApp.Workbooks.Add
Sheet = Book.ActiveSheet
' NOTE: Excel worksheet name max length = 31.
Sheet.Name = Left(ReportTitle, 31)
Application.DoEvents()

'''''''''''''''''' ERROR OCCURS HERE ''''''''''''''''''
' Paste the Clipboard contents.
Sheet.Paste()

' Format column headers.
Range = Sheet.Rows(3)
Range.Font.Bold = True

' AutoFit Columns
Range = Sheet.Range("A1", "IA1")
Range.EntireColumn.AutoFit()

' Format title.
Range = Sheet.Cells(1, 1)
Range.Font.Bold = True
Range.Font.Size = 14
Application.DoEvents()

' Add date/time created.
Range = Sheet.Cells(2, tbl.Columns.Count)
Range.Value = "Report Created: " & Now.ToString
Range.Font.Size = 8
Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight

' Center title across selection.
Dim cellStart As Excel.Range = ExcelApp.Range("A1")
Dim cellEnd As Excel.Range = _
DirectCast(Sheet.Cells(1, _
tbl.Columns.Count), Excel.Range)
Dim rng As Excel.Range = _
ExcelApp.Range(cellStart, cellEnd)
rng.Merge()
rng.HorizontalAlignment = _
Excel.XlHAlign.xlHAlignCenterAcrossSelection
Application.DoEvents()

ExcelApp.Visible = True
End Sub
End Class

'''''''''''''''''' END CODE ''''''''''''''''''



Start the task manager an select the processes tab. Click on "Image name"
and make sure there's no excel.exe ghost process. Or simply restar the
computer. What's in the datagrid? Does it make any difference if the user
tries to copy something like a single row?

/Fredrik



  #4   Report Post  
Posted to microsoft.public.dotnet.languages.vb,microsoft.public.excel.programming
No Name
 
Posts: n/a
Default Pasting to Excel sheet (getting desperate!)

Thank you so much, Dan! You are truly brilliant. I coded up your idea this
morning, and it works perfectly. It's even much faster than using Interop.

I've had this issue hanging over my head for more than a year, and you saved
my hide. If I had extra money to give, I'd cut you a big fat check. :-)

Eric


"Alien2_51" wrote in message
...
Just a suggestion, you could remove the dependency on having Excel

installed
on the machine you run your program and eleminate the possibility in
differences in the API versions by formatting your data from your

datatable
into an html table, then just save the string as .xls, Excel will

interpret
the html correctly when the file is opened.

"Eric" wrote:

I wrote a class in VB.NET to export the contents of a datagrid to Excel.

It
works perfectly on my machine, but it fails on my customers' PCs that

have
identical versions of Win XP (SP1) and Excel (SP1) installed.

The error is:
System.Runtime.InteropServices.COMException(0x800A 03EC): Exception from
HRESULT: 0x800A03EC.
at Microsoft.Office.Interop.Excel._Worksheet.Paste(Ob ject

Destination,
Object Link)
at myProject.ExcelWriter.ExportToExcel(DataTable& tbl)
at myProject.frmMain.btnExportToExcel_Click(Object sender, EventArgs

e)

My complete code for the class is below. Does anyone know what the

problem
with the Paste() statement might be?

Thank you!

Eric


'''''''''''''''''' BEGIN CODE ''''''''''''''''''
Option Explicit On

Imports System.Text
Imports Microsoft.Office.Interop

Public Class ExcelWriter

Public Shared Sub ExportToExcel(ByRef tbl As DataTable)
' This routine copies the contents of a data table, named "dt"
(declared
' Private within this Public class), to the Windows Clipboard in

a
tab-
' delimited format. It then creates an Excel spreadsheet and

pastes
the
' contents of the Clipboard to the spreadsheet.

If tbl Is Nothing Then Exit Sub

Dim sb As New StringBuilder
Dim row, col As Integer

' Add the title.
Dim ReportTitle As String = tbl.TableName
If ReportTitle.Length < 1 Then ReportTitle = "New Report"
sb.Append(ReportTitle & vbNewLine & vbNewLine)

' Copy column headers.
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Columns.Item(col).ColumnName) Then
sb.Append(tbl.Columns.Item(col).ColumnName)
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)

' Copy rows.
For row = 0 To tbl.Rows.Count - 1
For col = 0 To tbl.Columns.Count - 1
If Not IsDBNull(tbl.Rows(row)(col)) Then
If TypeOf tbl.Rows(row)(col) Is DateTime Then
Dim d As DateTime = tbl.Rows(row)(col)
sb.Append(d.ToShortDateString)
Else
Dim NewLine As Char = ChrW(32)

If tbl.Rows(row)(0).ToString = "68" AndAlso _
tbl.Columns(col).ColumnName = "Comments" Then

Dim iii As Integer = 0
End If

' Account for tab and newline chars in string.
Dim rawString As String =
tbl.Rows(row)(col).ToString
rawString = rawString.Replace(vbTab, " "c)
rawString = rawString.Replace(N, " "c)
rawString = rawString.Replace(NewLine, " "c)
sb.Append(tbl.Rows(row)(col))
End If
End If
sb.Append(vbTab)
Application.DoEvents()
Next
sb.Append(vbNewLine)
Next

Clipboard.SetDataObject(sb.ToString)

' Create Excel Objects
Dim ExcelApp As Excel.Application
Dim Book As Excel.Workbook
Dim Sheet As Excel.Worksheet
Dim Range As Excel.Range

' Start Excel and get Application object:
ExcelApp = CreateObject("Excel.Application")
ExcelApp.Visible = False

' Add a new workbook
Book = ExcelApp.Workbooks.Add
Sheet = Book.ActiveSheet
' NOTE: Excel worksheet name max length = 31.
Sheet.Name = Left(ReportTitle, 31)
Application.DoEvents()

'''''''''''''''''' ERROR OCCURS HERE ''''''''''''''''''
' Paste the Clipboard contents.
Sheet.Paste()

' Format column headers.
Range = Sheet.Rows(3)
Range.Font.Bold = True

' AutoFit Columns
Range = Sheet.Range("A1", "IA1")
Range.EntireColumn.AutoFit()

' Format title.
Range = Sheet.Cells(1, 1)
Range.Font.Bold = True
Range.Font.Size = 14
Application.DoEvents()

' Add date/time created.
Range = Sheet.Cells(2, tbl.Columns.Count)
Range.Value = "Report Created: " & Now.ToString
Range.Font.Size = 8
Range.HorizontalAlignment = Excel.XlHAlign.xlHAlignRight

' Center title across selection.
Dim cellStart As Excel.Range = ExcelApp.Range("A1")
Dim cellEnd As Excel.Range = _
DirectCast(Sheet.Cells(1, _
tbl.Columns.Count), Excel.Range)
Dim rng As Excel.Range = _
ExcelApp.Range(cellStart, cellEnd)
rng.Merge()
rng.HorizontalAlignment = _
Excel.XlHAlign.xlHAlignCenterAcrossSelection
Application.DoEvents()

ExcelApp.Visible = True
End Sub
End Class

'''''''''''''''''' END CODE ''''''''''''''''''






Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem Cutting and Pasting from SQL Table to Excel Sheet Chaplain Doug Excel Discussion (Misc queries) 0 April 25th 08 04:55 PM
Excel VBA - SQL to .dbf date problem - GETTING DESPERATE Erasmus Excel Discussion (Misc queries) 6 July 2nd 07 05:20 PM
Excel program help - desperate!!! Felicity Geronimo Excel Programming 1 October 21st 04 02:07 PM
Problem pasting a row from a hidden sheet to the first free row on another visible sheet Didier Poskin Excel Programming 2 January 10th 04 01:18 AM
Pasting to Sheet Nigel[_7_] Excel Programming 2 November 14th 03 06:03 PM


All times are GMT +1. The time now is 12:22 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"