Control a external program (1 Viewer)

Shas91

Member
Local time
Yesterday, 21:57
Joined
Nov 26, 2019
Messages
41
Hi !

This is a thing that I have tried to solve many times but with no success. I have done a program that explane the issue.

I have a embedded PDF file in Access and a whant to set focus on "Foxit" or Adobe or what ever program is used and scroll down to the buttom with a click on button in PDF.

Is there anyone who can assist a bit...
 

Attachments

  • Pagedown1.accdb
    960 KB · Views: 126

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:57
Joined
Feb 28, 2001
Messages
27,140
There are ways to do this, but the most common approach is not quite done exactly as you said. If your target application (e.g. Adobe) provides a set of library modules for VBA automation, then you could add the library to the Access list of references. Once you did that, you could write code in VBA to create what is called an Application Object (specific to the target app). I use Excel and Word app objects frequently and some of our users also use PowerPoint and Outlook. Once this app object has been created, you will have control over it through VBA automation. The topic to search in order to get some hints on the flavor of this type of thing is "Application Objects with VBA."

If you target an app that doesn't have or doesn't expose its control library then you have a much harder task. I won't say impossible, but I think it becomes very difficult. I know Adobe has such a library and in fact we have had discussions about it in the non-Access forum areas.
 

jdraw

Super Moderator
Staff member
Local time
Today, 00:57
Joined
Jan 23, 2006
Messages
15,379
If in your set up generally, you have identified Foxit as the program associated with .pdf files, then
you may want to add a filter in your FileDialog
.Filters.Add "PDF Files", "*.pdf"

Here is a link to FileDialog documentation with example.

This does NOT allow you to manipulate (edit) the pdf file from with vba.

Good luck.
 

Shas91

Member
Local time
Yesterday, 21:57
Joined
Nov 26, 2019
Messages
41
To jdraw: well it was not the case. The problem is to get the code behind buttom 2.Page down to work...

To the Doc_Man.. I have tried but no luck I have to give it some more efforts...
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:57
Joined
Feb 28, 2001
Messages
27,140
If you have the library routines for the utility in question, what you want is possible and once you become accustomed to it, rather easy. HOWEVER, if you do not have the library routines, you are strictly out of luck. Here is the technical reason.

Windows is designed based on a security concept called "isolation." Each task in memory is allocated its own chunk of memory for the life of the task. No other task can reach into that task's memory to do anything. Each task is ISOLATED from each other task. This is actually a security requirement imposed by more than one government, and if Windows hadn't been built that way, the governments would not have bought Windows workstations for office use.

So... if you have the ability to create an Application Object for the given program, you can create that object. In that case, its control channels are already linked to your task. You control what it does because it was built for your task through that app object creation step and that correct linkage is automatic.

If you are working with a task that you DIDN'T create through App Object methods, its controls are probably linked to the Windows Screen Interface and you will not be able to reach those controls - because they don't belong to you. They belong to a different task, and ISOLATION becomes the barrier to be overcome.

Your code would then have to somehow be granted advanced privileges to override those control channel connections. AND you would have to go through the trouble of finding the window in which that target task was running so that you might diddle with the control channel connections, because each window in a Windows system has its own set of control channels. (It's a per-process property.) There IS a way to do it. But the code required to find the task and change its connections is formidable. IF you could do that, you could then use the controls to find your desired button and click it. If you can't do that, you are going nowhere fast.

Your answer was "you have tried but no luck." That doesn't tell me WHAT you tried. Did you find a set of library routines for that app? Or is that where you ran into "no luck"? I can tell you that the other pathway, manipulating the cursor, requires some very sophisticated code. It HAS been done - but it's the level of code that is complex enough that people can actually sell it for good money.
 

Shas91

Member
Local time
Yesterday, 21:57
Joined
Nov 26, 2019
Messages
41
Eureka! - I have found it!...

I actually found a easy way to solve the problem... So much time I have spend on this and what a simple sollution... When I search the web I also found out that I'm not alone...

The sollution:

1. By vba move the cursor to the program You want to control.
2. By vba click and release.
3. By vba use SendKeys "{PGDN}" or what ever You want to do...

And there You have it...

Have a nice weekend // Stefan
 

Shas91

Member
Local time
Yesterday, 21:57
Joined
Nov 26, 2019
Messages
41
even when there are more than 2 pages?
In my case it is invoices, normaly just one page. But You got a point...

I solve it like this, uggly, I know but an uggly solution is better than no solution

SendKeys "{PGDN}{PGDN}{PGDN}{PGDN}"
 

Gasman

Enthusiastic Amateur
Local time
Today, 05:57
Joined
Sep 21, 2011
Messages
14,231
In my case it is invoices, normaly just one page. But You got a point...

I solve it like this, uggly, I know but an uggly solution is better than no solution

SendKeys "{PGDN}{PGDN}{PGDN}{PGDN}"
Use Ctrl & End ?
 

Shas91

Member
Local time
Yesterday, 21:57
Joined
Nov 26, 2019
Messages
41
Use Ctrl & End ?

Works perfect, thanks ! and the otherway pageup x 4 ?

It still an uggly solutions but I'm so glad I finaly have access to it. This is opening a new world. If You for an example whant to automaticly fill in data in an external program You can do it by vba copy past... Its a bit tricky to find out where the external program is place on the screen but its doable....
 

Isaac

Lifelong Learner
Local time
Yesterday, 21:57
Joined
Mar 14, 2017
Messages
8,774
Works perfect, thanks ! and the otherway pageup x 4 ?

It still an uggly solutions but I'm so glad I finaly have access to it. This is opening a new world. If You for an example whant to automaticly fill in data in an external program You can do it by vba copy past... Its a bit tricky to find out where the external program is place on the screen but its doable....
Please be aware that using SendKeys to control programs is, across the development world, considered to be very high-risk and therefore, not recommended.

I'm not trying to be critical of anyone in this thread (as I'll confess I've toyed around with SendKeys usage more than once over the years), but the best advice you'll get on it IMO is not to use it.

The reason is nothing other than the obvious that you already probably guessed: It's too uncertain what it's going to do, and might end up not only failing to do what you want it to do, but doing something else really harmful, or embarrassing ...

In all my time in the tech world, I have seen a single case where SendKeys was used regularly and actually worked correctly on a regular basis (although not 100%). A co-worker had set up a special Citrix desktop environment that was dedicated to this purpose. They would open two programs exclusively on that desktop: 1) the Excel workbook containing the sendkeys macro, 2) an AS400 Terminal screen, whose usage was exclusively centered around keystrokes and was guaranteed to hold the focus and keep its window at the Fore. The keystrokes would disposition certain medical claims in a certain way - and in fact, its success was also dependent on return messages that the macro was able to detect from AS400 via an API set up for that specific purpose. It was a rather strange and rare scenario - and the only time I've ever seen it done to accomplish any half-way serious purpose, over the long term, successfully.

SendKeys is fun & interesting until it accidentally deletes a file, activates the wrong window (like a Chat message to your Boss) and presses Enter, ruins what you are doing on your primary screen, accidentally edits or adds something, etc.

You'll also run into a plethora of issues due to user-specific program configurations. For example, I've tried to use it to Tab a certain number of times on things like: 1) a Browser, 2) a PDF.......then realized that the # of times Tab was needed would be totally different depending on how users had configured their favorites, menu, tabs, and a host of other things.

Use with extreme caution and only after understanding the risk.
 

vhung

Member
Local time
Yesterday, 21:57
Joined
Jul 8, 2020
Messages
235
Good day...

Nice attempt, SendKeys "{PGDN}{PGDN}" is enough for scroll down speed.
Button: SendKeys "{PGUP}" is for 1 step, could be up and down fast scroll.

However, if you have to open a PDF and scroll to the last page,
Rather use the pdf header, number page box.

See attaced files....
 

Attachments

  • pdflastpg.png
    pdflastpg.png
    307.9 KB · Views: 90
Last edited:

Users who are viewing this thread

Top Bottom