2016-05-30 76 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

在這段代碼中,我用「」字符分割了網站中的數據 –