2016-05-30 75 views
1

任何人都可以用一个简单的vbs脚本来帮助我,它将字段qwidget_lastsale从此站点复制到excel表单中?vbs将文本从IE复制到Excel

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

我一直在试图修改现有脚本,但似乎无法使它发挥作用。 Sofar我可以打开该网站和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

回答

1

您需要使用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有一个READYSTATE enumeration返回一个Long。您可以测试以查看是否在页面加载,而不是睡了1秒,希望最好...

  • 当您使用CreateObject("Excel.Application")一个Excel的实例的对象返回应用对象 - 无需要再次参考。你会注意到我拿走了那些。

  • 与Excel工作簿交互时,最好分别使用Application,WorkbookWorksheet对象的变量并使用它。这可以确保您总是在意图的工作表上,并且您正在关闭右侧工作簿时的右侧时间。

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

+0

感谢您的提示和建议。我想继续使用宏芒建议,但我得到一个错误:对象不支持此属性或方法'objExplorer.Document.getElementByID(...)。值。代码800A01B6。我能否在页面上选择了错误的元素,或者为什么不支持? – user24

+0

我设法使用.innerText而不是.Value来处理上述建议。谢谢你的帮助。 – user24

1

试试这个代码

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 
+0

在这段代码中,我用“”字符分割了网站中的数据 –