Board logo

標題: [發問] 如何在VBA中使用SendMessage或PostMessage來對"檔案下載"對話盒進行操作 [打印本頁]

作者: white5168    時間: 2012-6-12 21:32     標題: 如何在VBA中使用SendMessage或PostMessage來對"檔案下載"對話盒進行操作

本帖最後由 white5168 於 2012-6-23 20:19 編輯

請問各位大大,要如何在VBA的環境中使用SendMessage或PostMessage來對"檔案下載"對話盒進行操作?
如網址  http://www.twse.com.tw/ch/trading/fund/BFI82U/BFI82U_print.php?begin_date=20120611&end_date=&report_type=day&language=ch&save=csv

以下是我使用過的方法,但依舊無法成功
  1. Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
  2. Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
  3. Private Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
  4. Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
  5. Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
  6. Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

  7. Private Const WM_LBUTTONDOWN = &H201
  8. Private Const WM_LBUTTONUP = &H202
  9. Private Const BM_CLICK = &HF5
  10. Private Const MOUSEEVENTF_LEFTDOWN = &H2
  11. Private Const MOUSEEVENTF_LEFTUP = &H4

  12. Sub test()
  13.     Dim IE As Object
  14.     Dim hpwnd As Long, hcwnd As Long
  15.     Dim iRet As Long
  16.    
  17.     Set IE = CreateObject("InternetExplorer.Application") '設定InternetExplorer物件

  18.     strLink = "http://www.twse.com.tw/ch/trading/fund/BFI82U/BFI82U_print.php?begin_date=20120611&end_date=&report_type=day&language=ch&save=csv" '檔案下載網址
  19.    
  20.    
  21.    With IE
  22.         .Visible = True  '開啟IE
  23.         .Navigate strLink   '連結網址
  24.     End With
  25.    
  26.    
  27.     Do
  28.         hpwnd = FindWindow("#32770", "檔案下載") '抓取對話盒的handle
  29.         DoEvents
  30.     Loop While hpwnd = 0
  31.    
  32.     Do
  33.         hcwnd = FindWindowEx(hpwnd, 0, "Button", "儲存(&S)") '抓取對話盒按鈕的handle
  34.         DoEvents
  35.     Loop While hcwnd = 0

  36.     iRet = SetForegroundWindow(hcwnd) '設定焦點

  37.    '在以下五種方法經皆由windows發送訊息模仿滑鼠按鍵,,依然無法請問各位是否有其他方法可以做到windows發送訊息模仿滑鼠按鍵
  38.     '方法一
  39.     iRet = SendMessage(hcwnd, BM_CLICK, 0, 0)               '透過windows發送訊息模仿滑鼠按鍵

  40.     '方法二
  41.     'iRet = SendMessage(hcwnd, WM_LBUTTONDOWN, 0, 0)        '滑鼠左鍵按下
  42.     'iRet = SendMessage(hcwnd, WM_LBUTTONUP, 0, 0)          '滑鼠左鍵釋放

  43.     '方法三
  44.     'PostMessage hcwnd, WM_LBUTTONDOWN, 0&, CLng(&H90009)   '滑鼠左鍵按下
  45.     'PostMessage hcwnd, WM_LBUTTONUP, 0&, CLng(&H90009)     '滑鼠左鍵釋放
  46.     'PostMessage hcwnd, WM_LBUTTONDOWN, 0&, CLng(&H90009)   '滑鼠左鍵按下
  47.     'PostMessage hcwnd, WM_LBUTTONUP, 0&, CLng(&H90009)     '滑鼠左鍵釋放

  48.     '方法四
  49.     'mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0           '透過滑鼠事件, 滑鼠左鍵按下
  50.     'mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0             '透過滑鼠事件, 滑鼠左鍵釋放
  51.    
  52.     '方法五
  53.     'SendKeys "%s", True   '透過按下Alt + s 儲存檔案

  54.     Set IE = Nothing    '釋放資源
  55. End Sub
複製代碼

作者: white5168    時間: 2012-6-23 18:18

沒有大大想到方法?
作者: Hsieh    時間: 2012-6-23 23:05

回復 2# white5168
http://forum.twbts.com/thread-6807-1-4.html
作者: white5168    時間: 2012-6-23 23:18

我有需要一定要直接去操作對話框,我當然知道有其他的方法可以抓取,可是就一定需要直接操作對話框




歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)