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

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

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") 

现在我怎么把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