how to send Alt-S in VBA to other window

duncanb7
duncanb7 used Ask the Experts™
on
Dear Expert,

I would like to know how to send key of ATl-S to webpage , I try to us ASCILL combine code of ALT-S as 1F  but
it doesn't work, I also try WM_SYSKEYDOWN for Alt  but it also not response. THe purpise is using for save botton
of Alt-S in download diaglog and (I don't want to use sendigitemmessage hwnd, &H1,BM_CLIC,0 because  the function
 will hang up if the file is existed until you reply overwrite or not )
 
PostMessage hdlg, WM_KEYDOWN, &H1F&, 0   'Alt-S
PostMessage hdlg, WM_KEYUP, &H1F&, 0


Please help to point out my mistake for postmessage VBA code

Duncan
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2006

Commented:
what is it you are trying to do again? save a web page? or click on yes when you manually click a link to save a file
rather than explaining in code what you want to do, explain in a user point of view, what you would do manually
Top Expert 2010
Commented:
<<  (I don't want to use sendigitemmessage hwnd, &H1,BM_CLIC,0 because  the function will hang up if the file is existed until you reply overwrite or not ) >>
Use the custom implementation to avoid this behavior. The example is below... If the overwrite dialog appears you can automate that also under Windows XP the control ID for "Yes" and "No" are as follows.
Yes = 6
No = 7

 '// Usage
Where (hDlg) is the handle to dialog only (The main dialog window handle) you don't need to search deeper since the association of the control is obtained using the control ID. (XP only)
ClickMethod hDlg, 6 '//click yes
ClickMethod hDlg, 7 '//click no

Option Explicit
'egl1044
Private Const SMTO_NORMAL As Long = &H0&
Private Const BM_CLICK As Long = &HF5&

Private Declare Function SendMessageTimeoutW Lib "user32.dll" ( _
  ByVal hWnd As Long, _
  ByVal Msg As Long, _
  ByVal wParam As Long, _
  ByVal lParam As Long, _
  ByVal fuFlags As Long, _
  ByVal uTimeout As Long, _
  ByRef lpdwResult As Long) As Long
  
Private Declare Function GetDlgItem Lib "user32.dll" ( _
  ByVal hDlg As Long, _
  ByVal nIDDlgItem As Long) As Long
  

Public Function SendDlgItemMessageTimeout( _
  ByVal hWnd As Long, _
  ByVal Msg As Long, _
  ByVal wParam As Long, _
  ByVal lParam As Long) As Long
  
  ' Custom implementation of SendDlgItemMessage that doesn't
  ' block the current thread. This is useful for example when
  ' dialog opens a modal dialog.
  
  Dim Success     As Long
  Dim lpResult    As Long
  
  Success = SendMessageTimeoutW( _
      hWnd, _
      Msg, _
      wParam, _
      lParam, _
      SMTO_NORMAL, _
      1000, _
      lpResult)
  
  SendDlgItemMessageTimeout = lpResult
  
End Function


Public Sub ClickMethod( _
  ByVal hDlg As Long, _
  ByVal dwId As Long)
  
  Dim ctlhWnd       As Long
  ctlhWnd = GetDlgItem(hDlg, dwId)
  Debug.Print SendDlgItemMessageTimeout(ctlhWnd, BM_CLICK, 0, 0)
  
End Sub

Open in new window

Author

Commented:
Thanks it works

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial