vba AppActivate返回excel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21937053/
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
AppActivate to return to excel
提问by Fabio Rebelo
I'm writing a code to download a file from a website, but I must use Chrome for it. I used the shell command to open Google Chrome in the desired URL and right after the shell command I tried to use the AppActivate command but it is not working.
我正在编写代码来从网站下载文件,但我必须使用 Chrome。我使用 shell 命令在所需的 URL 中打开 Google Chrome,并在 shell 命令之后我尝试使用 AppActivate 命令,但它不起作用。
The command is as follow
命令如下
Sub DL(URL as string)
Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url URL")
AppActivate ("MicroSoft Excel - SIEVE")
End Sub
SIEVE is the name of my workbook. I've tried using only MicroSoft Excel, Excel and other combinantions, but only with MicroSoft Excel and Microsoft Excel - SIEVE I didn't get an error message.
SIEVE 是我的工作簿的名称。我曾尝试仅使用 MicroSoft Excel、Excel 和其他组合,但仅使用 MicroSoft Excel 和 Microsoft Excel - SIEVE 我没有收到错误消息。
Can anyone help me?
谁能帮我?
回答by ARich
I don't think you need to include the name of your workbook.
我认为您不需要包含工作簿的名称。
Try this:
尝试这个:
Sub DL(URL As String)
Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url URL")
AppActivate "MicroSoft Excel"
End Sub
If you have multiple workbooks open and the wrong workbook is brought to view, you could always insert this before the AppActivate
:
如果您打开了多个工作簿并且查看了错误的工作簿,您始终可以在以下内容之前插入它AppActivate
:
Workbooks("SIEVE").Activate
Edit
编辑
The reason AppActivate
doesn't appear to be working is because it works too soon. The code executes before Chrome has had a chance to fully open, so Chrome is activated afterAppActivate
occurs. The best option I see that you have is to use Application.Wait()
and wait until Chrome is loaded. Something like this:
原因AppActivate
似乎不起作用是因为它工作得太快了。代码在 Chrome 有机会完全打开之前执行,因此 Chrome在AppActivate
发生后被激活。我看到您拥有的最佳选择是使用Application.Wait()
并等待 Chrome 加载。像这样的东西:
Sub DL(URL As String)
Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url URL")
Application.Wait (Now + TimeValue("0:00:10")) '<~~ Waits ten seconds.
AppActivate "MicroSoft Excel"
End Sub
回答by karunamoorthy
You can try this:
你可以试试这个:
On Error Resume Next
**AppActivate Application.Caption**
On Error GoTo 0