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

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

问题描述

这感觉应该很简单.我的计算机上存储了一个 .HTML 文件,我想将整个文件读入一个字符串.当我尝试超级简单的

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.)

更一般地说,这就是我真正想知道的:如何使用 vba 打开任何扩展名(可以在文本编辑器中查看)和长度(不超过 VBA 的字符串限制)的文件),并确保我在基本文本编辑器中看到的任何字符都会被读入字符串?(如果这不能(轻松)完成,我当然会很高兴被指出一种可能适用于 .html 文件的方法)非常感谢您的帮助

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

这是我使用建议的方法时发生的情况的示例.具体

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

我将向您展示开头(通过消息框)和结尾(通过即时窗口),因为两者在不同方面都很奇怪.在这两种情况下,我都会将其与 chrome 中显示的 html 源的屏幕截图进行比较:

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:

开头:

结束:

推荐答案

好吧,我终于想通了.VBA 文件系统对象只能读取 asciiII 文件,我已将我的文件保存为 unicode.有时,就我而言,保存 asciiII 文件可能会导致错误.但是,您可以通过将文件转换为二进制文件,然后再转换回字符串来解决此问题.详细信息在此处说明 http://bytes.com/topic/asp-classic/answers/521362-write-xmlhttp-result-text-file.

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天全站免登陆