AppActivate (1 Viewer)

Freshman

Registered User.
Local time
Today, 20:43
Joined
May 21, 2010
Messages
437
Hi all,

I have a problem using AppActivate.

I'm using it along with a Shell command to open a Scanner application for my HP printer/scanner.
Then I pass a <Enter> key to it using the SendKey command.

This then brings up the "Save As" dialogue box which I'm very happy with.

At this stage I need to use SendKeys again to "paste" the filename into the Save As field, but the focus is now no longer on the dialogue box. Focus was returned to my original Form from where the code ran initially.

How do I get the focus back on the dialogue box to pass it the file name?

Thanks
Pierre

ReturnValue = Shell(DLookup("ScannerPath", "Settings"), vbNormalFocus)
AppActivate ReturnValue
SendKeys "~", True

PS: For anyone using an HP Printer, the "ScannerPath" is:
C:\Program Files\HP\HP LaserJet M1319 MFP Series\Scan To\hppscan0.exe
 

NigelShaw

Registered User.
Local time
Today, 18:43
Joined
Jan 11, 2008
Messages
1,573
Why don't you use a 'Save As' dialog from within Access and save all the trouble?

Cheers
 

Freshman

Registered User.
Local time
Today, 20:43
Joined
May 21, 2010
Messages
437
Hi Nigel,

Thanks, but it all has to do with integration with the scanning software and to automate the process. Not about just saving a file.
I want to call up the scanning software, move focus to it, paste the file name into the scanning software's "save as" box and initiate the scan all without any user intervention.
Turns out to be more difficult that I thought.

I think I narrowed it down to the fact that I'm batteling to address the "Save As" dialogue box from AppActivate.
In the Taskbar as well as the TaskList it is called "Save As", but I can't find a process like that. The only process related to it, is that of the actual scanning software (hppscan0.exe) but even if I play around with that name, it does not activate or find it.
So if I can just find out by what name to call it, I should be able to activate it.
Did a Tasklist, but none of the processes seems to be the right one.
 
Last edited:

Banana

split with a cherry atop.
Local time
Today, 11:43
Joined
Sep 1, 2005
Messages
6,318
I've always thought that trying automate via SendKeys & setting focus using AppActivate were kind of dicey because you're relying on certain UI behavior and that there's no unexpected errors (e.g. a messagebox displaying when there should be a Save dialog).

My first question is to find out whether HP scanner software offers any of the following:

1) an ActiveX DLL for automation
2) command parameters against the hppscan0.exe that you can use to pass in arguments
3) Public APIs that you can then invoke

If any one of those is available, then I'd suggest you use it instead of wrangling with SendKeys & AppActivate and crossing your fingers.

If worse comes to worse, the alternative is to not use AppActivate that's built in to Access but use WScript.AppActivate, which provides a boolean return value to indicate success in obtaining focus. A sample code (untested):
Code:
Dim objShell As Object 'WScript.Shell
Dim bolResult As Boolean

Set objShell = CreateObject("WScript.Shell")
Do
  'Warning! Potentially infinite loop!
  bolResult = objShell.AppActivate("Save As...")
Loop Until (bolResult = True)

HTH.
 

JHB

Have been here a while
Local time
Today, 19:43
Joined
Jun 17, 2012
Messages
7,732
In Excel you can do it this way.

ReturnValue = Shell("excel.exe", 1) ' Kør Microsoft Word.
AppActivate ReturnValue
SendKeys "%FG", True ' Open the file save box - G=Danish for save
MsgBox ("Do some thing")
AppActivate ReturnValue
SendKeys "YouFilename~", True

I haven't your scanner software installed, so I can't test it.
 

Freshman

Registered User.
Local time
Today, 20:43
Joined
May 21, 2010
Messages
437
Thanks guys, I'm gonna try that - especially Banana's suggestions.

Cheers
Pierre
 

NigelShaw

Registered User.
Local time
Today, 18:43
Joined
Jan 11, 2008
Messages
1,573
Hi,

cant you get the Hwnd of the SaveAs box and set focus to that?



cheers
 

NigelShaw

Registered User.
Local time
Today, 18:43
Joined
Jan 11, 2008
Messages
1,573
Hi,

a quick G search brought this up-

Code:
Declare Function GetActiveWindow Lib "user32" Alias "GetActiveWindow" () As Long

This should return the Hwnd of the most recent ( or active ) window which in your case would be the popup.

Cheers
 

Freshman

Registered User.
Local time
Today, 20:43
Joined
May 21, 2010
Messages
437
Let me try that thanks as I could for the live of me not get the correct handle name
Very strange!!
 

Freshman

Registered User.
Local time
Today, 20:43
Joined
May 21, 2010
Messages
437
Thanks I tried that but even with the number it gives me, I cannot activate it. Gives me an error not found.
What is really getting to me is the inconsistancy of the thing, somethings it takes the focus and sometimes not.
Also I noticed that sometimes, even with the "Save As" box being the ONLY window visible on the screen (the rest all minimized) it does not have the focus (so I guess the Desktop has the focus then).
 

Banana

split with a cherry atop.
Local time
Today, 11:43
Joined
Sep 1, 2005
Messages
6,318
This is basically why I shun SendKeys/AppActivate; you're at mercy at everything else and have to contend with timing your requests and ensure that there's no interrupts that may disrupt your requests.

I'd really look at whether there's APIs or command parameters that you can use instead of trying to manipulate the user interface which is ultimately fragile.
 

Freshman

Registered User.
Local time
Today, 20:43
Joined
May 21, 2010
Messages
437
Thanks Banana, and I do agree with your views about SendKeys.
I'm busy taking it up with Brother in the UK (as their office in South Africa is useless) regarding the automation.
I will now also contact HP to see they they can help.
We want to roll out the hardware to over 200 clients so there is sales in them if they can help me with that.

Cheers guys
Pierre
 

Users who are viewing this thread

Top Bottom