none
請問要在VBA的環境中如何使用SendMessage或PostMessage來對"檔案下載"對話盒進行操作 RRS feed

  • 問題

  • 請問要在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


    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    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
    Private Declare Function PostMessage& Lib "user32" Alias "PostMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any)
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    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
    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)
    
    Private Const WM_LBUTTONDOWN = &H201
    Private Const WM_LBUTTONUP = &H202
    Private Const BM_CLICK = &HF5
    Private Const MOUSEEVENTF_LEFTDOWN = &H2
    Private Const MOUSEEVENTF_LEFTUP = &H4
    
    Sub test()
        Dim IE As Object
        Dim hpwnd As Long, hcwnd As Long
        Dim iRet As Long
        
        Set IE = CreateObject("InternetExplorer.Application") '設定InternetExplorer物件
     
        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" '檔案下載網址
            
        With IE
            .Visible = True  '開啟IE
            .Navigate strLink   '連結網址
        End With
        
        Do
            hpwnd = FindWindow("#32770", "檔案下載") '抓取對話盒的handle
            DoEvents
        Loop While hpwnd = 0
        
        Do
            hcwnd = FindWindowEx(hpwnd, 0, "Button", "儲存(&S)") '抓取對話盒按鈕的handle
            DoEvents
        Loop While hcwnd = 0
        
    
        iRet = SetForegroundWindow(hcwnd) '設定焦點
    
    
        '在以下五種方法經皆由windows發送訊息模仿滑鼠按鍵,,依然無法請問各位是否有其他方法可以做到windows發送訊息模仿滑鼠按鍵
        '方法一
        iRet = SendMessage(hcwnd, BM_CLICK, 0, 0)               '透過windows發送訊息模仿滑鼠按鍵
    
        '方法二
        'iRet = SendMessage(hcwnd, WM_LBUTTONDOWN, 0, 0)        '滑鼠左鍵按下
        'iRet = SendMessage(hcwnd, WM_LBUTTONUP, 0, 0)          '滑鼠左鍵釋放
    
        '方法三
        'PostMessage hcwnd, WM_LBUTTONDOWN, 0&, CLng(&H90009)   '滑鼠左鍵按下
        'PostMessage hcwnd, WM_LBUTTONUP, 0&, CLng(&H90009)     '滑鼠左鍵釋放
        'PostMessage hcwnd, WM_LBUTTONDOWN, 0&, CLng(&H90009)   '滑鼠左鍵按下
        'PostMessage hcwnd, WM_LBUTTONUP, 0&, CLng(&H90009)     '滑鼠左鍵釋放
    
        '方法四
        'mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0           '透過滑鼠事件, 滑鼠左鍵按下
        'mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0             '透過滑鼠事件, 滑鼠左鍵釋放
        
        '方法五
        'SendKeys "%s", True   '透過按下Alt + s 儲存檔案
        
        Set IE = Nothing    '釋放資源
    End Sub
    

    • 已編輯 Amin white 2012年6月13日 上午 04:27
    2012年6月12日 下午 01:44

解答

所有回覆