sendkeys to Paint

P

Patricia Shannon

I am printing a bunch of documents containing screen prints of mainframe
screens, which have a black background. We did not want to print these as is
because it would use too many toner cartridges. I found I can copy the
picture into Paint, invert the colors, and copy back to Word.
Since I will be doing so many, I wrote a VBA macro for Word, which uses
Sendkeys to execute the needed actions in Paint. But it took longer to write
the macro than I expected, although not so long as to be a waste of time.

The problem is that I can't send the shortcut keys (eg., Ctrl+A, Ctrl+V)
that I would normally use when operating directly in Paint. They had no
effect. I was able to do what I wanted by using the menu commands (eg.,
Alt+EA, Alt+EP).
Also, I had to use more than one Sendkeys, because commands after the paste
did not execute.
I am including the macro I got to work (which includes comments to myself
about things that didn't work).
I'm wondering if my problems are specific to Paint, or are more general?

Sub InvertColors()
'
' InvertColors Macro
' Macro recorded 7/11/2006 by Patricia Shannon
' Invert colors of a picture, using Paint program
' Picture must be selected before macro is run
'
' ---- Word commands ---
Selection.Copy ' copy selected picture

' --- run Paint

AppActivate "untitled - Paint"
''' Note: Sendkeys Ctrl-codes (eg. "^A") do not work with Paint
''' Note: When all commands sent to Paint in one SendKeys,
''' SendKeys "%EA%EP%II%EC%{TAB}", True
''' actions ended after paste (%EP)
''' SendKeys "^A^V^C%{TAB}", True ' Ctrl-A Ctrl-V Ctrl-C return to
this application

SendKeys "%EA%EP", True
AppActivate "untitled - Paint"
SendKeys "%II%EC%{TAB}", True

' ---- Word commands ---
Selection.Delete Unit:=wdCharacter, Count:=1 ' delete old picture
Selection.Paste ' copied converted picture
Selection.MoveLeft Unit:=wdWord, Count:=1 ' scroll left

End Sub
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGF0cmljaWEgU2hhbm5vbg==?=,

I'm sorry, but it's not clear exactly what the problem is? What end result are
you not getting?

FWIW, SendKeys is shakey ground at best...
I am printing a bunch of documents containing screen prints of mainframe
screens, which have a black background. We did not want to print these as is
because it would use too many toner cartridges. I found I can copy the
picture into Paint, invert the colors, and copy back to Word.
Since I will be doing so many, I wrote a VBA macro for Word, which uses
Sendkeys to execute the needed actions in Paint. But it took longer to write
the macro than I expected, although not so long as to be a waste of time.

The problem is that I can't send the shortcut keys (eg., Ctrl+A, Ctrl+V)
that I would normally use when operating directly in Paint. They had no
effect. I was able to do what I wanted by using the menu commands (eg.,
Alt+EA, Alt+EP).
Also, I had to use more than one Sendkeys, because commands after the paste
did not execute.
I am including the macro I got to work (which includes comments to myself
about things that didn't work).
I'm wondering if my problems are specific to Paint, or are more general?

Sub InvertColors()
'
' InvertColors Macro
' Macro recorded 7/11/2006 by Patricia Shannon
' Invert colors of a picture, using Paint program
' Picture must be selected before macro is run
'
' ---- Word commands ---
Selection.Copy ' copy selected picture

' --- run Paint

AppActivate "untitled - Paint"
''' Note: Sendkeys Ctrl-codes (eg. "^A") do not work with Paint
''' Note: When all commands sent to Paint in one SendKeys,
''' SendKeys "%EA%EP%II%EC%{TAB}", True
''' actions ended after paste (%EP)
''' SendKeys "^A^V^C%{TAB}", True ' Ctrl-A Ctrl-V Ctrl-C return to
this application

SendKeys "%EA%EP", True
AppActivate "untitled - Paint"
SendKeys "%II%EC%{TAB}", True

' ---- Word commands ---
Selection.Delete Unit:=wdCharacter, Count:=1 ' delete old picture
Selection.Paste ' copied converted picture
Selection.MoveLeft Unit:=wdWord, Count:=1 ' scroll left

End Sub

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
P

Patricia Shannon

Thank you for your reply. I was able to get the end result I needed, but I
thought someone might help me to understand why some things didn't work the
way they should, to help further my understanding so I can do a better job in
the future, and not waste time trying things that don't work. Also, I admit
I am a seeker of knowledge, and gain satisfaction from understanding. You
don't like the use of Sendkeys, but you didn't give an alternate.
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGF0cmljaWEgU2hhbm5vbg==?=,
I was able to get the end result I needed, but I
thought someone might help me to understand why some things didn't work the
way they should, to help further my understanding so I can do a better job in
the future, and not waste time trying things that don't work. Also, I admit
I am a seeker of knowledge, and gain satisfaction from understanding. You
don't like the use of Sendkeys, but you didn't give an alternate.
And you still haven't clarified *what* didn't work, which makes it difficult to
give you any answer beyond the fact that SendKeys is chancy, at best. The reason
is because there's no way to ensure that things will be "just so" when the
keyboard commands are sent. For some reason or another, the application state
might not be what you expect, so the commands "disappear into the blue" or cause
the wrong actions.

Some programs provide an automation interface and some do not. Paint does not,
so there's really no reliable way to control it from code. The alternate would
be to find a graphics program that does provide an automation interface and use
that, not Paint.

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 
M

Mike Starr

The question I have is why do you need to involve Word? When you do your
screen capture from the mainframe screens, why not paste them into Paint at
that point, make your changes and then save them as individual files (.gif
files work best for screen captures). Then you can import them into word
(Link to file works best). Or if you have a lot of them to do, there are a
number of tools that can help with the process (SnagIt is one, Paint Shop
Pro is another ... in addition to being a great graphic editor, it's got a
built-in screen capturing utility that allows you to capture a series of
images and save them with configurable file names).

Mike
 
P

Patricia Shannon

Thank you. Since this is a project at work, I don't have a choice in the
programs I have available. I wouldn't be able to justify asking the company
to install another program for this one project. Since I am using the macro
on one picture at a time, not looping through them all, if something goes
wrong, like forgetting to start the Paint program or forgetting to select
the picture, I see it right away and fix it. I am saving a whole lot of time
with this macro. Eg., I just converted a document with 61 screen prints!
And I have a bunch of documents.

The things that don't work are :
(1) I can't use Sendkeys to send a shortcut using the Ctrl key, even though
I can send commands that use the Alt key. In this case, I was able to
accomplish what I wanted using the Alt, maybe because I was accessing the
menus, not doing shortcuts, but I don't know. In the future, there might be
a case where I would really have to send a CTL sequence.
(2) I could send several commands in one SendKeys, but when I sent a paste
command, the following commands apparently were ignored, so I had to use
multiple AppActivate/SendKey commands. It would be helpful to know some
general rule, rather than trial and error.
 
P

Patricia Shannon

I'm sorry I posted this question, because all it has done is waste my time,
unlike most of the other problems I've posted.
The short answer to your questions is that I'm doing this for my job, and
this is what I have to work with. The only graphics program I have is Paint.
Also, time is limited, and I don't have time to do a bunch of exporting and
linking. These are documentations for SOX (Sarbannes-Oxley (sp?)), and will
probably never be looked at after I printed them and put them in binders. So
doing all that fancy stuff would not be worth it.
My task is to print these files, which already exist, and were created by
several other people. There are about 600 files. Some have more than 60
print screens. The original screen prints have a black background, and would
use too many toner cartridges to print, so I am using Paint to invert the
colors. Paint does the job fine.
 
P

Patricia Shannon

I'm sorry if my previous comments made it seem that I don't realize that
people are trying to be helpful. I do appreciate it. After all, you didn't
know the details of what I'm doing. Thank you for trying help.
 
M

Mike Starr

Not to worry... now that I know you've inherited a bunch of legacy documents
from some consulting company (seems to me that the worst documents always
seem to come from big four accounting/consulting companies) I understand. I
confess I thought you were crafting a needlessly complex solution to what I
considered a simple problem but that conclusion was based on invalid
assumptions on my part.

Now that I know what you're doing, I do have a suggestion. You can download
a 30-day trial of Paint Shop Pro. PSP has a scripting language. If you save
your word documents as HTML, each of the graphics will be saved as a file.
You can then use the scripting in PSP to mass tweak your graphics. Now,
reopen the HTML file with Word, and save as a revised Word document. Voilá

However, if your SendKeys solution is doing the job, that's cool too... I
ran into some trouble myself with SendKeys on another project. For some
reason, the first use of the SendKeys command would cause a 70-second freeze
of the user's computer, after which my macros went on their merry way as if
nothing was wrong.

Meanwhile, have fun.

Best regards

Mike
 
C

Cindy M -WordMVP-

Hi =?Utf-8?B?UGF0cmljaWEgU2hhbm5vbg==?=,
Since this is a project at work, I don't have a choice in the
programs I have available. I wouldn't be able to justify asking the company
to install another program for this one project. Since I am using the macro
on one picture at a time, not looping through them all, if something goes
wrong, like forgetting to start the Paint program or forgetting to select
the picture, I see it right away and fix it. I am saving a whole lot of time
with this macro. Eg., I just converted a document with 61 screen prints!
And I have a bunch of documents.
OK, I can identify with this :) (Most people who post in a "programming" group
do expect things to run without supervision, but if you're OK with that - OK
:))
(1) I can't use Sendkeys to send a shortcut using the Ctrl key, even though
I can send commands that use the Alt key. In this case, I was able to
accomplish what I wanted using the Alt, maybe because I was accessing the
menus, not doing shortcuts, but I don't know. In the future, there might be
a case where I would really have to send a CTL sequence.
Yes, the menu commands are on a "different wave-length" that was apparently
more accessible to SendKeys in whatever state/circumstances it found at the
time. If you ever need this in the future and it doesn't work you can try the
older Windows 3.0 alternate: Shift+Insert (= paste)
(2) I could send several commands in one SendKeys, but when I sent a paste
command, the following commands apparently were ignored, so I had to use
multiple AppActivate/SendKey commands. It would be helpful to know some
general rule, rather than trial and error.
When you Paste you're invoking the Windows Clipboard, which means the
application could lose focus. Plus, it takes a measurable amount of time for
the Paste action to complete, which means any additional keys you sent may fall
down a "black hole". This is the kind of thing that makes SendKeys so chancy
and unpredictable :)

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question or reply
in the newsgroup and not by e-mail :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

VBA word change keys 0
How to clear prior search terms from Navigation Pane 0
sendkeys 0
SendKeys with enter 1
Sendkeys 6
AppActivate will not retain focus 2
SendKeys 3
Can't Paste into Paint, any suggestions? 2

Top