Results 1 to 3 of 3

Thread: Sendkeys.sendwait doenst work. Why? and how to close an app opened thru shellexecute

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    12

    Unhappy Sendkeys.sendwait doenst work. Why? and how to close an app opened thru shellexecute

    Hi Gurus,
    I am trying to automate an application (yeas, i am a novice!). Baiscally i need to run a software called traned from excel. The software doesnt have a com interface.

    this thread is a follow up of my previous thread;
    http://www.vbforums.com/showthread.p...09#post3624109


    I have just 3 simple questions for you experts;

    1) I am trying to use sendkeys.wait. but When i run the macros it comes up with 'compile error: Argument not optional'. If i use sendkeys only it works but its too fast! i need sendkeys to send keystrokes only when previous key strokes have finished!
    2) How to terminate an application opened through shellexecute??

    3) A better way to wait for the run to finish. I am using application.wait at the moment. but a better option is need due to variability in duration of runs.

    I have search many forums but can't seem to find the right answer.
    Please help!!

    here is my code (for running the application once);
    please note im using windows XP and office 07. Thanks
    HTML Code:
    Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" ( _
    ByVal lpClassName As String, _
    ByVal lpWindowName As String) As Long
    
    Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, _
    ByVal nCmdShow As Long) As Long
    Private Const SW_MAXIMIZE = 3
    Private Declare Function SetForegroundWindow Lib "user32.dll" (ByVal hwnd As Long) 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 Sub Open_TranedFile()
     
    'ShellExecute Application.hwnd, "open", "C:\Documents and Settings\naziz.EGR\Desktop\a.tnd", vbNullString, vbNullString, 1
    'Application.Wait Now + TimeValue("00:00:06")
     
    Dim hwndMs As Long
    hwndMs = FindWindow("Afx:400000:8:10011:0:2ab0c27", vbNullString)
    If hwndMs <= 0 Then MsgBox hwndMs
    'MsgBox hwndMs
    'Wait for application to load
     
    'SetForegroundWindow (hwndMs)
    Call ShowWindow(hwndMs, SW_MAXIMIZE)
     
    
    
    SendKeys.SendWait "%"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{right}"
    SendKeys.SendWait "{down}"
    SendKeys.SendWait "~"
    
    End Sub

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Sendkeys.sendwait doenst work. Why? and how to close an app opened thru shellexec

    Use the Sleep Api to introducing wait

    vb Code:
    1. Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
    2.  
    3. Sub Sample()
    4.     '~~> Rest of your code
    5.     SendKeys "{right}"
    6.     '~~> Sleep for 5 Seconds
    7.     Sleep 5000
    8.     SendKeys "{right}"
    9. '~~> Rest of your code
    10. End Sub

    For terminating...

    vb Code:
    1. Public Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
    2. ByVal uExitCode As Long) As Long
    3.  
    4. Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, _
    5. ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
    6.  
    7. '~~> Example Usage:
    8. Retval = ShellExecute(Application.Hwnd, "Open", Sfile, vbNullString, _
    9. vbNullString, vbMinimized)
    10.  
    11. hProcess = OpenProcess(PROCESS_ALL_ACCESS, True, Retval)
    12.  
    13. TermSucc = TerminateProcess(hProcess, 0)
    Last edited by Siddharth Rout; Oct 6th, 2009 at 01:17 PM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2009
    Posts
    12

    Thumbs up Re: Sendkeys.sendwait doenst work. Why? and how to close an app opened thru shellexec

    thank you so much for your response. I'll give it a go.

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width