vbs将文本从IE复制到Excel

时间:2016-05-30 10:55:31

标签: excel internet-explorer vbscript copy

任何人都可以帮我一个简单的vbs脚本,它将字段qwidget_lastsale从这个站点复制到excel表吗?

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

我一直在尝试修改现有脚本,但似乎无法使其正常工作。 Sofar我可以打开网站并取得优异成绩,但无法复制该网站。

我希望在此之后有复制脚本:

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

2 个答案:

答案 0 :(得分:1)

您需要使用DOM操作,并且 NOT 很快就会从内存中释放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工作簿交互时,最好分别使用ApplicationWorkbookWorksheet个对象的变量并使用它。这可以确保您始终在打算的工作表上,并确保您在正确时关闭正确的工作簿。

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

答案 1 :(得分: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
相关问题