- 帖子
- 23
- 主題
- 0
- 精華
- 0
- 積分
- 73
- 點名
- 63
- 作業系統
- XP
- 軟體版本
- Office 2003
- 閱讀權限
- 20
- 註冊時間
- 2012-4-21
- 最後登錄
- 2025-3-13
           
|
回復 14# joey0415
另一種方式,試試看...- Option Explicit
- #If Win64 Then
- Private Declare PtrSafe Function GetWindow Lib "USER32" (ByVal hWnd As LongPtr, ByVal wCmd As LongPtr) As LongPtr
- Private Declare PtrSafe Function SetForegroundWindow Lib "USER32" (ByVal hWnd As Long) As Long
- Private Declare PtrSafe Function FindWindow Lib "USER32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
- Private Declare PtrSafe Function FindWindowEx Lib "USER32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
- Private Declare PtrSafe Function SendMessage Lib "USER32" Alias "SendMessageA" (ByVal hWnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
- #Else
- Private Declare Function GetWindow Lib "user32" (ByVal HWND As Long, ByVal wCmd As Long) As Long
- Private Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long
- Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
- Private Declare Function FindWindowEx Lib "user32.dll" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
- Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
- #End If
- Private Const WM_CLOSE = &H10
- Private Const GW_HWNDNEXT = 2
- Private Const GW_CHILD = 5
- Private Const BM_CLICK = &HF5
- Sub 點擊()
- Cells.Clear
- Dim hWndChild, hWndPopup, x, Retries As Integer: Retries = 10
-
- With CreateObject("InternetExplorer.Application")
- .Visible = True
- .Navigate "http://www.tdcc.com.tw/smWeb/QryStock.jsp"
-
- Do While .Busy Or .ReadyState <> 4 '等候網頁下載完畢
- DoEvents
- Loop
- Set x = .document.all.tags("option") '查下拉選單
- x(7).Selected = True '選取該子項目
-
- .document.getElementsByName("StockNo")(0).Value = "6456"
- .document.getElementsByName("sub")(0).Click
-
- Do While (.Busy Or .ReadyState <> 4) And 0 < Retries
-
- hWndChild = GetWindow(.hWnd, GW_CHILD)
-
- If 0 <> hWndChild Then
- Do While hWndChild
- hWndPopup = FindWindow("#32770", "網頁訊息") '#32770 The class for a dialog box
- If 0 <> hWndPopup Then
- SendMessage FindWindowEx(hWndPopup, 0, "Button", "確定"), BM_CLICK, 0, 0
- Else
- Exit Do
- End If
- 'SetForegroundWindow .hWnd 'HWND is the HWND of the myIEWindow
- 'Application.SendKeys "~", True
- hWndChild = GetWindow(hWndChild, GW_HWNDNEXT)
- Loop
-
- End If
- Application.Wait Now + #12:00:01 AM#
- DoEvents
- Retries = Retries - 1
- Loop
- If 0 = Retries Then
- AppActivate Application.Caption
- MsgBox "無法關閉訊息視窗!", vbCritical
- Exit Sub
- End If
- '-------------------以下為失焦處,無法點擊
- Stop
- .document.getElementsByName("StockNo")(0).Value = "1101"
- .document.getElementsByName("sub")(0).Click
-
- Do While .Busy Or .ReadyState <> 4 '等候網頁下載完畢
- DoEvents
- Loop
-
- Stop
- .Quit
- End With
- End Sub
複製代碼 |
|