Can someone help with VB 2010? Trying to open & manipulate external program.. I'm stuck.. lol

9 replies
I'm trying to do something very simple, I just forget how its done (I used to program when I was a teen in VB3, a lifetime ago)

I am currently using VB 2010

So, for example, I want to have a button that when it is clicked, it will open Notepad, and then it will click the "File" menu, and then click on "New"

How would I do this?

Here is what I have so far:

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Process.Start("C:windowssystem32notepad.exe")

    End Sub
I have the open notepad part right - It will open Notepad when I click the button.

I just need to figure out how to make it select one of the menu items & then one of the choices from that menu.

Like the "File > New" example I posted above.

Can someone help please?
#2010 #external #lol #manipulate #menu #menus #open #program #stuck #vb 2010 #vb2010
  • Profile picture of the author Brandon Tanner
    To open Notepad and send the keys Ctrl+N (new file), you can do this...

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim ProcessID As Integer = Shell("NOTEPAD.EXE", AppWinStyle.NormalFocus)
            AppActivate(ProcessID)
            My.Computer.Keyboard.SendKeys("^{n}", True)
    
        End Sub
    On the 'send keys' line, the ^ represents Ctrl, and the key inside the curly brackets is the key that gets sent with Ctrl (note that this key should be lower-cased).

    So for example if you wanted to do Ctrl+O (open file), you would do this...

    Code:
    My.Computer.Keyboard.SendKeys("^{o}", True)
    And if you want to send regular text, you would do this...

    Code:
    My.Computer.Keyboard.SendKeys("blah blah blah", True)
    Hope that helps!

    And happy New Year
    Signature

    {{ DiscussionBoard.errors[7543452].message }}
  • Profile picture of the author Robert Michael
    Ahhh!!

    Thank you man, this is WAY different from what it used to be back in VB3.. I never would have figured this out, LOL!

    I appreciate the help, & happy new year!!

    Hopefully I can figure everything out from this point on, but if not I'll post back.. Man this is gonna be awesome if I can get this working!
    {{ DiscussionBoard.errors[7544916].message }}
  • Profile picture of the author Robert Michael
    Quick question - If ^ represents CTRL, what represents ALT ?

    If it helps, the program isn't really for Notepad - that was just an example.

    The program I'm really doing this for doesn't have those CTRL + shortcuts, so I need to find a way to click ALT to highlight the menu, then navigate to the proper menu item and select it that way (if that makes sense)
    {{ DiscussionBoard.errors[7545476].message }}
    • Profile picture of the author Michael Westgate
      % (percent sign).

      Your idea makes sense and in theory I see no reason why you can't achieve menu selection using the sendkeys commands. If it doesn't work, reply back and I'll check out if there are any other methods.

      Reference msdn

      To combine a key with SHIFT, precede the key code with + (plus sign). To combine a key with CTRL, precede the key code with ^ (caret). To combine a key with ALT, precede the key code with % (percent sign). To specify repeating keys, use the form {key number}. You must put a space between key and number. For example, {LEFT 42} means "press the LEFT ARROW key 42 times"; {h 10} means "press 'h' 10 times."
      {{ DiscussionBoard.errors[7547654].message }}
      • Profile picture of the author Brandon Tanner
        If the program is not part of the Windows OS, then you can do something like this...

        Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        
                Dim CurrentProcess As Process = Process.Start("C:PathToExeFile.exe")
                CurrentProcess.WaitForInputIdle(3000)
                AppActivate(CurrentProcess.Id)
                My.Computer.Keyboard.SendKeys("blah blah blah", True)
        
        End Sub
        Depending on how long it takes VB to open the exe file, you may have to bump up the 3000 to a higher figure (3000 = 3 seconds to wait for the exe file to open before it sends keys to it).

        As Michael mentioned, the % character represents ALT. But if the menu item you want to select can not be controlled via shortcut keys, then that's going to be tricky.

        If that's the case, then one option would be to have VB open the exe, and then simulate mouse clicks at specific coordinates on the screen. But in order for that to work, the program's GUI would have to open in the exact same position every time (for example, it would have to open 'maximized' every time). And if you need it to work at different screen resolutions (ie if you plan to distribute the VB program to others), then you would have to account for that as well.

        That said... here's how to move the mouse and simulate clicks...

        VB Helper: HowTo: Simulate a mouse movement and click the mouse in Visual Basic 2005
        Signature

        {{ DiscussionBoard.errors[7549020].message }}
  • Profile picture of the author Robert Michael
    No, its not part of the OS processes. Its a 3rd party application I'm trying to control.

    I tried your latest code, but this is the error it keeps giving me once it reaches the AppActivate(CurrentProcess.Id)

    {"Process '{0}' was not found."}

    Heres a screenshot if it helps:

    {{ DiscussionBoard.errors[7549121].message }}
    • Profile picture of the author Brandon Tanner
      I just double checked it and that's the correct code.

      1) Are you sure that the full path to the exe is correct? (and I believe the entire path is case-sensitive).

      2) You may have to bump up the wait time (try 10000 for now just to make sure it works, then you can lower it as needed).

      Also, that code as is won't work if the exe file is already open when VB tries to open it. So you would need to either make sure that the exe is closed before you run the VB code, or create conditional statements to check whether or not the exe is already open before it processes the rest of the code.
      Signature

      {{ DiscussionBoard.errors[7549195].message }}
  • Profile picture of the author Robert Michael
    Yeah, the path is correct, its opening the program like it should..

    I just tried it with the program closed so it could open itself & I set the idle to 20000 but it still gives the same error as the screenshot I posted before

    This is so confusing & frustrating.. lol
    {{ DiscussionBoard.errors[7549420].message }}
    • Profile picture of the author Brandon Tanner
      I don't know what else to tell you, as that code works perfectly for me. Maybe try to open different exe files and see if that works? If no other exe files open, then is it possible that a firewall or antivirus program (or your UAC setting) is blocking VB from opening external programs?
      Signature

      {{ DiscussionBoard.errors[7549560].message }}

Trending Topics