First of all, that issue has nothing whatsoever to do with Sendkeys, so to say that SendKeys doesn't work is completely wrong. SendKeys does exactly what it is supposed to do, but it's not something you should rely on in most acses anyway.
I think you'll find that your problem is that the Firefox window doesn't run in the actual process that you start. Try running just the Shell line and then look in the Task Manager and see what the process ID of Firefox is.
Actually, given that the alternative to AppActivate is to use pinvoke, I'd go with it. Having said that, it won't restore a minimised window so if that's an issue then API is the only option. I'd not use Shell though. In my opinion Runtime functions should be avoided UNLESS they add value. Avoiding the need for unmanaged code is adding value in my opinion.
You're right. The issue isn't with SendKeys at all. My apologies.
TTn's example start's the application and has the added benefit of leaving focus on the new app. Unfortunately, I’m trying to open a web page with it, and that chocks it at .start, as it can’t find the file specified. I know I didn't include that info with my first post, but I figured one thing at a time.
Also, it is AFTER that where I run into problems.
Being new and frustrated with VB2005, can you at least throw me a bone and tell me what you're talking about with pinvoke and the API? My impression is that .NET is "doing away" with dealing with the Windows API favoring its own flavor (called something else).
I suppose part of the problem is that I'm jumping into the deep end of the pool with 2005 and I don’t know the intricacies of the language enough to be productive, other that raising my frustration level.
"pinvoke" is "Platform Invoke", which basically means using the Windows API. The .NET Framework includes a lot of types and a lot of members but it still can't do everything. There are still certain situations where you need to resort to unmanaged code, which means using COM components via COM Interop or exported methods via Platform Invoke. Remember, you can Google any term you like and almost certainly get a detailed explanation.
Now, may I suggest that you tell us exactly what you're doing and exactly what happens? That way we know exactly what problem we're trying to solve.
TTn's example start's the application and has the added benefit of leaving focus on the new app
For a moment anyway. An important side note with this, is that you cant specify a start, without that application stealing focus for at least a moment.
Sometimes Shelling will still steal focus, even if you specify not to.
Keeping the focus there is another thing all together.
I thought that a WebBrowser component might help you, but...
I want to open a web browser (either IE 6 or 7 and Firefox 1 or 2 depending on the user's preference) to a specific page. I then want to refresh it periodically (say every 5 seconds) and/or interact with controls on the page, including buttons.
That’s it!
I’ve been trying to Google at every step of the way. That’s how I got one of the lame shell/appactivate examples. I also found a lot of unanswered threads asking similar questions. The ones that are answered don't address the problem, or are just plain wrong.
Okay so you're new to 2005.
You should be able to open the internet explorer, with the .Start method I posted. I dont know if Firefox is the same.
VB Code:
Dim startInfo As New ProcessStartInfo("IExplore.exe")
startInfo.Arguments = "www.YourLink.com"
Process.Start(startInfo)
I believe a WebBrowser component is the way for you to go in 2005.
However, if you want to interact with the browser directly by simulating keyboard messages, then maybe you should check out my SendKeysToWindow class below in my signature.
If you are determined to use sendkeys to interact with the browser, which I don't think is the correct way to do it, then you may prefer to use sendmessage instead of send keys as it will be slightly more reliable in that you won't have to set the focus to the window and so can send messages to minimised windows. You may also want to run this in another thread as well if you are running it from a form based app, to allow user interaction, but that is another matter.
Attached is an example of a function you could use. Copy code into a new class (for example named sendClass) and call it,
SendKeys is unreliable because you've just got to hope that the correct control has focus to receive your simulated key strokes. It is much better to use the Windows API to interact with external applications. That way you know for sure that you're interacting with exactly the controls you intend. If you use SendKeys and the wrong control has focus you may do something really bad.
when use sendkey, you have to let the window get focus and bring it as top window, then send keys, then return the top window back to its position. this may make the window flash on the screen. send message is better.
Using an API call would be the ideal way of doing this, I agree.
In fact, in light of all the trouble I've had trying to do it this way; I'd much rather do that.
How?
(BTW, during the course of this thread, I've written 80% of the app I need in VB6. I think I'd be able to use an API call in VB6 as well as .NET, so, by all means, if anyone knows how to send messages to other applications, let me know).
One of the (other) drawbacks of using SendKeys is the necessity of knowing the title (form caption) in the other app. With a web browser, that's nearly impossible because the URL occupies it!