我们有一个Excel加载项(由我们创建),它从服务器获取数据并转储到excel中。它在Excel的新构建版本(16.9029。*)之前一直正常工作。在此更新之后,所有数据都从服务器正确获取,但无法转储到Excel工作表中,并显示错误“Microsoft Excel已停止工作"”。
当我尝试调试问题时。我发现在" QueryTables.BeforeRefresh" 事件发生后,Excel突然崩溃了&显示错误" Microsoft Excel已停止工作"。
以下是我们可以提供给您的示例代码。
代码说明:
'Some logic here for our add-in Webquery (URL more than 1000 character)
Public Sub createQueryTable(URL As String, dest As String)
Dim oBk As Workbook
Dim x As New clsQueryTable
Dim oQT As QueryTable
Dim i As Integer
Dim strFileName As String
Dim posturl, postdata As String
Dim instrstart, instrend As Integer
If Not Mid(URL, Len(URL) - 12, Len(URL)) = "&format=.html" Then
strFileName = URL & "&format=.html"
Else
strFileName = URL
End If
On Error Resume Next
ActiveSheet.Range(dest).QueryTable.ResultRange.ClearContents
ActiveSheet.Range(dest).QueryTable.Delete
On Error GoTo errorhandler
With ActiveSheet
Set oQT = .QueryTables.Add( _
Connection:="URL;" & strFileName, _
Destination:=Range(dest))
End With
Dim flagcheck As Boolean
flagcheck = False
With oQT
'State that we're selecting a specific table
.WebSelectionType = xlSpecifiedTables
.RefreshStyle = xlOverwriteCells
.EnableRefresh = True
'Import the 1st table on the page
.WebTables = "1"
'Ignore the web page's formatting
.WebFormatting = xlWebFormattingAll
'Do not try to recognize dates
.WebDisableDateRecognition = True
'Don't automatically refresh the query each time the file is opened
.RefreshOnFileOpen = False
'Waiting for the query to complete before continuing
.BackgroundQuery = True
'Save the query data with the workbook
.SaveData = True
'Adjust column widths to autofit new data
.AdjustColumnWidth = True
.PreserveFormatting = True
End With
'Perform the query, waiting for it to complete
Set x.qt = oQT
Set outputCell = Range(dest)
x.qt.Refresh BackgroundQuery:=False 'From this it calls qt_BeforeRefresh.
Set x = Nothing
Exit Sub
errorhandler:
'On Error Resume Next
If Err.Number = 1004 Then
MsgBox "URL length exceeds the limit. Remove " &
Len(x.qt.Connection) - 222 & " from URL"
Err.Clear
Else
MsgBox Err.Number & vbCrLf & Err.Description
End If
Set x = Nothing
End Sub
Private Sub qt_BeforeRefresh(Cancel As Boolean)
Debug.Print "hiii" 'It is printed Succesfully
End Sub 'After this point of debug Excel Crashed So further execution also
stops.
我们的AddIn有一些逻辑,它从服务器获取数据&将其打印到Excel工作表中。
获取数据后,它会转到" QueryTable" 功能&最后我们使用了Excel的Refresh方法来执行" BeforeRefresh"活动&执行此方法后; Excel停止工作。
我们已将调试点放在该方法中以进行最终执行的测试。
我们发现的另一件事是,新版本中的请求网址长度(16.9029。*)可能存在问题。
之前它在请求网址(网页查询)中提取 1800个字符,但现在它只 250 (根据我们的测试)。请求上面的字符然后此字符会导致问题&停止执行Excel。 我们还没有找到任何有关新版Excel的文档。有没有人对此有任何想法?
非常感谢任何帮助。我们陷入了困境。
期待这些建议。
谢谢&的问候,