首页 > 解决方案 > VBA检查IE url是否已更改

问题描述

我一直在环顾四周,但似乎找不到这个问题的答案。我目前正在尝试使用 oauth2 连接到 API。

我可以很容易地在 IE 中显示登录屏幕。

Dim IE As InternetExplorer
Set IE = CreateObject("InternetExplorer.Application")
Dim url as string
url = "www.loginpage.com"


IE.Navigate url
IE.Visible = True

每当输入用户名和密码时,url 都会更改为带有授权码的重定向 url。我想将此重定向 url 存储到另一个变量,因此我可以减去授权代码并将其用于下一步。

我一直在尝试一个简单的如果,但到目前为止没有任何运气。第一个 url 变量填充了显示登录页面的 url。所以我想如果当前 url 与那个 url 不同,它可以存储在一个新变量中。可悲的是,我收到了运行时错误 80004005。也许你们知道更好的方法来做到这一点?

Dim url As String
Dim newurl  As String

If IE.Document.url <> url Then

newurl = IE.Document.url
MsgBox (newurl)

End If

标签: vbaapiinternet-exploreroauth-2.0

解决方案


你也可以ie.LocationURL用来获取网址。我用 bing 做了一个例子,它可以很好地工作:

Sub LOADIE()
    Set ieA = CreateObject("InternetExplorer.Application")
    ieA.Visible = True
    Dim url As String
    url = "https://www.bing.com/"
    ieA.navigate url
    Do Until ieA.readyState = 4
       DoEvents
    Loop
    
    Set doc = ieA.Document
    doc.getElementById("sb_form_q").Value = "VBA"
    doc.getElementById("sb_form_go").Click
    Application.Wait (Now + TimeValue("00:00:05"))
    
    Dim newurl  As String
    If ieA.LocationURL <> url Then
    newurl = ieA.LocationURL
    MsgBox (newurl)
    End If
End Sub

结果:

在此处输入图像描述


推荐阅读