ebr*_*rts 2 html io excel vba excel-vba
这感觉应该很简单.我的计算机上存有.HTML文件,我想将整个文件读成字符串.当我尝试超级直接
Dim FileAsString as string
Open "C:\Myfile.HTML" for input as #1
Input #1, FileAsString
Close #1
debug.print FileAsString
Run Code Online (Sandbox Code Playgroud)
我没有得到整个文件.我只得到前几行(我知道即时窗口切断,但这不是问题.我绝对不会将整个文件放入我的字符串中.)我也尝试使用文件系统对象的替代方法,并且得到了类似的结果,只是这次有很多奇怪的字符和问号投入.这让我觉得它可能是某种编码问题.(虽然坦率地说,我并不完全明白这意味着什么.我知道有不同的编码格式,这可能会导致字符串解析出现问题,但这就是它.)
更一般地说,这是我真正想知道的:如何使用vba打开任何扩展名(可以在文本编辑器中查看)和长度(不超过VBA的字符串限制)的文件,以及确保我在基本文本编辑器中看到的任何字符都被读入字符串?(如果不能(轻松)完成,我当然会感谢被指向一种可能与.html文件一起使用的方法)非常感谢你的帮助
编辑:这是我使用建议的方法时会发生什么的一个例子.特别
Dim oFSO As Object
Dim oFS As Object, sText As String
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFS = oFSO.OpenTextFile(Path)
Do Until oFS.AtEndOfStream
sText = oFS.ReadAll()
Loop
FileToString = sText
Set oFSO = Nothing
Set oFS = Nothing
End Function
Run Code Online (Sandbox Code Playgroud)
我将向您展示开头(通过消息框)和结束(通过即时窗口),因为两者在不同方面都很奇怪.在这两种情况下,我都会将它与chrome中显示的html源的屏幕截图进行比较:
开始:

结束:


这是一种方法
Option Explicit
Sub test()
Dim oFSO As Object
Dim oFS As Object, sText As String
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFS = oFSO.OpenTextFile("C:\Users\osknows\Desktop\import-store.csv")
Do Until oFS.AtEndOfStream
' sText = oFS.ReadLine 'read line by line
sText = oFS.ReadAll()
Debug.Print sText
Loop
End Sub
Run Code Online (Sandbox Code Playgroud)
编辑:
尝试将以下行更改为以下3行之一,看看它是否有任何区别
http://msdn.microsoft.com/en-us/library/aa265347(v=vs.60).aspx
Set FS = FSO.OpenTextFile("C:\Users\osknows\Desktop\import-store.csv", 1, 0)
Set FS = FSO.OpenTextFile("C:\Users\osknows\Desktop\import-store.csv", 1, 1)
Set FS = FSO.OpenTextFile("C:\Users\osknows\Desktop\import-store.csv", 1, 2)
Run Code Online (Sandbox Code Playgroud)
EDIT2:
这些代码在你那正常吗?
Function ExecuteWebRequest(ByVal url As String) As String
Dim oXHTTP As Object
Set oXHTTP = CreateObject("MSXML2.XMLHTTP")
oXHTTP.Open "GET", url, False
oXHTTP.send
ExecuteWebRequest = oXHTTP.responseText
Set oXHTTP = Nothing
End Function
Function OutputText(ByVal outputstring As String)
MyFile = ThisWorkbook.Path & "\temp.html"
'set and open file for output
fnum = FreeFile()
Open MyFile For Output As fnum
'use Print when you want the string without quotation marks
Print #fnum, outputstring
Close #fnum
End Function
Sub test()
Dim oFSO As Object
Dim oFS As Object, sText As String
Dim Uri As String, HTML As String
Uri = "http://www.forrent.com/results.php?search_type=citystate&page_type_id=city&seed=859049165&main_field=12345&ssradius=-1&min_price=%240&max_price=No+Limit&sbeds=99&sbaths=99&search-submit=Submit"
HTML = ExecuteWebRequest(Uri)
OutputText (HTML)
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFS = oFSO.OpenTextFile(ThisWorkbook.Path & "\temp.html")
Do Until oFS.AtEndOfStream
' sText = oFS.ReadLine 'read line by line
sText = oFS.ReadAll()
Debug.Print sText
Loop
End Sub
Run Code Online (Sandbox Code Playgroud)

| 归档时间: |
|
| 查看次数: |
19206 次 |
| 最近记录: |