|
-
Jul 8th, 2016, 08:23 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] System,Runtime.InterloopServices.COMException when writing to excel
I have been using below code for 10 years with no issues.
One user is getting error in below image when trying to export list items to excel, he was using a student/home license so we download a 2016 trial and same issue.

Although if i delete the items in the list it will write the hard coded text below this comment without any issues "'add accessories"
Code:
Option Explicit On
Option Strict Off
Imports System.Xml
Imports System
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Windows.Forms.ListViewItem
Code:
Inherits System.Windows.Forms.Form
Private moApp As Object
Dim moSheet1 As Object
Dim moSheet2 As Object
Code:
Public Sub ExportToExcel()
Try
Dim i As Integer
Dim i2 As Integer
Dim row As Integer, col As Integer
moApp = CreateObject("Excel.Application")
moApp.ScreenUpdating = False
moApp.EnableEvents = False
moApp.DisplayAlerts = False
moApp = moApp.Workbooks.Open(My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\xxx\Template.xlt", True) ', OverWriteFiles) ', as excel.workbook
moSheet1 = moApp.worksheets(1)
moSheet2 = moApp.worksheets(2)
row = 1
col = 1
moSheet2.Cells(row, col) = Console.lblJobNo.Text
col = 1
row = row + 1
Dim dDircetory As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments & "\xxx\"
'************************************************************************************************************************
row = 3
col = 1
moSheet1.Cells(row, col) = Console.lblJobNo.Text
col = 1
row = row + 1
With Me.lstTakeoffList
Dim qQty As Integer = 4
For i = 0 To .Items.Count - 1
col = 1
moSheet1.Cells(row, col) = .Items(i).Text
col = col + 1
moSheet1.Cells(row, col) = .Items(i).SubItems(1).Text
col = col + 1
moSheet1.Cells(row, col) = .Items(i).SubItems(2).Text
col = col + 1
moSheet1.Cells(row, col) = .Items(i).SubItems(3).Text
col = col + 1
moSheet1.Cells(row, col) = .Items(i).SubItems(4).Text
col = col + 1
moSheet1.Cells(row, col) = .Items(i).SubItems(5).Text
col = col + 1
moSheet1.Cells(row, col) = .Items(i).SubItems(6).Text
col = col + 1
'******************************************************************
'23/02/2016 remove $ sign
'H
Dim lLabourCostFormulaToEnter As String = Nothing
lLabourCostFormulaToEnter = "=IF(ISNA(VLOOKUP(A:A,'" & dDircetory & sShortenedpPricingSheetName & "'!A1:D5000,2,FALSE)),"""",VLOOKUP(A:A,'" & dDircetory & sShortenedpPricingSheetName & "'!A1:D5000,2,FALSE))" '.ToString
moSheet1.Cells(row, col).Formula = lLabourCostFormulaToEnter
col = col + 1
'I
Dim mMaterialCostFormulaToEnter As String = Nothing
mMaterialCostFormulaToEnter = "=IF(ISNA(VLOOKUP(A:A,'" & dDircetory & sShortenedpPricingSheetName & "'!A1:D5000,3,FALSE)),"""",VLOOKUP(A:A,'" & dDircetory & sShortenedpPricingSheetName & "'!A1:D5000,3,FALSE))" '.ToString
moSheet1.Cells(row, col).Formula = mMaterialCostFormulaToEnter
col = col + 1
'J
Dim lLabourPriceFormulaToEnter As String = Nothing
lLabourPriceFormulaToEnter = "=IF(F" & qQty & "="""", """",F" & qQty & "*H" & qQty & ")"
moSheet1.Cells(row, col).Formula = lLabourPriceFormulaToEnter
col = col + 1
'K
Dim mMaterialPriceFormulaToEnter As String = Nothing
mMaterialPriceFormulaToEnter = "=IF(F" & qQty & "="""", """",F" & qQty & "*I" & qQty & ")"
moSheet1.Cells(row, col).Formula = mMaterialPriceFormulaToEnter
col = col + 1
'L
Dim tTotalCostFormulaToEnter As String = Nothing
tTotalCostFormulaToEnter = "=IF(F" & qQty & "="""", """",J" & qQty & "+K" & qQty & ")"
moSheet1.Cells(row, col).Formula = tTotalCostFormulaToEnter
col = col + 1
row = row + 1
qQty = qQty + 1
Next
'add assessoreies
If chkAss.Checked Then
col = 1
row = row + 1
moSheet1.Cells(row, col) = "Assesories [rivert/silicon]"
col = col + 6
moSheet1.Cells(row, col) = "no"
col = 1
row = row + 1
moSheet1.Cells(row, col) = "Deliveries"
col = col + 6
moSheet1.Cells(row, col) = "no"
col = 1
row = row + 1
moSheet1.Cells(row, col) = "Supervision"
col = col + 6
moSheet1.Cells(row, col) = "no"
col = 1
row = row + 1
moSheet1.Cells(row, col) = "Waste"
col = col + 6
moSheet1.Cells(row, col) = "no"
col = 1
row = row + 1
moSheet1.Cells(row, col) = "Height Allowance"
col = col + 6
moSheet1.Cells(row, col) = "m2"
End If
End With
End Sub
I checked if the 2.0 and 3.5 frameworks were enabled and they are.
It appears to the way the formulas are being written to excel?
Any ideas what is causing this error
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|