'How to find the title of an URL using VBA in MS Excel
I am working on a VBA project to get Page title and created date from an URL. I found this code online but it's not working since IE is not available. Is there any way I can get the title without IE?
Function GetPageTitle(a As String)
Set ie = CreateObject("InternetExplorer.Application")
With ie
.Visible = False
.Navigate a
Do Until .ReadyState = 4
DoEvents
Loop
GetPageTitle = .document.Title
.Quit
End With
End Function
I have tried to use Edge as Internet Explorer but it's not working. I have tried this code also HTML Page Title in Excel VBA
Solution 1:[1]
Get Webpage Title
Option Explicit
Sub GetPageTitleTEST()
Const URL As String = "https://www.google.com"
Dim PageTitle As String: PageTitle = GetPageTitle(URL)
If Len(PageTitle) > 0 Then
Debug.Print PageTitle
Else
Debug.Print "Nope."
End If
End Sub
Function GetPageTitle( ByVal URL As String) As String
Dim Response As String
On Error GoTo ClearError
With CreateObject("MSXML2.ServerXMLHTTP")
.Open "Get", URL, False
.send
Response = .responseText
End With
With CreateObject("htmlfile")
.body.innerHTML = Response
GetPageTitle = .getElementsByTagName("title")(0).innerText
End With
ProcExit:
Exit Function
ClearError:
Debug.Print "'GetPageTitle' Run-time error '" _
& Err.Number & "':" & vbLf & " " & Err.Description
Resume ProcExit
End Function
Solution 2:[2]
If you want to automate Edge with VBA, you need to use SeleniumBasic. SeleniumBasic is a Selenium based browser automation framework for VB.Net, VBA and VBScript.
You can follow the steps below:
- Download the latest version of SeleniumBasic v2.0.9.0 from this link and install it.
- Download the corresponding version of Edge WebDriver from this link.
- Find the path of SeleniumBasic which is
C:\Users\%username%\AppData\Local\SeleniumBasicin my computer (it might also be in this pathC:\Program Files\SeleniumBasic), copy the Edge WebDrivermsedgedriver.exeto this path. - Rename
msedgedriver.exetoedgedriver.exe. - Open Excel and write the VBA code.
- In the VBA code interface, click Tools > References, add Selenium Type Library reference and click OK to save.
- Sample VBA code to automate Edge using SeleniumBasic to get URL title:
Public Sub Selenium()
Dim bot As New WebDriver
bot.Start "edge", "https://www.bing.com"
bot.Get "/"
bot.Wait 5000
Debug.Print bot.Window.Title 'this line of code can get title
End Sub
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ashleedawg |
| Solution 2 | Yu Zhou |
