html文件从vba保存还原

时间:2015-01-27 17:48:00

标签: vba excel-vba web-scraping excel

我想重新使用网络响应的快照来测试需要进行网络抓取的应用。我尝试做的只是保存响应(来自Chrome)并从文件中重新加载字符串:

doc.body.innerHtml = StringFromFile

这虽然不起作用,虽然看起来像好的html。如果不工作,我的意思是找不到通过网络时标签“表”(6)中的数据。有没有更好的方法来加载HTML文档?

以下代码是尝试将现有doc保存到文件然后重复使用。它毫无价值,但也许它会帮助我直截了当。

干杯

Private Function GetEWhipersTestHtmlDoc() As HTMLDocument
Dim doc As HTMLDocument
Set doc = New HTMLDocument

Dim sText As String
sText = GetStringFromFile(GetFileName("UnconfirmedRelease"))
doc.body.innerHTML = sText
Set GetEWhipersTestHtmlDoc = doc
End Function

Private Function GetFileName(testName As String) As String
GetFileName = ThisWorkbook.path & Application.PathSeparator & _
    "Earnings Whispers Test Scenarios" & Application.PathSeparator & testName & ".txt"
Debug.Assert Dir(GetFileName) <> ""
End Function

Private Function SaveHtmlStringToFile(testName As String, sInnerHtml As String) As String
Dim fso As Object
Dim oFile As Object
Dim sPath As String

Set fso = CreateObject("Scripting.FileSystemObject")
sPath = GetFileName(testName)

Set oFile = fso.CreateTextFile(sPath)
oFile.WriteLine sInnerHtml
oFile.Close
End Function

**更新**

保存doc.body.outerHtml似乎是对我的改进。文本可以使用“代码段”转换为网页。我尝试将保存的文本放回新文档时遇到错误:

Err 600, Application-defined or object-defined error

Private Function GetEWhipersTestHtmlDoc() As HTMLDocument
    Dim doc As New HTMLDocument
    Dim sText As String

'   Error Handling
    On Error GoTo ErrHandler

    sText = GetStringFromFile(GetFileName("UnconfirmedRelease"))

    doc.body.outerHTML = sText <---- ** ERROR is Here
    Set GetEWhipersTestHtmlDoc = doc

    Exit Function

ErrHandler:
    Select Case DspErrMsg("blah")
         Case Is = vbAbort:  Stop: Resume    'Debug mode - Trace
         Case Is = vbRetry:  Resume          'Try again
         Case Is = vbIgnore:                 'End routine
     End Select


End Function

最终更新

感谢Tim和David,我得到了一些可用的东西。蒂姆最终解决方案的唯一一点就是HtmlDocument.Write在VBA方面受到限制。所以为了“愚弄”编译器,我需要将它声明为一个Object:

Dim doc As Object   <--- don't let vba know we want to write to HTMLDoc
Set doc = New HTMLDocument
Dim sText As String

sText = GetStringFromFile(GetFileName("UnconfirmedRelease"))
doc.Open
doc.Write sText  <-- no intellisense, but compiles...and works!
doc.Close

0 个答案:

没有答案