彰化一整天的論壇

 找回密碼
 立即註冊
查看: 84|回復: 2

VBA執行確認的問題

[複製鏈接]
發表於 2018-7-23 21:03:07 | 顯示全部樓層 |閱讀模式
我是做了一個vba用來執行網頁做到一半卻卡關了
請大大幫我看一下哪邊寫錯了
Sub SetValueToComboBox()
   Dim ie  As Object
   'Dim oHTML_Element As IHTMLElement

   Set ie = CreateObject("InternetExplorer.Application")
   ie.Visible = True
   ie.navigate "http://home13.compeq.com.tw/ctshopfloor/WipEquipParams/tts2Entry.asp"
   While ie.Busy Or ie.readyState <> 4: DoEvents: Wend

   Set oHTML_Element = ie.document.getElementsByName("dept_code")(0)
   If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "B20"
   Application.Wait (Now + TimeValue("0:00:01"))
   '呼叫onchange
   oHTML_Element.fireevent "onchange"   '執行 onchange事件
   Application.Wait (Now + TimeValue("0:00:03"))
   Set oHTML_Element = ie.document.getElementsByName("equip_name")(0)
   If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "水平冷卻線"
   Application.Wait (Now + TimeValue("0:00:01"))
   oHTML_Element.fireevent "onchange"   '執行 onchange事件
   Application.Wait (Now + TimeValue("0:00:02"))
   
   Set oHTML_Element = ie.document.getElementsByName("line_name")(0)
   If Not oHTML_Element Is Nothing Then
   oHTML_Element.Value = Sheet1.Range("B2").Value
   End If
       Application.Wait (Now + TimeValue("0:00:01"))
    '呼叫onchange
   oHTML_Element.fireevent "onchange"   '執行 onchange事件
   Application.Wait (Now + TimeValue("0:00:01"))
   Set oHTML_Element = ie.document.getElementsByName("work_num")(0)
   If Not oHTML_Element Is Nothing Then
   oHTML_Element.Value = Sheet1.Range("D2").Value
   End If
   Application.Wait (Now + TimeValue("0:00:01"))
   
   Set oHTML_Element = ie.document.getElementsByName("lot_num1")(0)
   If Not oHTML_Element Is Nothing Then
   oHTML_Element.Value = Sheet1.Range("E2").Value
   End If
    Application.Wait (Now + TimeValue("0:00:01"))
   
   Set oHTML_Element = ie.document.getElementsByName("btn_add_lot")(0)
      If Not oHTML_Element Is Nothing Then oHTML_Element.Click
      For Each oHTML_Element In ie.document.getElementsByTagName("input")
     If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
   Next
    Application.Wait (Now + TimeValue("0:00:01"))
            
   Set oHTML_Element = ie.document.getElementsByName("lot_num2")(0)
   If Not oHTML_Element Is Nothing Then
   oHTML_Element.Value = Sheet1.Range("F2").Value
   End If
    Application.Wait (Now + TimeValue("0:00:05"))
    '呼叫onchange
    oHTML_Element.fireevent "onchange"   '執行 onchange事件
   
    Set oHTML_Element = ie.document.getElementsByName("btn_start")(0)
    If Not oHTML_Element Is Nothing Then oHTML_Element.Click
    For Each oHTML_Element In ie.document.getElementsByTagName("input")
    If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
   Next
    Application.Wait (Now + TimeValue("0:00:01"))
   
    '將ie關閉
    ie.Quit
'   For Each oHTML_Element In ie.document.getElementsByTagName("input")
'      If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
'   Next
End Sub

圖1

圖1

圖2

圖2


正常按下新增批號後開始檢點會跑出來可是我用的vba執行卻如圖2一樣出不來
到底是哪邊寫錯了ㄋ
附上原始檔
<input type="button" name="btn_add_lot" id="btn_add_lot" value="新增批號">
            </td>

            <td>
            <input type='button' name='btn_start' id='btn_start' value='開始點檢' disabled>





回復

使用道具 舉報

發表於 2018-7-24 22:27:22 | 顯示全部樓層
您好,
    透過您所提供的部分程式碼,我沒有環境可以偵錯,也不知道是什麼問題造成.
回復 支持 反對

使用道具 舉報

發表於 2018-7-25 09:15:06 | 顯示全部樓層
您的新增批號,我把它往後,您再執行看看.
  1. Sub SetValueToComboBox()
  2.    Dim ie  As Object
  3.    'Dim oHTML_Element As IHTMLElement

  4.    Set ie = CreateObject("InternetExplorer.Application")
  5.    ie.Visible = True
  6.    ie.navigate "http://home13.compeq.com.tw/ctshopfloor/WipEquipParams/tts2Entry.asp"
  7.    While ie.Busy Or ie.readyState <> 4: DoEvents: Wend

  8.    Set oHTML_Element = ie.document.getElementsByName("dept_code")(0)
  9.    If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "B20"
  10.    Application.Wait (Now + TimeValue("0:00:01"))
  11.    '呼叫onchange
  12.    oHTML_Element.fireevent "onchange"   '執行 onchange事件
  13.    Application.Wait (Now + TimeValue("0:00:03"))
  14.    Set oHTML_Element = ie.document.getElementsByName("equip_name")(0)
  15.    If Not oHTML_Element Is Nothing Then oHTML_Element.Value = "水平冷卻線"
  16.    Application.Wait (Now + TimeValue("0:00:01"))
  17.    oHTML_Element.fireevent "onchange"   '執行 onchange事件
  18.    Application.Wait (Now + TimeValue("0:00:02"))
  19.    
  20.    Set oHTML_Element = ie.document.getElementsByName("line_name")(0)
  21.    If Not oHTML_Element Is Nothing Then
  22.    oHTML_Element.Value = Sheet1.Range("B2").Value
  23.    End If
  24.        Application.Wait (Now + TimeValue("0:00:01"))
  25.     '呼叫onchange
  26.    oHTML_Element.fireevent "onchange"   '執行 onchange事件
  27.    Application.Wait (Now + TimeValue("0:00:01"))
  28.    Set oHTML_Element = ie.document.getElementsByName("work_num")(0)
  29.    If Not oHTML_Element Is Nothing Then
  30.    oHTML_Element.Value = Sheet1.Range("D2").Value
  31.    End If
  32.    Application.Wait (Now + TimeValue("0:00:01"))
  33.    
  34.    Set oHTML_Element = ie.document.getElementsByName("lot_num1")(0)
  35.    If Not oHTML_Element Is Nothing Then
  36.    oHTML_Element.Value = Sheet1.Range("E2").Value
  37.    End If
  38.     Application.Wait (Now + TimeValue("0:00:01"))
  39.    

  40.             
  41.    Set oHTML_Element = ie.document.getElementsByName("lot_num2")(0)
  42.    If Not oHTML_Element Is Nothing Then
  43.    oHTML_Element.Value = Sheet1.Range("F2").Value
  44.    End If
  45.     Application.Wait (Now + TimeValue("0:00:05"))
  46.     '呼叫onchange
  47.     oHTML_Element.fireevent "onchange"   '執行 onchange事件

  48.    Set oHTML_Element = ie.document.getElementsByName("btn_add_lot")(0)
  49.       If Not oHTML_Element Is Nothing Then oHTML_Element.Click
  50.       For Each oHTML_Element In ie.document.getElementsByTagName("input")
  51.      If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
  52.    Next
  53.     Application.Wait (Now + TimeValue("0:00:05"))
  54.    
  55.     Set oHTML_Element = ie.document.getElementsByName("btn_start")(0)
  56.     If Not oHTML_Element Is Nothing Then oHTML_Element.Click
  57.     For Each oHTML_Element In ie.document.getElementsByTagName("input")
  58.     If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
  59.    Next
  60.     Application.Wait (Now + TimeValue("0:00:01"))
  61.    
  62.     '將ie關閉
  63.     ie.Quit
  64. '   For Each oHTML_Element In ie.document.getElementsByTagName("input")
  65. '      If oHTML_Element.Type = "submit" Then oHTML_Element.Click: Exit For
  66. '   Next
  67. End Sub
複製代碼

回復 支持 反對

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

 ㄚ母滴雞湯
 員林香純滴雞精

Archiver|手機版|小黑屋|彰化一整天的論壇(Excel,Office)  |网站地图

GMT+8, 2018-10-17 18:20 , Processed in 0.131830 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表