Excel VBA 中的 HTML 页面标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7625316/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 14:09:27  来源:igfitidea点击:

HTML Page Title in Excel VBA

htmlexcelvbaexcel-vbainternet-explorer

提问by redGREENblue

Given an url, how can I get the title of the html page in VBA in Excel?

给定一个 url,如何在 Excel 中的 VBA 中获取 html 页面的标题?

For example suppose I have three urls like :

例如,假设我有三个 url,如:

Now I need to get the title of these html pages in another column. How do I do it?

现在我需要在另一列中获取这些 html 页面的标题。我该怎么做?

回答by Benk

Remou's answer was VERY helpful for me, but it caused a problem: It doesn't close the Internet Explorer process, so since I needed to run this dozens of times I ended up with too many IEs open, and my computer couldn't handle this.

Remou 的回答对我很有帮助,但它引起了一个问题:它没有关闭 Internet Explorer 进程,所以由于我需要运行几十次,我最终打开了太多 IE,我的电脑无法处理这个。

So just add

所以只需添加

wb.Quit

and everything will be fine.

一切都会好起来的。

This is the code that works for me:

这是对我有用的代码:

Function GetTitleFromURL(sURL As String)
Dim wb As Object
Dim doc As Object

Set wb = CreateObject("InternetExplorer.Application")

wb.Navigate sURL

While wb.Busy
    DoEvents
Wend

GetTitleFromURL = wb.Document.Title

wb.Quit

Set wb = Nothing
End Function

回答by Fionnuala

I am not sure what you mean by title, but here is an idea:

我不确定你的标题是什么意思,但这里有一个想法:

Dim wb As Object
Dim doc As Object
Dim sURL As String

Set wb = CreateObject("InternetExplorer.Application")

sURL = "http://lessthandot.com"

wb.Navigate sURL

While wb.Busy
    DoEvents
Wend

''HTML Document
Set doc = wb.document

''Title
Debug.Print doc.Title

Set wb = Nothing