由于我一直在VB.Net中构建这个Windows表格,我有一个持续的错误,我似乎不能撼动。我一直在通过谷歌挖掘,因为我可以为一个答案,但没有任何运气。
基本上发生的事情是,当我的应用程序得到下面的子,它有时会出错,一个非常无益的。
Exception thrown: 'System.Runtime.InteropServices.COMException' in mscorlib.dll
错误,但有时运行正常。
这是问题中的Sub。
Sub DriverSheet_Data_Transfer(XLAimApp As Excel.Application, XLAimWorkSheet As Excel.Worksheet, XLAimWorkBook As Excel.Workbook, e As System.ComponentModel.DoWorkEventArgs)
Console.WriteLine(vbCrLf & "Transfering Driver Information...")
' Define Driver Sheet Vaiables
Dim XLDriverWorkBook As Excel.Workbook
Dim XLDriverWorkSheet As Excel.Worksheet
Dim rowNum As Integer
' Open Driver Sheet
XLDriverWorkBook = XLAimApp.Workbooks.Open(GeneratorForm.driverPath)
Console.WriteLine(vbCrLf & "{0} Succesfully Loaded...", driverPathLbl.Text)
XLDriverWorkSheet = CType(XLDriverWorkBook.Sheets(1), Excel.Worksheet)
' Find total number of rows
rowNum = 2
While (XLDriverWorkSheet.Cells(rowNum, 1).Value IsNot Nothing)
rowNum += 1
End While
' Filter DriverSheet Data by ID ascending
XLDriverWorkSheet.Range("A1:D" & rowNum - 1).Select()
XLDriverWorkSheet.Range("A1:D" & rowNum - 1).Sort(Key1:=XLDriverWorkSheet.Range("A1"),
Order1:=Excel.XlSortOrder.xlAscending,
Orientation:=Excel.XlSortOrientation.xlSortColumns,
Header:=Excel.XlYesNoGuess.xlYes,
MatchCase:=False,
SortMethod:=Excel.XlSortMethod.xlPinYin,
DataOption1:=Excel.XlSortDataOption.xlSortNormal
)
Try
' Select Data and Copy
'Clipboard.SetDataObject(XLDriverWorkSheet.Range("A2:D" & rowNum - 1))
XLDriverWorkSheet.Range("A2:D" & rowNum - 1).Copy()
'XLAimWorkSheet.Paste(Destination:=XLAimWorkSheet.Range("R2"))
XLAimWorkSheet.Range("R2").Select()
XLAimWorkSheet.Range("R2:U" & rowNum - 1).PasteSpecial(Excel.XlPasteType.xlPasteAllExceptBorders)
Catch ex As System.Runtime.InteropServices.COMException
MsgBox("ERROR: Guess what? That one error that you can't figure out happened again. Ugh...", MsgBoxStyle.Critical, "Stupid Error Strikes Again")
' Close Driver
XLAimApp.DisplayAlerts = False
XLDriverWorkBook.Close()
releaseObject(XLDriverWorkBook)
releaseObject(XLDriverWorkSheet)
' Close Aim
XLAimWorkBook.Close()
XLAimApp.Quit()
releaseObject(XLAimApp)
releaseObject(XLAimWorkBook)
releaseObject(XLAimWorkSheet)
e.Cancel = True
Exit Sub
End Try
Console.WriteLine(vbCrLf & "Transfer Done. Closing Driver Sheet...")
XLDriverWorkBook.Close()
releaseObject(XLDriverWorkBook)
releaseObject(XLDriverWorkSheet)
End Sub
我绝不是VB专家,所以请原谅我只能想象是非常低效的,而且是新手的代码。
谢谢!我一直在构建这个Windows系统。
这是一个常见的Excel错误。你必须先选择表,再选择范围。
XLDriverWorkSheet.Range("A2:D" & rowNum - 1).Copy()
XLAimWorkSheet.Select() ' ADDED LINE
XLAimWorkSheet.Range("R2").Select()
XLAimWorkSheet.Range("R2:U" & rowNum - 1).PasteSpecial(Excel.XlPasteType.xlPasteAllExceptBorders)
但也要注意: .Select
不需要 PasteSpecial
操作。只需要移动单元格光标。
XLDriverWorkSheet.Range("A2:D" & rowNum - 1).Copy()
XLAimWorkSheet.Range("R2:U" & rowNum - 1).PasteSpecial(Excel.XlPasteType.xlPasteAllExceptBorders)