標題:
網頁查詢問題
[打印本頁]
作者:
sillykin
時間:
2016-2-21 11:03
標題:
網頁查詢問題
http://www.land.moi.gov.tw/ngis/chhtml/query2.asp
作者:
GBKEE
時間:
2016-2-23 15:35
回復
1#
sillykin
試試看
Option Explicit
Sub Ex_土地段名代碼查詢系統()
Dim i As Integer, E As Object, xTile, Ar, iR As Integer
With CreateObject("InternetExplorer.Application")
' .Visible = True
.Navigate "http://www.land.moi.gov.tw/ngis/chhtml/query2.asp"
Do While .Busy Or .readyState <> 4: DoEvents: Loop
For i = 0 To .document.all.tags("SELECT").Length - 1
Set E = .document.all.tags("SELECT")(i)
ReDim Ar(1 To E.Length - 1)
For iR = 1 To E.Length - 1
Ar(iR) = iR & Space(1) & E(iR).INNERTEXT
Next
Do
xTile = InputBox(Join(Ar, vbLf), E(0).INNERTEXT & " 1-" & E.Length - 1, 1)
If Val(xTile) = 0 Then MsgBox "離開程式": GoTo OU
Loop Until Val(xTile) >= 1 And Val(xTile) <= E.Length - 1
E.selectedIndex = Val(xTile)
E.fireEvent ("onchange")
Do While .Busy Or .readyState <> 4: DoEvents: Loop
Next
Do
Set E = .document.all.tags("INPUT")
Loop Until Not E Is Nothing
For Each Ar In E
If Ar.Value = "查詢" And Ar.Type = "button" Then
Ar.Click
Exit For
End If
Next
Do While .Busy Or .readyState <> 4: DoEvents: Loop
Do
Set E = .document.all.tags("STRONG")(0)
Loop Until Not E Is Nothing
MsgBox E.INNERTEXT
OU:
.Quit '關閉網頁
End With
End Sub
複製代碼
作者:
sillykin
時間:
2016-2-23 20:19
回復
2#
GBKEE
謝謝g大的協助...試用沒問題...
但上述原理看不太懂...方便解釋一下嗎??
另一提要如何將代碼結果值放到[B11]中呢??
作者:
GBKEE
時間:
2016-2-23 20:39
回復
3#
sillykin
http://forum.twbts.com/tag.php?name=網頁元素
'MsgBox E.INNERTEXT
[B11]=E.INNERTEXT
複製代碼
作者:
sillykin
時間:
2016-2-23 23:21
謝謝大大耐心的回覆,小弟有很多不懂地方,會更加努力學習,感謝
作者:
sillykin
時間:
2016-2-24 11:27
回復
2#
GBKEE
G大能在請問
所輸入每筆的代號,名稱各出現在B11,c11,d11,e11,f11
方便核對輸入值是否正確
作者:
sillykin
時間:
2016-2-25 17:09
回復
6#
sillykin
[/
回復 2# GBKEE
G大能在請問
所輸入每筆的代號,名稱各出現在B11,c11,d11,e11,f11
方便核對輸入值是否正確
能否給個提示
作者:
GBKEE
時間:
2016-2-25 21:35
本帖最後由 GBKEE 於 2016-2-26 06:13 編輯
回復
7#
sillykin
試試看
UserForm的程式碼
需制定如下的6個控制項
ComboBox0, ComboBox1, ComboBox2, ComboBox3, ComboBox4, Label1
Option Explicit
Dim IE As Object, Combo_Ar(), xMsg As Boolean, xButton As Boolean, xRng As Range
Private Sub UserForm_Initialize()
'ComboBox 控制項 -> 縣市'事務所名稱 '鄉鎮市區'段'小段
Combo_Ar = Array(ComboBox0, ComboBox1, ComboBox2, ComboBox3, ComboBox4)
Set IE = CreateObject("InternetExplorer.Application")
Set xRng = [B11]
Label1 = ""
xRng.Resize(, 6) = ""
With IE
'.Visible = True '可不顯示IE
.Navigate "http://www.land.moi.gov.tw/ngis/chhtml/query2.asp"
Do While .Busy Or .readyState <> 4: DoEvents: Loop
ComboBox_list 0
End With
End Sub
Private Sub ComboBox_list(ByVal Op As Integer)
'縣市'事務所名稱 '鄉鎮市區'段'小段 的選項內容導入ComboBox的list
Dim xSelect As Object, i As Integer, x As Integer
xMsg = True
'xButton = True
For i = Op To UBound(Combo_Ar)
With Combo_Ar(i)
If i = Op And .ListCount = 0 Then
Do
Set xSelect = IE.Document.all.tags("SELECT")(i)
Loop Until xSelect.Length > 0
' MsgBox .List(1)
For x = 0 To xSelect.Length - 1
.AddItem
.List(.ListCount - 1, 0) = xSelect(x).innertext
Next
.ListIndex = 0
ElseIf i = Op And .ListIndex > 0 Then
Do
Set xSelect = IE.Document.all.tags("SELECT")(i)
Loop Until xSelect.Length > 0
xSelect.selectedIndex = .ListIndex
If Op <> UBound(Combo_Ar) Then
xSelect.fireEvent ("onchange")
Do While IE.Busy Or IE.readyState <> 4: DoEvents: Loop
Do
Set xSelect = IE.Document.all.tags("SELECT")(i + 1)
Loop Until xSelect.Length > 0
With Combo_Ar(i + 1)
.Clear
For x = 0 To xSelect.Length - 1
.AddItem
.List(.ListCount - 1, 0) = xSelect(x).innertext
Next
.ListIndex = 0
End With
End If
ElseIf i > 0 Then
If Combo_Ar(i - 1).ListIndex = 0 Or Combo_Ar(i - 1).ListCount = 0 Then
.Clear
End If
End If
If .ListCount = 0 Or .ListIndex = 0 Then xButton = False
If UBound(Combo_Ar) = i And .ListCount > 0 Then
If .ListCount = 2 And .List(1) = "" Then xButton = True
If .ListCount = 2 And .List(1) <> "" And .ListIndex > 0 Then xButton = True
If .ListCount > 2 And .ListIndex > 0 Then xButton = True
End If
End With
Next
If xButton Then
button_Click
Else
xRng.Cells(1, 6) = ""
Label1 = ""
End If
xMsg = False
End Sub
Private Sub ComboBox0_Change() '縣市
If xMsg = False Then ComboBox_list 0
xRng.Cells(1, 1) = ComboBox0.Value
End Sub
Private Sub ComboBox1_Change() '事務所名稱
If xMsg = False Then ComboBox_list 1
xRng.Cells(1, 2) = ComboBox1.Value
End Sub
Private Sub ComboBox2_Change() '鄉鎮市區
If xMsg = False Then ComboBox_list 2
xRng.Cells(1, 3) = ComboBox2.Value
End Sub
Private Sub ComboBox3_Change() '段
If xMsg = False Then ComboBox_list 3
xRng.Cells(1, 4) = ComboBox3.Value
End Sub
Private Sub ComboBox4_Change() '小段
If xMsg = False Then ComboBox_list 4
xRng.Cells(1, 5) = IIf(InStr(ComboBox4, "請選擇"), "", ComboBox4.Value)
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
IE.Quit
End Sub
Private Sub button_Click() '查詢
Dim xIObject As Object, E
With IE
Do
Set xIObject = .Document.all.tags("INPUT")
Loop Until Not xIObject Is Nothing
For Each E In xIObject
If E.Value = "查詢" And E.Type = "button" Then
E.Click
Exit For
End If
Next
Do While .Busy Or .readyState <> 4: DoEvents: Loop
Do
Set xIObject = .Document.all.tags("STRONG")(0)
Loop Until Not xIObject Is Nothing
End With
E = xIObject.innertext
Label1 = Split(xIObject.innertext, ":")(1)
xRng.Cells(1, 6) = "'" & Split(xIObject.innertext, ":")(1)
End Sub
複製代碼
作者:
sillykin
時間:
2016-2-25 22:21
回復
8#
GBKEE
謝謝G大的協助...感謝
但試了一下..發生型態不符情形
作者:
sillykin
時間:
2016-2-25 22:38
回復
9#
sillykin
因發生型態不符13,小弟加入下面一行
On Error Resume Next
但發生
物件值無法出現,又出現
此段有問題
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
IE.Quit
End Sub
作者:
GBKEE
時間:
2016-2-26 06:19
回復
10#
sillykin
抱歉程式碼沒貼齊全,原程式碼以更新
UserForm模組頂端的變數宣告(這模組的SUB(程序)皆可使用)
Option Explicit
Dim IE As Object, Combo_Ar(), xMsg As Boolean, xButton As Boolean, xRng As Range
複製代碼
作者:
c_c_lai
時間:
2016-2-26 07:46
回復
9#
sillykin
GBKEE 版大的:
Combo_Ar = Array(ComboBox0, ComboBox1, ComboBox2, ComboBox3, ComboBox4)
複製代碼
是從 0 起宣告。
亦即你的 ComboBox 物件名稱, 應為 ComboBox0、ComboBox1、ComboBox2、ComboBox3、 ComboBox4;
而非是 ComboBox1、ComboBox2、ComboBox3、 ComboBox4、 ComboBox5。
作者:
sillykin
時間:
2016-2-26 19:57
回復
11#
GBKEE
謝謝g大 c_c_lai大 二位的說明,感謝
但還是有問題,
作者:
sillykin
時間:
2016-2-26 21:45
回復
11#
GBKEE
謝謝g大的幫忙
作者:
sillykin
時間:
2016-2-26 23:43
回復
2#
GBKEE
出現此錯誤
作者:
GBKEE
時間:
2016-2-27 06:51
回復
15#
sillykin
IE8,2003 執行你的附檔,沒有錯誤
請在表單模組上按F8逐步執行程式碼,看錯誤點在哪裡.
作者:
c_c_lai
時間:
2016-2-27 09:55
本帖最後由 c_c_lai 於 2016-2-27 09:57 編輯
回復
15#
sillykin
這是你附上程式執行的結果畫面,
我的執行環境:
Windows 10 x64 專業版 (CPU: 16.0G);
Excel 2010 Pro
請再從 Sheet1 的 Private Sub CommandButton1_Click()
按 F8 Tracing 看看。
[attach]23342[/attach]
作者:
sillykin
時間:
2016-2-27 19:33
謝謝g大 c_c_lai大 二位的說明,感謝
是小弟的電腦出現問題,製造二位麻煩,在此說聲『抱歉』,也感謝二位大大協助
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)