返回列表 上一主題 發帖

[發問] 如何用VBA,透過Excel在網頁上控制勾選?

回復 1# espionage
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim E As Object
  4.     With CreateObject("InternetExplorer.Application")
  5.         .Visible = True
  6.         .Navigate "D:\A.HTM"  '修正為你的網頁
  7.         Do While .Busy Or .readyState <> 4: DoEvents: Loop
  8.         '<INPUT type="checkbox" name="displayAttribute" id="displayAttribute1" value="SequenceNo">
  9.         '<INPUT type="checkbox" name="displayAttribute" id="displayAttribute5" value="QuantityStar" >
  10.         With .Document            '
  11.            'TagName= "INPUT", 不是唯一的
  12.            'For Each E In .all.tags("input")  '=>.getElementsByTagName("INPUT")
  13.            '     If E.Value = "SequenceNo" Then E.Checked = [A2] = "Y"
  14.            '     If E.Value = "QuantityStar" Then E.Checked = [B2] = "Y"
  15.            'Next
  16.            'name="displayAttribute"不是唯一的
  17.            'For Each E In .all("displayAttribute")  '=>.getElementsByName("displayAttribute")
  18.            '    If E.Value = "SequenceNo" Then E.Checked = [A2] = "Y"
  19.            '    If E.Value = "QuantityStar" Then E.Checked = [B2] = "Y"
  20.            'Next
  21.             'id="displayAttribute1",id="displayAttribute5"
  22.             'id 是唯一的
  23.             
  24.             '勾選的框 "項次"
  25.             .getElementByID("displayAttribute1").Checked = [A2] = "Y"
  26.             '勾選的框 "用量(*)"
  27.             .getElementByID("displayAttribute5").Checked = [B2] = "Y"
  28.         End With
  29.         '.Quit        '關閉網頁
  30.     End With
  31. End Sub
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

本帖最後由 GBKEE 於 2015-9-22 05:45 編輯

回復 4# espionage

   
我好像讓您誤會了,我的想法是
好像沒有誤會.

我有說到
  1.   'id="displayAttribute1",id="displayAttribute5"
  2.             'id 是唯一的沒有子物件
  3. 所以   For Each A In .getelementbyid("displayAttribute1") 程式傳回錯誤
複製代碼
要改為
  1. Set A = .getelementbyid("displayAttribute1")
  2.         If Sheets(1).Cells(3, 3) = "Y" Then A.Checked = True
複製代碼
簡化
  1. .getelementbyid("displayAttribute1").Checked = [A2] = "Y"
複製代碼
感恩的心......(在麻辣家族討論區.用心學習會有進步的)
但資源無限,後援有限,  一天1元的贊助,人人有能力.

TOP

        靜思自在 : 成功是優點的發揮,失敗是缺點的累積。
返回列表 上一主題