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 :
例如,假设我有三个网址:
Now I need to get the title of these html pages in another column. How do I do it?
现在我需要在另一列中获取这些html页面的标题。我该怎么做?
2 个解决方案
#1
5
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
#2
6
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
#1
5
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
#2
6
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