From: Mike Williams on
On 19 Nov, 08:52, not.t...(a)address.spam.no (beginner) wrote:

> You could perhaps tell me when
> this code will not work.

Okay. Further to my previous response I've just tried exactly the same
code on a spare XP machine of my own and it behaves pretty much the
same as it does on my Vista machine, opening up dozens of copies of MS
Paint and not actually performing the task it is supposed to perform.

I then tried it on another XP machine belonging to my grandson and it
doesn't work at all on that machine and just bombs out with an error
because MS Paint is not installed. So if you want it to work on such
machines you will need to make sure that you create an install program
for your VB code that also installs a copy of MS Paint if it does not
already exist, which will undoubtedly give you some licensing
headaches with Microsoft.

Mike

From: beginner on
In article <38902846-7190-4305-b753-31a591fb4ac0(a)w28g2000hsf.googlegroups.com>,
Mike Williams <gagamomo(a)yahoo.co.uk> wrote:
>On 19 Nov, 08:52, not.t...(a)address.spam.no (beginner) wrote:
>
>You're wrong. Leaving aside the "idiot proof code" thing (which is
>another subject in itself!) I want code that works using whatever
>methods are available. If native VB methods are capable of doing a
>specific job then I'm quite happy to use them, and in fact I would
>prefer to do so. I use API commands, as you call them, only when
>native VB methods are not up to the job.

OK, then we have the same approach and attitude.

>
>> while I want something short and simple
>
>So do I, if it works.
>
>> which works most of the time on three
>> of my computers.
>
>But so far you've only shown that it works on ONE of your own
>computers, not three of them!

I knew it wouldn't work on Vista, but about the path I knew how to handle.
I have been doing it for other purposes also, and know that the SYSTEM32
directories are under WINNT, WINDOWS and WIN97.

In any case, I want my code to work all
>of the time, not just some of the time or even most of the time, and I
>want it to work on other computers as well as just one of the few
>specific machines that currently happens to belong to me.
>
>> So this is what I wrote.
>> Filnamn = "A.BMP"
>> PaintCommand = "C:\WINNT\SYSTEM32\MSPAINT.EXE " & Filnamn
>> X = Shell(PaintCommand, 1)
>> SendKeys "%FA{TAB}{DOWN}{DOWN}{DOWN}{DOWN}
>> {DOWN}{UP}{UP}{UP}{TAB}{ENTER}Y%FX"
>>
>> I am not sure it will work on Vista
>
>It won't. And neither will it work on Win98 or WinXP, partly because
>the path is wrong on those machines and partly because the basic idea
>is flawed. You can easily get around the path problem in a number of
>different ways, and in fact you can fix the path problem by taking
>advantage of the fact the Paint is normally a properly registered
>program and by omitting everything expect the "MSPaint.exe " part of
>the path, but you still won't make it work, at least on most systems.
>It might work on a few systems that happen to be set up similar to
>your own NT machine, if you're lucky, but certainly not on most of
>them.

This is how I have extended it now.

Sub bmp_in_16 (FilNamn As String)

Dim PaintCommand As String, PaintPath As String

Call PaintDir(PaintPath)
PaintCommand = PaintPath & "MSPAINT.EXE " & FilNamn
If FileExists(PaintPath & "MSPAINT.EXE") < 1 Then Exit Sub
' MsgBox "MSPAINT.EXE not found in SYSTEM32"
' Exit Sub
' Else
' MsgBox "MSPAINT.EXE found"
' End If
' PaintPath = "C:\WINNT\SYSTEM32"
' If DirExists(PaintPath) = 1 Then PaintCommand = PaintPath & "\MSPAINT.EXE " & FilNamn
' PaintPath = "C:\WINDOWS\SYSTEM32"
' If DirExists(PaintPath) = 1 Then PaintCommand = PaintPath & "\MSPAINT.EXE " & FilNamn
' On Error Resume Next
X = Shell(PaintCommand, 1)
' SendKeys "%FA{TAB}{DOWN}{UP}{UP}{TAB}{ENTER}Y%FX" unreliable
SendKeys "%FA{TAB}{DOWN}{DOWN}{DOWN}{DOWN}{DOWN}{UP}{UP}{UP}{TAB}{ENTER}Y%FX"

End Sub

Sub PaintDir (DirName As String)

DirName = "C:\WINDOWS\SYSTEM32\" ' most likely
If DirExists(DirName) Then Exit Sub
DirName = "C:\WINNT\SYSTEM32\"
If DirExists(DirName) Then Exit Sub
DirName = "C:\WIN97\SYSTEM32\"
If DirExists(DirName) Then Exit Sub
DirName = "" ' no directory

End Sub

Function DirExists (DirName As String) As Integer

Dim DirName1 As String

DirName1 = DirName
If Right(DirName, 1) <> "\" Then DirName1 = DirName & "\"

On Error GoTo NoDir
DirExists = 0

If Dir$(DirName1) <> "\\" Then DirExists = 1
' Label2 = "1, exists"
Exit Function

NoDir:
' Label2 = "0, does not exist"
Exit Function

End Function

Sub Command1_Click ()
Call bmp_in_16("A.bmp")
End Sub

If MSPAINT.exe is not there, it won't do the work and won't complain
either. Note that this code is not really meant for others, so I
don't have to explain things to the "user".

I suspect this might work on Vista if compiled first. There should be
a single bmp file called A.bmp in any resolution.

>
>> You could perhaps tell me when this code
>> will not work.
>
>Okay. I've just tried your code on my own Vista machine, running it in
>the IDE, and I get a "Runtime error 70 - Permission Denied", as I
>expected I would because SendKeys does not work in Vista when running
>in the IDE. SendKeys works in Vista when the code is compiled though,
>so I compiled the code to a standard native code exe and tried it
>again.


>To make things even worse, there was no "16 colour" copy of the
>original anywhere on my system, so as well as messing up my computer
>and forcing me to manually close 68 separate windows to get it working
>again it did not even carry out the task it was supposed to carry out!

Why did it have to open 68 windows?

I suspect new versions of Paint do different things and may have
options and commands in different locations, in which case, I will
have to check from the computer running Vista.

>> I have learnt something, and also solved a
>> little problem . . .
>
>Well, you've learned somnething alright, but I can't see what problem
>you've solved?

I often create a lot of bitmaps in 24 bit where the actual colour has
little use, but it is good to have a few lines in different
colours. These occupy 1 MB each, so this little subroutine can cut out
the size by a large factor.

>
>> Thanks again to you and others who have tried
>> to help.
>
>We've done more than "try" to help. We've given you "real" help and
>pointed you to code that actually works and that does exactly the job
>you require. That surely qualifies us for something more than a mere
>'tried to help', doesn't it?

I agree. You and others who helped deserve to be thanked profusely (no
irony intended). I have learnt several things from this newsgroup and
am thankful to others also who have answered other people's elementary
questions from which I have been able to learn.

>
>> The objective now is only to learn something
>> new, for which I have limited time, but
>> unlimited energy and urge.
>
>I'm afraid that energy and urge in themselves are not a great deal of
>use if you have no time in which to use those otherwise commendable
>attributes. If I were you I would spend a little time looking at the
>various things we have previously posted in this thread and using them
>to help you to write your code in a way that will actually work.
>
>Mike
>

I don't have the time to work on this too often. I have a normal job
to do during the day time, and it has to be given its due priority.

From: Mike Williams on
On 19 Nov, 16:24, not.t...(a)address.spam.no (beginner) wrote:

> This is how I have extended it now.
> Sub bmp_in_16 (FilNamn As String)
> loads of code snipped <

And are you saying that the code you posted is actually working? It
certainly does not work at this end, either on my Vista machine or on
my laptop or on my XP machine.

Have you tried it on all three machines that you've got at your end?
And does it work on them? I would be very surprised if it does.

> I suspect this might work on Vista if
> compiled first. There should be a single
> bmp file called A.bmp in any resolution.

No. It does not work in Vista, compiled or not, even if I use a .bmp
file in a directory that Vista allows access to. And it does not work
on my XP machine either, or on my laptop.

> Why did it have to open 68 windows?

It is almost certainly a timing problem (along with a few other
problems), which makes me wonder how you have managed to get the code
working on your own three systems. Are you *sure* that it works on
your own systems? On all the machines on which I have tried it(three
different machines and two different versions of Windows) MS Paint
opens up okay (as I would expect it to) but the keys (SendKeys) are
sent *before* MS Paint has finished opening, and therefore *before* MS
Paint has the focus. Those key strokes are therefore sent to the VB
app itself, which in this specific case appears to be causing the
Command Button to be "pressed" again, which of course sends the keys
again, also sending them to the VB app *before* the next instance of
MS Paint has fully opened, etc, etc, resulting in a loop in which as
many instances of MS Paint as my system will cope with opening up
until it eventually runs out of the required resources and effectively
locks up my system.

In order to reliably send the keys to the MS Paint window your code
needs to open up MS Paint and then wait until the MS Paint window
actually has the focus before your code sends any key strokes. To do
this properly of course requires you to use various API functions, but
if you're going to use API functions (which you've said you really
don't want to use) to perform this little task then you might as well
use the API to do the job properly in the first place without ever
getting MS Paint or any other third party app involved!

By the way, even when I do add some code to make it wait until the MS
Paint Windows has the focus before sending the keystrokes it still
does not work. It fixes the "60 or 70 instances of MS Paint opening
up" problem (which of course I expected it to) but only the first two
keystrokes (Alt FA) to get the "Save As" dialog opened works, with the
remaining keystrokes getting lost and doing nothing at all. This,
again, is due to a timing problem in which the remaining keystrokes
are being sent *before* the MS Paint "Save As" dialog fully opens and
gets the focus, and so the remaining keystrokes get swallowed up by MS
Paint itself and do not get sent to the Save As dialog. Again, this
problem is easy to fix with some additional code but (like I said in
the beginning) you are going to need to cross quite a few bridges
before you will get this code working reliably. Those bridges are
actually fairly easy to cross, but even when you have done so there
will be no guarantee that it will work on all machines because there
is no guarantee whereabouts in the drop down list is the "16 color"
option (although in many cases I expect it will be second from the top
in the list). So, in order to do this with full reliablity you are
really going to need to get involved with the API again (which is
something you have said you don't want to do).

By the way, the fact that the code you posted apparently runs at your
end without producing a "variable not defined" error (which it
apparently does) tells me that you are not in the habit of using
Option Explicit at the top of all your Form and code modules. Failing
to use Option Explicit will lead you into all sorts of difficulties
and I would strongly advise you to use it in future. While you're at
it, you might as well make it the default behaviour by using the
Tools / Options menu in your VB IDE and clicking the Editor tab and
placing a tick against "Require variable declaration".

> I often create a lot of bitmaps in 24 bit
> where the actual colour has little use,
> but it is good to have a few lines in
> different colours. These occupy 1 MB each,
> so this little subroutine can cut out the
> size by a large factor.

Well, it will if you ever get it working properly. But as an aside,
why do you create lots of bitmaps in 24 bit colour and then convert
them later to 4 bit 16 colour bitmaps to save memory? Why don't you
just create them as 4 bit 16 colour bitmaps in the first place and
save yourself a job?

As a final note, I'll help you to get this "use MS Paint to convert a
bitmap" thing off the ground if you wish, and I'll do so without using
a single API function if you really wish, as I'm sure will other
people here, but it really isn't the way I would advise going about it
myself. Especially when you've already been shown in this very thread
how to do it properly without getting MS Paint involved at all.

Mike

From: beginner on
In article <e69fafef-8f18-43e4-a47c-087969f20ad8(a)d4g2000prg.googlegroups.com>,
Mike Williams <gagamomo(a)yahoo.co.uk> wrote:
>On 19 Nov, 16:24, not.t...(a)address.spam.no (beginner) wrote:
>
>> This is how I have extended it now.
>> Sub bmp_in_16 (FilNamn As String)
>> loads of code snipped <
>
>And are you saying that the code you posted is actually working? It
>certainly does not work at this end, either on my Vista machine or on
>my laptop or on my XP machine.

Only on this NT.

>
>Have you tried it on all three machines that you've got at your end?
>And does it work on them? I would be very surprised if it does.

No, it won't work on Vista.

>
>> I suspect this might work on Vista if
>> compiled first. There should be a single
>> bmp file called A.bmp in any resolution.
>
>No. It does not work in Vista, compiled or not, even if I use a .bmp
>file in a directory that Vista allows access to. And it does not work
>on my XP machine either, or on my laptop.

The trouble is that Vista contains a different MSPAINT.exe.

There the commands (keystrokes) will have to be different.

I intend to recognise the presence of Vista by seeing the
presence of C:\WINDOWS\vistawuredir.cab or
C:\Applications\oem\Vista_Manual_EN.pdf
and then giving a different sequence to SendKeys. Does that
make sense?

>
>> Why did it have to open 68 windows?
>
>It is almost certainly a timing problem (along with a few other
>problems), which makes me wonder how you have managed to get the code
>working on your own three systems. Are you *sure* that it works on
>your own systems? On all the machines on which I have tried it(three
>different machines and two different versions of Windows) MS Paint
>opens up okay (as I would expect it to) but the keys (SendKeys) are
>sent *before* MS Paint has finished opening, and therefore *before* MS
>Paint has the focus. Those key strokes are therefore sent to the VB
>app itself, which in this specific case appears to be causing the
>Command Button to be "pressed" again, which of course sends the keys
>again, also sending them to the VB app *before* the next instance of
>MS Paint has fully opened, etc, etc, resulting in a loop in which as
>many instances of MS Paint as my system will cope with opening up
>until it eventually runs out of the required resources and effectively
>locks up my system.

This does not happen on my machine, but to be safer, I should use the
wait option in the SendKeys command.

>will be no guarantee that it will work on all machines because there
>is no guarantee whereabouts in the drop down list is the "16 color"
>option (although in many cases I expect it will be second from the top
>in the list).

I am going to make it work only on the three machines I have. People
can always have their own things on which this is not going to work.
My objective is not to produce code for others, but just for myself.

So, in order to do this with full reliablity you are
>really going to need to get involved with the API again (which is
>something you have said you don't want to do).

I have nothing against API. I want to do it in as simple a way as
possible, and preferably in as few lines of code as possible. If there
is a good API which can cut down all this coding, I don't see any big
problem with it.

>
>By the way, the fact that the code you posted apparently runs at your
>end without producing a "variable not defined" error (which it
>apparently does) tells me that you are not in the habit of using
>Option Explicit at the top of all your Form and code modules. Failing
>to use Option Explicit will lead you into all sorts of difficulties
>and I would strongly advise you to use it in future. While you're at
>it, you might as well make it the default behaviour by using the
>Tools / Options menu in your VB IDE and clicking the Editor tab and
>placing a tick against "Require variable declaration".

I also use End without closing windows, but these things I can learn.

>
>> I often create a lot of bitmaps in 24 bit
>> where the actual colour has little use,
>> but it is good to have a few lines in
>> different colours. These occupy 1 MB each,
>> so this little subroutine can cut out the
>> size by a large factor.
>
>Well, it will if you ever get it working properly. But as an aside,
>why do you create lots of bitmaps in 24 bit colour and then convert
>them later to 4 bit 16 colour bitmaps to save memory? Why don't you
>just create them as 4 bit 16 colour bitmaps in the first place and
>save yourself a job?

These are created with the simplest code I have had for years. It is
done with SavePicture.

>
>As a final note, I'll help you to get this "use MS Paint to convert a
>bitmap" thing off the ground if you wish, and I'll do so without using
>a single API function if you really wish, as I'm sure will other
>people here, but it really isn't the way I would advise going about it
>myself. Especially when you've already been shown in this very thread
>how to do it properly without getting MS Paint involved at all.
>
>Mike
>

I hope some day I will be advanced enough to do things the way you
do. Till then I am trying to learn as well as creating a little
utility which serves some useful purpose.


























From: Mike Williams on
On 20 Nov, 06:13, not.t...(a)address.spam.no (beginner) wrote:

> I intend to recognise the presence of Vista by
> seeing the presence of C:\WINDOWS\vistawuredir.cab
> or C:\Applications\oem\Vista_Manual_EN.pdf

Why C:\?

> and then giving a different sequence to SendKeys.
> Does that make sense?

No. What would make sense to me would be to do the job without getting
MS Paint involved at all, perhaps using the code at the link we have
already posted for you. If MS Paint exposed some reliable and
documented functions then it would be fine to use them in your app,
but it does not do so and the method you are using of "hitting keys"
in your code in the hopes that those specific keystrokes suit whatever
version of Paint happens to be installed does not make sense at all to
me.

In fact, if you want 4 bit (16 colour) bmp files then it would make
sense to save them in that format initially, rather than creating them
as 24 bit full colour bitmaps and then converting them. Such a task
would of course require fairly heavy use of the various Windows API
functions, which you have said you don't really want to use, but you
will be helped greatly in that task if you study the code at the links
we have already posted.

By the way, what is it you are doing that requires only a 4 bit (16
colour) bitmap? Can you explain exactly what these images are, and
what their purpose is? The reason I ask is that it is likely that
something other than a bmp format might better suit your needs.

Mike