在 IE 中打开一个文档,并能够将其作为变量存储在 vbs 中

Open a Document in IE and be able to store it as a variable in vbs

我正在尝试编写一个 vbs 程序,我在其中导航到网页,单击 link 打开 excel 中的文件,然后 运行 excel 文件。我知道如何导航到网页并单击 link。我需要帮助弄清楚如何以 vbs 程序可以操纵它的方式存储 excel 文件。

Set IE = WScript.CreateObject("InternetExplorer.Application", "IE_")
IE.Visible = True
IE.Navigate ("https://www.whateverWebsite.com/")

Dim LinkHref    
LinkHref = "analyze" 'the key word that will be in the link to click
Dim a

For Each a In IE.Document.GetElementsByTagName("a") ' for every element whose tag name starts with 'a' for "a href" pull out its contents
    If InStr((a.GetAttribute("href")), LinkHref)>0 Then 'checks to see if the link that is set contains the string stored in LinkHref
        a.Click 'click the link
    End If
Next

Dim objExcel  
Set objExcel = CreateObject("Excel.Application")

现在如何将使用 link 打开的 excel 文件附加到 objExcel?

您可以通过 GetObject:

附加到 运行 Excel 实例
Set xl = GetObject(, "Excel.Application")

不过,如果您启动了多个实例,那只会让您启动第一个实例。您必须终止第一个实例才能进入第二个实例。

话虽如此,更好的方法是让 Excel 直接打开 URL:

Set xl = CreateObject("Excel.Application")
For Each a In IE.Document.GetElementsByTagName("a")
    If InStr(a.href, LinkHref) > 0 Then
        Set wb = xl.Workbooks.Open(a.href)
        Exit For
    End If
Next