vba VBA打开URL,等待5秒,再打开另一个URL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26864518/
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
VBA to open URL, wait for 5 seconds, then open another URL
提问by espen4001
I have a webpage I want to open (URL is generated out of cell values in Excel), but going to that URL directly requires logging in to open. But if I first open the mainpage of the server, I have automatic login, and then I'm able to open the first URL without the need for user/pass.
我有一个要打开的网页(URL 是从 Excel 中的单元格值生成的),但是直接访问该 URL 需要登录才能打开。但是如果我首先打开服务器的主页,我有自动登录,然后我就可以打开第一个网址而无需用户/密码。
So far I have this code, which opens IE with both URLs at the same time, in the same window, but different tabs, exactly as I want it to do, except URL2 requires the login. To get around the login, I would like to add a pause between the navigate and navigate2, so the first page can complete loading before the second URL opens.
到目前为止,我有这段代码,它同时打开带有两个 URL 的 IE,在同一个窗口中,但在不同的选项卡中,完全符合我的要求,除了 URL2 需要登录。为了绕过登录,我想在导航和导航 2 之间添加一个暂停,以便第一页可以在第二个 URL 打开之前完成加载。
Can anyone help me with this?
谁能帮我这个?
Edit: I have tried the suggestions from below, but it still needs the login. I have tried another solution, which is not optional, but it works. It consists of two buttons, running different macros, where the first one opens the main page to get the login, and the second one opens the next URL. I have written them as follows:
编辑:我已经尝试了下面的建议,但它仍然需要登录。我尝试了另一种解决方案,这不是可选的,但它有效。它由两个按钮组成,运行不同的宏,第一个按钮打开主页以获取登录信息,第二个按钮打开下一个 URL。我把它们写成如下:
First one:
第一:
Sub login()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.example.mainpage.com"
End Sub
Second one:
第二个:
Sub search()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
End Sub
Is it possible to have a third macro running the other two with a delay between them?
是否可以让第三个宏运行其他两个宏,但它们之间有延迟?
Original code:
原始代码:
Sub open_url()
Dim IE As Object
Const navOpenInNewTab = &H800
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "http://www.example.mainpage.com"
'here I would like to add a pause for 5 seconds
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
End Sub
回答by Miguel Febres
Maybe it would be better to wait until the first page is fully loaded:
也许等到第一页完全加载会更好:
IE.Navigate "http://www.example.mainpage.com"
Do While IE.Busy Or Not IE.readyState = IE_READYSTATE.complete: DoEvents: Loop
IE.Navigate2 "http://www.example.mainpage.com" & Range("w1").Cells.Value & Range("W2").Cells.Value, CLng(navOpenInNewTab)
Note that the ReadyState enumREADYSTATE_COMPLETE has a numerical value of 4. This is what you should use in the case of late binding (always the case in VBScript).
请注意,ReadyState 枚举READYSTATE_COMPLETE 的数值为 4。这是您在后期绑定的情况下应该使用的值(在 VBScript 中始终如此)。
回答by Golemic
Do you mean something like:
你的意思是这样的:
Application.Wait(Now + TimeValue("00:00:05"))