我使用以下代码从网页中读取一些数据,然后将其粘贴到工作表中。我在同一个子程序中执行此过程两次(即2个网页)。但是,当第二次使用pastespecial时,Excel会崩溃,我不知道为什么会这样。
Sub main()
Application.ScreenUpdating = False
Dim ie As InternetExplorer
Dim doc As HTMLDocument
Dim inputEl As HTMLInputElement
Set ie = New InternetExplorer
'open website
ie.navigate "http://...website.../page1.html"
ie.Visible = False
Sleep 5000
While ie.Busy
DoEvents
Wend
''now copying source code to excel
Set doc = ie.document
CopyText doc.body.innerHTML
Sheet3.Activate
Sheet3.Cells.Clear
Sheet3.DrawingObjects.Delete
Range("A1").Select
Range("A1").PasteSpecial xlPasteAll
'opening second page
ie.navigate "http://...website.../page2.html"
Sleep 2000
While ie.Busy
DoEvents
Wend
Set doc = ie.document
Sheet8.Activate
Sheet8.DrawingObjects.Delete
CopyText doc.body.innerHTML
Range("A1").Select
'''''Excel crashes after this point, specifically on the next line
Sheet8.Range("A1").PasteSpecial xlPasteAll
End Sub
作为参考,我使用了一个函数CopyText(借用互联网),定义如下:
Public Sub CopyText(Text As String)
Dim MSForms_DataObject As Object
Set MSForms_DataObject = CreateObject("new:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
MSForms_DataObject.SetText Text
MSForms_DataObject.PutInClipboard
Set MSForms_DataObject = Nothing
End Sub
我也尝试过使用它,但没有帮助 - pastespecial Crashing when pasting excel data
答案 0 :(得分:0)
您可以使用Sheet8.Paste
代替Sheet8.Range("A1").PasteSpecial
而不使用任何参数。
当剪贴板包含的数据不是来自当前的Excel应用程序时,粘贴特殊对话框会有所不同。