2017-02-26 35 views
0

新手问题传入;我正在YouTube上关注如何在网页上显示废料信息的网络系列。我的目标是能够在一个内部公司应用程序上使用它,它只是显示一个带有文本值的空白网页。然而,两天后,我仍然无法弄清楚为什么我无法在VB中进行简单的函数调用。当我在窗体上单击按钮'Private Sub CommandButton1_Click()'时,我所做的所有事情都会继续发出编译错误。Visual Basic - 简单的Web刮板

下面是按钮的代码:

Private Sub CommandButton1_Click() 
'FirstMacro 

    Set objIE = CreateObject("InternetExplorer.Application") 
    objIE.Top = 0 
    objIE.Left = 0 
    objIE.Width = 800 
    objIE.Height = 600 
    objIE.AddressBar = 0 
    objIE.StatusBar = 0 
    objIE.Toolbar = 0 
    objIE.Visible = True 'We will see the window navigation' 

    objIE.Navigate ("http://www.google.com") 
    TextBox4.Text objIE.Document.body.innerHTML 
End Sub 

下面是类代码:

Public Function FirstMacro() 
'the_start: 

    Set objIE = CreateObject("InternetExplorer.Application") 
    objIE.Top = 0 
    objIE.Left = 0 
    objIE.Width = 800 
    objIE.Height = 600 
    objIE.AddressBar = 0 
    objIE.StatusBar = 0 
    objIE.Toolbar = 0 
    objIE.Visible = True 'We will see the window navigation' 

    'MsgBox.Err.Number 

    'On Error Resume Next 
     'MsgBox.objIE.Document.body.innerHTML 
     'If Err.Number > 0 Then 
      'objIE.Quit 
      'Set objIE = Nothing 
      'GoTo the_start: 
     'End If 

    objIE.Navigate ("http://www.google.com") 

    'Do 
     'DoEvents 
    'Loop Until objIE.ReadyState = 4 

    TextBox4.Text objIE.Document.body.innerHTML 
End Function 

这是我继系列:https://www.youtube.com/watch?v=Blls6GrCBCY&index=12&list=PL6OYc4rwKjcOu3UL7LYpvO_S2waYO-hVU

谢谢帮助noob。

enter image description here

回答

0

你或许应该尝试这样的事情。

Sub DumpData() 

Set IE = CreateObject("InternetExplorer.Application") 
IE.Visible = True 

URL = "http://finance.yahoo.com/q?s=sbux&ql=1" 

'Wait for site to fully load 
IE.Navigate2 URL 
Do While IE.Busy = True 
    DoEvents 
Loop 

RowCount = 1 

With Sheets("Sheet1") 
    .Cells.ClearContents 
    RowCount = 1 
    For Each itm In IE.document.all 
     .Range("A" & RowCount) = itm.tagname 
     .Range("B" & RowCount) = itm.ID 
     .Range("C" & RowCount) = itm.classname 
     .Range("D" & RowCount) = Left(itm.innertext, 1024) 

     RowCount = RowCount + 1 
    Next itm 
End With 
End Sub 

然后,你应该有你在每个URL处理什么用更好的主意。