WM_COMMAND Stuck

dominica526
dominica526 used Ask the Experts™
on
Hello Experts,

I’m trying to automate applications outside of VB, I’m done with sendkeys and I am having great success with sendmessage. I can put text in Notepad with this code:

Private Sub Send1_Click()
Dim lNotepadHwnd As Long
Dim lNotepadEdit As Long
Dim sCaption As String
   
    lNotepadHwnd = FindWindow("Notepad", vbNullString)
    lNotepadEdit = FindWindowEx(lNotepadHwnd, 0&, "Edit", vbNullString)

    SendMessageSTRING lNotepadEdit, WM_SETTEXT, 256, "Automatic"
    SendMessageSTRING lNotepadHwnd, WM_SETTEXT, 256, "Look Ma no hands"

End Sub

Here’s where I need help. I want to open a file in Notepad and this getmenu is cool, but when I issue the menu command it opens beautifully.

X = Sendmessage(lNotepadHwnd, WM_COMMAND, MenuID, 0&)

This opens the open menu but locks VB until I manually respond, help!!! I don’t want to manually respond I want VB to keep moving on to my next commands. Help here is the code:

Private Sub send4_Click()
Dim lNotepadHwnd As Long
Dim lNotepadEdit As Long
Dim sCaption As String
Dim hMainMenu As Long
Dim hMenu As Long
Dim MenuID As Long
Dim ChildHwnd As Long
Dim ChildEdit As Long
Dim X As Long

   
lNotepadHwnd = FindWindow("Notepad", "Untitled - Notepad")
   
If lNotepadHwnd > 0 Then
 hMainMenu = GetMenu(lNotepadHwnd)
 hMenu = GetSubMenu(hMainMenu, 0)
MenuID = GetMenuItemID(hMenu, 1)
X = Sendmessage(lNotepadHwnd, WM_COMMAND, MenuID, 0&)
End If


ChildHwnd = FindWindow("#32770", "Open")
ChildEdit = FindWindowEx(ChildHwnd, 0&, "Edit", vbNullString)

SendMessageSTRING ChildEdit, WM_SETTEXT, 256, "c:\dominica.txt"


End Sub
 


Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Are you trying to open a file with notepads open menu?Why not just launch the file with ShellExecute API then Find the window since you will know the caption.

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 SendMessageSTRING Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long
Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Const WM_SETTEXT = &HC

Private Sub Command1_Click()
Dim lNotepadHwnd As Long
Dim lNotepadEdit As Long
Dim sCaption As String
Dim sFile As String
sFile = "C:\dominica.txt"
   
    Call ShellExecute(hwnd, "Open", (sFile), "", App.Path, 1)
    Call Sleep(500)
    sFile = Mid$(sFile, InStrRev(sFile, "\") + 1)

    lNotepadHwnd = FindWindow("Notepad", sFile & " - Notepad")
    lNotepadEdit = FindWindowEx(lNotepadHwnd, 0&, "Edit", vbNullString)

    SendMessageSTRING lNotepadEdit, WM_SETTEXT, 256, "Automatic"
    SendMessageSTRING lNotepadHwnd, WM_SETTEXT, 256, "Look Ma no hands"

End Sub


Sorry if this isn't what you are looking for.

Author

Commented:
Hey Vinny,

I’m not trying to open a file I’m just trying to learn how to automate other applications with VB. So far the getmenu thing is excellent and once I getting it working right I will use it many other ways and in many other applications. As I said it works great until it comes to a stop. I need to somehow let it know to move on.

This is where everything comes to a stop, after the menu is opened and ready for me to send
Sendmessage(lNotepadHwnd, WM_COMMAND, MenuID, 0&) more commands it halts VB so that I can’t move on to these commands.

ChildHwnd = FindWindow("#32770", "Open")
ChildEdit = FindWindowEx(ChildHwnd, 0&, "Edit", vbNullString)

SendMessageSTRING ChildEdit, WM_SETTEXT, 256, "c:\dominica.txt"

I’m thinking that there is some way to tell WM_command to let go.


 

Author

Commented:
I found it!!! Postmessage
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Author

Commented:
Hello Experts,

I figured out this problem but now I have another, if this is okay here goes. I have SendMessage working pretty well but I need to press return to continue with my attempt to automate Internet Explorer. As you can see from the code below I made my way to the edit box in IE, does anyone know how to use something like WM_KEYDOWN to press Return for my new entered Web address to go? Thanks

Private Sub NET_Click()

IEHwnd = FindWindow("IEFrame", "My Yahoo! - Microsoft Internet Explorer")
IEWorker = FindWindowEx(IEHwnd, 0&, "WorkerW", vbNullString)
IEReBar = FindWindowEx(IEWorker, 0&, "ReBarWindow32", vbNullString)
IEComboM = FindWindowEx(IEReBar, 0&, "ComboBoxEx32", vbNullString)
IECombo = FindWindowEx(IEComboM, 0&, "ComboBox", vbNullString)
IEEdit = FindWindowEx(IECombo, 0&, "Edit", vbNullString)

ShowWindow IEHwnd, SW_MAXIMIZE

SendMessageSTRING IEEdit, WM_SETTEXT, 256, "https://owa.deltanet.net/exchange/logon.asp"

End Sub

Author

Commented:
Can anyone help me automate the return key? Please! Okay fine, you tell me how many points you need!

Commented:
find the handle of the 'Go' button say 'hwnd'
and
SendMessage(hwnd,BM_CLICK,0,0);

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