vbs将文本从IE复制到Excel

任何人都可以帮助我一个简单的VBS脚本从该站点复制字段qwidget_lastsale到Excel表?

http://www.nasdaq.com/symbol/abt/recommendations

我一直在尝试修改现有的脚本,但似乎无法使其工作。 我可以打开网站和excel,但不能复制该字段。

我想这之后有副本脚本:

Set objExplorer = CreateObject("InternetExplorer.Application") WebSite = "http://www.nasdaq.com/symbol/abt/recommendations" with objExplorer .Navigate2 WebSite .left=5 .top=5 .height=1100 .width=700 .AddressBar = 0 .Visible = 1 .ToolBar = 0 .StatusBar = 1 WScript.Sleep 1000 Set objIE = Nothing end with Set xl = CreateObject("Excel.application") xl.Application.Workbooks.Open "C:\Users\user\Documents\testauto.xlsx" xl.Application.Visible = True 

最好的问候user24

你需要使用DOM操作,而不是很快从内存中释放你的objExplorer

 Set objExplorer = CreateObject("InternetExplorer.Application") WebSite = "http://www.nasdaq.com/symbol/abt/recommendations" Const READYSTATE_COMPLETE As Long = 4 With objExplorer .Navigate2 WebSite .Left=5 .Top=5 .Height=1100 .Width=700 .AddressBar = 0 .Visible = 1 .ToolBar = 0 .StatusBar = 1 '//WScript.Sleep 1000 Do Until .ReadyState = READYSTATE_COMPLETE WScript.Sleep 1000 Loop '//Set objIE = Nothing (your variable isn't called 'objIE' anyway?) End With Set xl = CreateObject("Excel.Application") xl.Visible = True Set wb = xl.Workbooks.Open("C:\Users\user\Documents\testauto.xlsx") Set ws = wb.Sheets("Sheet1") '// Change name of sheet as required ws.Range("A1").Value = objExplorer.Document.getElementById("qwidget_lastsale").Value '// Rest of code.... '// ... '// NOW clear down your variables. objExplorer.Quit wb.Close SaveChanges:=True xl.Quit Set ws = Nothing Set wb = Nothing Set xl = Nothing Set objExplorer = Nothing 

另外,正如你所看到的,我改变了一些东西:

  • Internet Explorer具有一个返回LongREADYSTATE枚举 。 你可以testing这个看看页面是否已经加载,而不是睡1秒,希望最好的…

  • 当您使用CreateObject("Excel.Application")创buildExcel的实例时,返回的对象应用程序对象 – 无需再次引用它。 你会注意到我拿走了那些。

  • 与Excel工作簿交互时,最好分别为ApplicationWorkbookWorksheet对象使用variables,并使用它。 这可以确保您始终处于您打算成为的表格上,并且能够在正确的时间closures正确的工作簿。

  • 代码缩进是你的朋友 – 它使得一切都更容易阅读和遵循。

试试这个代码

 Function Read(URL) Set ie = Wscript.CreateObject("InternetExplorer.Application") Set objFSO = Wscript.CreateObject("Scripting.FileSystemObject") ie.Navigate(URL) ie.Visible = 1 DO WHILE ie.busy wscript.sleep 100 LOOP Data = ie.document.documentElement.innertext Msgbox(Data) sp = Split(Data," ") b = ubound(sp) Msgbox(b) For i=0 to b Msgbox(sp(i)) Next selectexcel=inputbox("Enter the location","Location of the excel file(Xls/xlsx)","Enter your path here !") Set objExcel = CreateObject("Excel.Application") Set objWorkbook = objExcel.Workbooks.Open(selectexcel) objExcel.visible=True rowCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Rows.count colCount=objExcel.ActiveWorkbook.Sheets(1).UsedRange.Columns.count For i=1 to b Step 1 For j=1 to 1 Step 1 objExcel.Cells(i,j).Value=sp(i) k=k+1 Next Next End Function Read "http://www.nasdaq.com/symbol/abt/recommendations" Set ie = Nothing