[英]Excel Object in Access VBA - Web Scraper - Open excel file or Refer to Excel File which is already open
Question: The Code works when I use it with a file that is closed and on the desktop. 问题:当我在桌面上关闭的文件中使用该代码时,该代码有效。 But it will not work with an excel file, which is already open.
但是它不适用于已经打开的excel文件。
I changed Set xlWbk = Workbooks.Open to Set xlWbk = GetObject, but this did not work. 我将Set xlWbk = Workbooks.Open更改为Set xlWbk = GetObject,但这没有用。
Sub MSAcessGoogleWebScraper()
'Tools/References...
'Microsoft HTML Object Library
'Microsoft Internet Controls
'Microsoft Excel 16.0 Object Library
' Enter some ticker symbols in row 1 of the excel worksheet
' Save file to your desktop
'---------------------------------------------------
' Open Excel Workbook
Dim xlApp As Excel.Application
Dim xlWbk As Excel.Workbook
Dim xlSht As Excel.Worksheet
Set xlApp = Excel.Application
Set xlWbk = Workbooks.Open("C:\Users\XXXXXX\Desktop\Book1.xlsx")
Set xlSht = Worksheets(1)
xlApp.Visible = True
xlWbk.Activate
xlSht.Activate
' Start WebScraper
Dim IE As New InternetExplorer
Dim DOC As HTMLDocument
Dim Row_Company As Range
Dim Row_Price As Range
Range("A1").Select
Do Until ActiveCell.Value = ""
IE.navigate " https://www.google.com/finance?q= " & ActiveCell.Value
IE.Visible = False
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Set DOC = IE.Document
Set Row_Company = ActiveCell.Offset(0, 1)
Set Row_Price = ActiveCell.Offset(0, 2)
Row_Company = DOC.getElementsByClassName("appbar-snippet-primary")(0).innerText
Row_Price = DOC.getElementsByClassName("pr")(0).innerText
ActiveCell.Offset(1, 0).Select
Loop
IE.Quit
End Sub
In your example, the worksheet variable "xlSht" is assigned with the current worksheet and therefore with the worksheet from the workbook that is currently opened. 在您的示例中,工作表变量“ xlSht”被分配给当前工作表,并因此被分配给当前打开的工作簿中的工作表。 Try this instead:
尝试以下方法:
Set xlApp = Excel.Application
Set xlWbk = Workbooks.Open("C:\Users\XXXXXX\Desktop\Book1.xlsx")
Set xlSht = xlWbk.Worksheets(1)
This is the solution to the Queston. 这是Queston的解决方案。 I found it "Using Access 2010 VBA list all open Excel Workbooks"
我发现它“使用Access 2010 VBA列出所有打开的Excel工作簿”
Option Compare Database
Option Explicit
Sub MSAcessGoogleWebScraper()
'Tools/References...
'Microsoft HTML Object Library
'Microsoft Internet Controls
'Microsoft Excel 16.0 Object Library
' Enter some ticker symbols in row 1 of the excel worksheet
' Save file to your desktop
'---------------------------------------------------
' Open Excel Workbook
Dim xlApp As Excel.Application
Dim xlWbk As Excel.Workbook
Dim xlSht As Excel.Worksheet
Set xlApp = GetObject(, "Excel.Application")
Set xlWbk = xlApp.Workbooks(1)
Set xlSht = xlWbk.Worksheets(1)
xlApp.Visible = True
xlWbk.Activate
xlSht.Activate
' Start WebScraper
Dim IE As New InternetExplorer
Dim DOC As HTMLDocument
Dim Row_Company As Range
Dim Row_Price As Range
xlApp.Range("A1").Select
Do Until xlApp.ActiveCell.Value = ""
IE.navigate " https://www.google.com/finance?q= " & xlApp.ActiveCell.Value
IE.Visible = False
Do
DoEvents
Loop Until IE.ReadyState = READYSTATE_COMPLETE
Set DOC = IE.Document
Set Row_Company = xlApp.ActiveCell.Offset(0, 1)
Set Row_Price = xlApp.ActiveCell.Offset(0, 2)
Row_Company = DOC.getElementsByClassName("appbar-snippet-primary")(0).innerText
Row_Price = DOC.getElementsByClassName("pr")(0).innerText
xlApp.ActiveCell.Offset(1, 0).Select
Loop
IE.Quit
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.