使用 VBA 切换 Internet Explorer Windows

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14017321/
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 18:57:01  来源:igfitidea点击:

Switching Internet Explorer Windows with VBA

windowsexcelinternet-explorerexcel-vbaautomationvba

提问by user1919328

I have been working on an Excel macro to automate an Internet Explorer process at work. I'm incredibly close to figuring everything out, but at the end, the macro clicks on a "Download Report" button, which launches a new window. I need to initiate a few things on this new window but I cannot figure out how to change the focus of the macro to the new window. I have found a few things online, but they have been unsuccessful. I know there are ways to toggle between browser windows by telling Excel to look for the name of the browser window, and to activate it, but parts of the name of the new window are constantly changing (it's a unique URL for that particular window).

我一直在使用 Excel 宏来自动化工作中的 Internet Explorer 进程。我非常接近解决所有问题,但最后,宏单击“下载报告”按钮,启动一个新窗口。我需要在这个新窗口上启动一些事情,但我不知道如何将宏的焦点更改为新窗口。我在网上找到了一些东西,但都没有成功。我知道有一些方法可以通过告诉 Excel 查找浏览器窗口的名称并激活它来在浏览器窗口之间切换,但是新窗口的部分名称不断变化(它是该特定窗口的唯一 URL) .

I was wondering if there might be a way to have the Macro look for just the first Nine letters of the window and switch to that window? Similar to the LEFT function in excel? The reason being is that the first 9 characters of the URL never change. The name of the original window (the one I'm trying to switch from) never changes, so maybe I could tell Excel to look for the the Browser Window that is NOT named the original one? Or any other ideas?

我想知道是否有办法让宏只查找窗口的前九个字母并切换到该窗口?类似于excel中的LEFT函数?原因是 URL 的前 9 个字符永远不会改变。原始窗口的名称(我试图从中切换的那个)永远不会改变,所以也许我可以告诉 Excel 查找未命名为原始窗口的浏览器窗口?还是有其他想法?

Thanks in advance for any help

在此先感谢您的帮助

回答by Mr Smith

Is the VBA AppActivate command what you are looking for?

VBA AppActivate 命令是您要查找的命令吗?

回答by Sorceri

You will need to add the references to the Internet Controls and shell. You just need to loop through the IE windows and look at the left hand side of the url. See below

您将需要添加对 Internet 控件和外壳程序的引用。您只需要循环浏览 IE 窗口并查看 url 的左侧。见下文

Function GetInternetWindow(urlToLookFor as String) As InternetExplorer

Dim winShell As Shell
Dim ie As InternetExplorer

Set winShell = New Shell
'loop through the windows and look at the urls
For Each ie In winShell.Windows
    If Left(ie.LocationURL, 9) = urlToLookFor Then        
        GetInternetWindow = ie
        Exit For
    End If
Next
End Function