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

by 9 replies
12
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?
#programming #2010 #external #lol #manipulate #menu #menus #open #program #stuck #vb 2010 #vb2010
  • 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
    • [ 1 ] Thanks
  • 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!
  • 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)
    • [1] reply
    • % (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

      • [ 1 ] Thanks
      • [1] reply
  • 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:

    • [1] reply
    • 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.
  • 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
    • [1] reply
    • 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?

Next Topics on Trending Feed