使用VBA将本地HTML文件读入字符串 [英] Read Local HTML File into String With VBA

查看:2155
本文介绍了使用VBA将本地HTML文件读入字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这感觉就像这样简单。我有一个.HTML文件存储在我的电脑上,我想把整个文件读入一个字符串。当我尝试超级简单的

  Dim FileAsString as string 

打开C:\Myfile HTML输入为#1
输入#1,FileAsString
关闭#1

debug.print FileAsString

我没有得到整个文件。我只得到前几行(我知道立即关闭窗口,但这不是问题,我绝对不会把整个文件放入我的字符串。)我也尝试使用另一种方法使用文件系统对象,得到类似的结果,只有这个时候有很多奇怪的字符和问号被抛出。这让我觉得这可能是某种编码问题。 (虽然坦白说,我不完全明白这是什么意思,我知道有不同的编码格式,这可能会导致字符串解析的问题,但这是关于它。)



所以更一般地,这里是我真正想知道的:我如何使用vba打开任何扩展名的文件(可以在文本编辑器中查看)和长度(这不超过VBA的字符串限制) ,并确保在基本文本编辑器中看到的任何字符是什么被读入字符串? (如果这不能(很容易)完成,我一定会赞赏指向一个可能与.html文件一起使用的方法)非常感谢您的帮助



EDIT:
这是一个使用建议方法时会发生什么的示例。具体

  Dim oFSO As Object 
Dim oFS As Object,sText As String

Set oFSO = CreateObject(Scripting.FileSystemObject)
设置oFS = oFSO.OpenTextFile(路径)

直到oFS.AtEndOfStream
sText = oFS.ReadAll()
循环
FileToString = sText

设置oFSO =没有
设置oFS =没有

结束函数

我将通过不同的方式显示开始(通过消息框)和结束(通过即时窗口),因为它们都是奇怪的。在这两种情况下,我会将其与chrome中显示的html源的屏幕截图进行比较:



开始:





结束:



解决方案

好的,我终于设法弄清楚了。 VBA文件系统对象只能读取asciiII文件,而且我已将我的存储为unicode。有时,像我的情况,保存asciiII文件可能会导致错误。然而,您可以通过将文件转换为二进制文件,然后将其转换为字符串来解决此问题。详细说明请参见 http:// bytes。 com / topic / asp-classic / answers / 521362-write-xmlhttp-result-text-file


This feels like it should be simple. I have a .HTML file stored on my computer, and I'd like to read the entire file into a string. When I try the super straightforward

Dim FileAsString as string 

Open "C:\Myfile.HTML" for input as #1
Input #1, FileAsString
Close #1

debug.print FileAsString

I don't get the whole file. I only get the first few lines (I know the immediate window cuts off, but that's not the issue. I'm definitely not getting the whole file into my string.) I also tried using an alternative method using the file system object, and got similar results, only this time with lots of weird characters and question marks thrown in. This makes me think it's probably some kind of encoding issue. (Although frankly, I don't fully understand what that means. I know there are different encoding formats and that this can cause issues with string parsing, but that's about it.)

So more generally, here's what I'd really like to know: How can I use vba to open a file of any extension (that can be viewed in a text editor) and length (that's doesn't exceed VBA's string limit), and be sure that whatever characters I would see in a basic text editor are what gets read into a string? (If that can't be (easily) done, I'd certainly appreciate being pointed towards a method that's likely to work with .html files) Thanks so much for your help

EDIT: Here's an example of what happens when I use the suggested method. Specifically

    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

I'll show you both the beginning (via a message box) and the end (via the immediate window) because both are weird in different ways. In both cases I'll compare it to a screen capture of the html source displayed in chrome:

Beginning:

End:

解决方案

Okay so I finally managed to figure this out. The VBA file system object can only read asciiII files, and I had saved mine as unicode. Sometimes, as in my case, saving an asciiII file can cause errors. You can get around this, however, by converting the file to binary, and then back to a string. The details are explained here http://bytes.com/topic/asp-classic/answers/521362-write-xmlhttp-result-text-file.

这篇关于使用VBA将本地HTML文件读入字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆