AppActivate返回到Excel

我正在编写一个代码来从网站下载文件,但我必须使用Chrome。 我使用shell命令在所需的URL中打开Goog​​le Chrome,并在shell命令之后尝试使用AppActivate命令,但是它不起作用。

命令如下

Sub DL(URL as string) Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url URL") AppActivate ("MicroSoft Excel - SIEVE") End Sub 

SIEVE是我工作簿的名字。 我试过只使用MicroSoft Excel,Excel和其他组合,但只使用MicroSoft Excel和Microsoft Excel – SIEVE我没有收到错误消息。

谁能帮我?

我不认为你需要包括你的工作簿的名称。

尝试这个:

 Sub DL(URL As String) Shell ("C:\Program Files (x86)\Google\Chrome\Application\chrome.exe -url URL") AppActivate "MicroSoft Excel" End Sub 

如果您打开了多个工作簿并将错误的工作簿带入查看,则可以在AppActivate之前始终插入此工作簿:

 Workbooks("SIEVE").Activate 

编辑

AppActivate的原因似乎不工作,因为它工作得太快。 该代码在Chrome有机会完全打开之前执行,所以Chrome AppActivate发生后被激活。 我看到的最好的select是使用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 

你可以试试这个:

 On Error Resume Next **AppActivate Application.Caption** On Error GoTo 0