How to give file path to "save as" window using vb6 -
i'm working on vb6 code. has perform following operations in sequence: 1. check window open or not (done! using findwindows) 2. press ctrl + s (done! using sendkeys("^s") 3. type full path name (stuck here! don't know how proceed) 4. hit enter key (done! using sendkeys)
private declare function findwindow lib "user32" alias "findwindowa" (byval lpclassname string, _ byval lpwindowname string) long private declare function getwindowtext lib "user32" alias "getwindowtexta" (byval hwnd long, byval lpstring string, _ byval cch long) long private declare function getwindowtextlength lib "user32" alias "getwindowtextlengtha" (byval hwnd long) long private declare function getwindow lib "user32" (byval hwnd long, byval wcmd long) long '-------------------------------------------------------- private declare function postmessage lib "user32" alias "postmessagea" (byval lhwndp long, byval wmsg long, byval wparam long, byval lparam long) long private declare function sendmessage lib "user32" alias "sendmessagea" (byval lhwndp long, byval wmsg long, byval wparam long, lparam any) long private declare function findwindowex lib "user32" alias "findwindowexa" (byval hwnd1 long, byval hwnd2 long, byval lpsz1 string, byval lpsz2 string) long private declare function setforegroundwindow lib "user32" ( _ byval hwnd long) _ long private const bm_click = &hf5 private const wm_close = &h10 private const wm_settext long = &hc '------------------------------------------------------------ private const gw_hwndnext = 2 private sub command1_click() dim lhwndp long dim lhwndp1 long dim hwnd1 long dim hwnd11 long if dir$("c:\users\public\123.txt") <> "" kill ("c:\users\public\123.txt") end if if gethandlefrompartialcaption(lhwndp, "untitled - notepad") = true setforegroundwindow lhwndp doevents call vba.sendkeys("^s") doevents call vba.sendkeys("c:\users\public\123.txt") 'this not working 100% if gethandlefrompartialcaption(lhwndp1, "save as") = true doevents hwnd11 = findwindowex(lhwndp1, 0, "button", "&save") if hwnd11 <> 0 call postmessage(hwnd11, bm_click, 0, 0) else msgbox "button handle not found!" end if end if hwnd11 = findwindowex(lhwndp1, 0, "button", "&save") if hwnd11 <> 0 call postmessage(hwnd1, bm_click, 0, 0) else msgbox "button handle not found!" end if end if end end sub public function gethandlefrompartialcaption(byref lwnd long, byval scaption string) boolean dim lhwndp long dim sstr string gethandlefrompartialcaption = false lhwndp = findwindow(vbnullstring, vbnullstring) 'parent window while lhwndp <> 0 sstr = string$(getwindowtextlength(lhwndp) + 1, chr$(0)) getwindowtext lhwndp, sstr, len(sstr) sstr = left$(sstr, len(sstr) - 1) if instr(1, sstr, scaption) > 0 gethandlefrompartialcaption = true lwnd = lhwndp exit end if lhwndp = getwindow(lhwndp, gw_hwndnext) loop end function
i tried sendmessage function. wm_settext setting junk window title , not in file name field.
any alternate wm_settext ? or other method accompolish task?
note: in example i've used notepad. actual application uses third party window. dont have code application.
the problem aren't waiting sendkeys text processed target application. call doevents not same thing waiting external application something. allows your application flush rest of its event queue.
if need wait external application process, quick , dirty way add short sleep. declare api function as...
public declare sub sleep lib "kernel32" (byval dwmilliseconds long)
...and try this:
'... if gethandlefrompartialcaption(lhwndp, "untitled - notepad") = true setforegroundwindow lhwndp sleep 100 call vba.sendkeys("^s") sleep 100 call vba.sendkeys("c:\users\public\123.txt") 'this not working 100% if gethandlefrompartialcaption(lhwndp1, "save as") = true sleep 100 hwnd11 = findwindowex(lhwndp1, 0, "button", "&save") if hwnd11 <> 0 call postmessage(hwnd11, bm_click, 0, 0) else msgbox "button handle not found!" end if end if hwnd11 = findwindowex(lhwndp1, 0, "button", "&save") if hwnd11 <> 0 call postmessage(hwnd1, bm_click, 0, 0) else msgbox "button handle not found!" end if end if '...
if still doesn't work, adjust sleep times until does.
Comments
Post a Comment