我是 VB 新手,在尝试复制工作表时遇到了令人沮丧的崩溃。
调用worksheet.Copy()
在大多数情况下都有效,但是当用户在单元格中输入文本并且没有取消选择该单元格或按下 Enter 键时,它会崩溃并显示消息“工作表类的复制消息失败”和错误代码 -2146827284。谁能告诉我为什么会这样?
这是整个子(当用户单击 excel 功能区中的按钮时调用)
Friend Sub CheckSheet(ByRef worksheet As Microsoft.Office.Interop.Excel.Worksheet, ByVal testType As List(Of String), ByRef guide As List(Of String), ByRef display As Dictionary(Of String, Boolean), ByRef URL As String)
'Read the labels and units from the current worksheet
Dim readings = loadReadings(worksheet, 3, 3, 4)
If readings Is Nothing Then 'If the readings are not successfully loaded
Exit Sub
End If
'Create a copy of the workbook to be processed to put the results in
Dim originalFilename As String = Application.ActiveWorkbook.FullName
Dim using1904system As Boolean = Application.ActiveWorkbook.Date1904
worksheet.Copy() 'FIXME Crash here if selected cell has newly entered text
worksheet = Application.ActiveWorkbook.ActiveSheet
Application.ActiveWorkbook.Date1904 = using1904system 'Make sure the new worksheet uses the same date option
'Get requests from worksheet
Dim cellMap = New Dictionary(Of String, Range)
Dim allRequests = getRequests(worksheet, readings, cellMap, 5, testType, guidelines, 1, 2, 3)
'Gets the results from the server and outputs them to a new Excel workbook
Dim success = processResults(allRequests, URL, worksheet, display, cellMap, 5, originalFilename, testType, guidelines)
If Not success Then
Exit Sub
End If
'Set focus to the checked worksheet
worksheet.Activate()
End Sub
编辑:
我试图通过选择不同的单元格以编程方式取消选择单元格,例如:
worksheet.Range("A1").Activate()
但这似乎没有任何影响(崩溃仍然发生)。我的插件只在用户明确指示这样做时调用复制方法,所以如果用户停止在单元格中输入文本并单击按钮运行我的插件,它们似乎是一个非常安全的假设完成在框中输入文本。