'程序没有问题,只有一个常量不对
Option ExplicitPrivate Const Form_ID = 2 
'这个常量出问题了。如果定义的是1那么所选定的Form在html中的Forms数组中是第二个
'通过分析html页面,可以看出,程序需要的Form是第三个.
'所以,将Form_ID赋值为2就可以了.'另外,如果你想看懂Case中的代码,可能需要了解一些关于HTML的基本常识。Dim Code(9) As String
Dim Current As Long
Private Sub Form_Load()
  Form1.MousePointer = 11
  ' 以下是个股代码
  ' 为了程序简洁,这里仅使用九只代码。
  ' 而在真实环境中,应从数据文件中读入全部个股代码。
  Code(0) = "600001": Code(1) = "600002": Code(2) = "600003"
  Code(3) = "600005": Code(4) = "600006": Code(5) = "600007"
  Code(6) = "600008": Code(7) = "600009": Code(8) = "600010"
  Current = 0
  WebBrowser1.Navigate "www.stockstar.com.cn"   ' 起始网址
End SubPrivate Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
  Dim i, k
  Text2 = WebBrowser1.LocationURL    ' 显示当前网址
  ' 判断当前网页是否全部调入完毕
  If Not (pDisp Is WebBrowser1.Object) Then Exit Sub
 ' On Error Resume Next
  Select Case Text2
  Case "http://www.stockstar.com.cn/home.htm"  ' 当进入主页面时执行以下程序
   For i = 0 To WebBrowser1.Document.Forms(Form_ID).length - 1
      ' 找到代码输入框后填入个股代码
      If WebBrowser1.Document.Forms(Form_ID)(i).Name = "code" Then _
        WebBrowser1.Document.Forms(Form_ID)(i).Value = Code(Current)
      ' 在下拉式列表中进行选择
      If WebBrowser1.Document.Forms(Form_ID)(i).Name = "target" Then
        For k = 0 To WebBrowser1.Document.Forms(Form_ID)(i).length - 1
           If WebBrowser1.Document.Forms(Form_ID)(i).Options(k).Text _
                     = "个股资料" Then
             WebBrowser1.Document.Forms(Form_ID)(i).Options(k).Selected = True
             Exit For
           End If
        Next k
      End If
      ' 点击按钮
      If WebBrowser1.Document.Forms(Form_ID)(i).Value = " 查询 " Then _
        WebBrowser1.Document.Forms(Form_ID)(i).Click
   Next
  Case Else   ' 当进入数据页面时执行以下程序
   For i = 0 To WebBrowser1.Document.All.length - 1
      If WebBrowser1.Document.All(i).tagName = "PRE" Then
        ' 精确提取数据
        Text1 = Text1 + Code(Current) + vbCrLf + _
                WebBrowser1.Document.All(i).innerText + vbCrLf
        Exit For
      End If
   Next
   ' 数据存盘
   Open "C:\Data2.Txt" For Append As #1
   Print #1, Text1: Text1 = "": Close #1
   ' 换下一只股票
   Current = Current + 1
   If Current >= 9 Then
     ' 上网任务完成后,应在此调用自动挂断过程。
     Form1.MousePointer = 0: MsgBox "Finished!": End
   End If
   ' 回退到主页面,查询下一只股票的信息
   WebBrowser1.GoBack
  End Select
End Sub

解决方案 »

  1.   

    几位大侠高人哦!还有下面几行代码不明白,请教请教:'××××WebBrowser1.Document.Forms(Form_ID).length 是啥意思?
    For i = 0 To WebBrowser1.Document.Forms(Form_ID).length - 1
          ' 找到代码输入框后填入个股代码
          '××××WebBrowser1.Document.Forms(Form_ID)(i).Name = "code"是什么意思?为什么要这个if的判断?
          If WebBrowser1.Document.Forms(Form_ID)(i).Name = "code" Then _
            WebBrowser1.Document.Forms(Form_ID)(i).Value = Code(Current)
          ' 在下拉式列表中进行选择
          '××××下面的这个if和for、if是干什么的?
          If WebBrowser1.Document.Forms(Form_ID)(i).Name = "target" Then
            For k = 0 To WebBrowser1.Document.Forms(Form_ID)(i).length - 1
               If WebBrowser1.Document.Forms(Form_ID)(i).Options(k).Text _
                         = "个股资料" Then
                 WebBrowser1.Document.Forms(Form_ID)(i).Options(k).Selected = True
                 Exit For
               End If
            Next k
          End If
          ' 点击按钮
          If WebBrowser1.Document.Forms(Form_ID)(i).Value = " 查 询 " Then _
            WebBrowser1.Document.Forms(Form_ID)(i).Click
       Next
      Case Else   ' 当进入数据页面时执行以下程序
       '××××WebBrowser1.Document.All.length 是啥意思?
       For i = 0 To WebBrowser1.Document.All.length - 1
       '××××WebBrowser1.Document.All(i).tagName = "PRE"是啥意思?
          If WebBrowser1.Document.All(i).tagName = "PRE" Then
            ' 精确提取数据
            Text1 = Text1 + Code(Current) + vbCrLf + _
                    WebBrowser1.Document.All(i).innerText + vbCrLf
            Exit For
          End If
       Next请各位大侠帮帮忙告诉一下我上面的带“××××”的代码的意思好吗?
    我很菜,sorry。
    谢谢先!
      

  2.   

    '××××WebBrowser1.Document.Forms(Form_ID).length 是啥意思?
    For i = 0 To WebBrowser1.Document.Forms(Form_ID).length - 1
    -----------------------------------------
    A:length是指在Forms(Form_ID)这个Form中的对象个数
    ---------------------------------
     '××××WebBrowser1.Document.Forms(Form_ID)(i).Name = "code"是什么意思?为什么要这个if的判断?
          If WebBrowser1.Document.Forms(Form_ID)(i).Name = "code" Then _
    ------------------------------------
    A:code是指Forms(Form_ID)(i)这个对象的名字,一般在Html中表现为<input type=... name="code">,当然还有其它对象:select,textarea,iframe 等等。-----------------------------------------------
         '××××下面的这个if和for、if是干什么的?
          If WebBrowser1.Document.Forms(Form_ID)(i).Name = "target" Then
            For k = 0 To WebBrowser1.Document.Forms(Form_ID)(i).length - 1
               If WebBrowser1.Document.Forms(Form_ID)(i).Options(k).Text _
                         = "个股资料" Then
    ---------------------------------------
    A:循环判断,上面已经讲了,If的作用是判断对象的某个属性是否符合。
    -------------------------------------- '××××WebBrowser1.Document.All.length 是啥意思?
       For i = 0 To WebBrowser1.Document.All.length - 1
       '××××WebBrowser1.Document.All(i).tagName = "PRE"是啥意思?
    -------------------------------------
    A:Document,All.length是整个Document对象中的子对象个数
      tagName也是对象的一种属性。
    ---------------------------
    如果你可以看懂VB的代码,那么你需要了解一下HTML的一些基本知识,可以到Web版中去了解一下,或者查找相关资料。
      

  3.   

    另外,哪位大侠愿意帮我学通这类编程,我愿意出3000元培训费!绝不食言!
    我的msn(email):[email protected]
    谢谢联系!
      

  4.   

    我的MSN: [email protected]
      

  5.   

    TO boybluesky(boybluesky):紫竹桥附近一家小公司...
      

  6.   

    hsn1982(我 爱 猫 猫) 大侠给个msn如何?有空请教请教!我有大量这方面的东东要请教请教的。好吗?
    我的msn:[email protected]
    不管如何,先行谢过!