Capture keystroke to close Word doc?

E

Ed from AZ

I have a VB6 application (personal use only, not going to be
distributed) that opens Word documents (Word 2000) that I select.
When the Word file opens, Word become the active application (of
course), covering the VB app form.

I would like to somehow be able to close the Word file with a single
keystroke - any keystroke, really, because I don't want to make
changes to these docs while the VB app is running. I already have a
keystroke-capture routine in the VB app, but that goes to the
background and is unavailable when the Word doc opens. I can't
program the "doc.close" into Word without affecting Word in general,
unless I can somehow set something the detects the VB app and "If this
then close Word on any keystroke"? Or open the Word doc and then make
VB the active app without showing the form?

Any suggestions?
Ed
 
D

Dave O.

What are you using for your existing keystroke routine?

If you're not using GetAsyncKeyState have a look at it, using that with a
timer and GetForegroundWindow will allow you to monitor for keystrokes while
Word is active with the focus.
Are you certain about the wisdom of using any key to kill Word, are all the
documents to be viewed less than one screen? Because if they are not,
scrolling with the keyboard will with your plan kill Word.

Dave O.
 
E

Ed from AZ

Hi, Dave. I've mid-posted some reponses.

What are you using for your existing keystroke routine?

I don't have my laptop with me right now - I _knew_ I shouldn't have
tried this without the code!! 8>(
If you're not using GetAsyncKeyState have a look at it, using that with a
timer and GetForegroundWindow will allow you to monitor for keystrokes while
Word is active with the focus.

So a timer set for a small interval will catch keystrokes in the
active app while VB is background? Cool! I guess I thought that when
VB went to the background it kind of went dormant. But no, that
wouldn't make sense - the code is still active and running.
Are you certain about the wisdom of using any key to kill Word, are all the
documents to be viewed less than one screen? Because if they are not,
scrolling with the keyboard will with your plan kill Word.

Actually, all the docs are exactly one screen - as long as I turn the
laptop and screen view sideways. I do this with my display driver,
and then set the Zoom to 93% whenever I open a doc. The problem I'm
running into is that I'm using CTRL+W to close the doc, but if I miss
the CTRL I put a W in the doc - and now I have to re-execute a Close
command AND tell it not to save changes. Just a few seconds and a
minor annoyance - but one I'd like to program out so I can change docs
quickly with a bare minimum of fuss.

Ed
 
D

Dave O.

Have you considered using a Word viewer to look at the files, it should load
far faster and editing the file deliberately or by accident is not an
option:

http://www.microsoft.com/downloads/...87-8732-48D5-8689-AB826E7B8FDF&displaylang=en


I can still see circumstances where allowing any key press would not be
good, how about setting the keyboard monitor to watch the Escape key and
have it close on that keystroke.
Another issue is that the keystroke will still be transmitted through to
Word so if you do hit a letter it will be added to the document so you will
still get the Save/Cancel dialogue.
Taking these issues into consideration I reckon you'd be better off using a
non-editing viewer for this.

Dave O.

Hi, Dave. I've mid-posted some reponses.

What are you using for your existing keystroke routine?

I don't have my laptop with me right now - I _knew_ I shouldn't have
tried this without the code!! 8>(
If you're not using GetAsyncKeyState have a look at it, using that with a
timer and GetForegroundWindow will allow you to monitor for keystrokes
while
Word is active with the focus.

So a timer set for a small interval will catch keystrokes in the
active app while VB is background? Cool! I guess I thought that when
VB went to the background it kind of went dormant. But no, that
wouldn't make sense - the code is still active and running.
Are you certain about the wisdom of using any key to kill Word, are all
the
documents to be viewed less than one screen? Because if they are not,
scrolling with the keyboard will with your plan kill Word.

Actually, all the docs are exactly one screen - as long as I turn the
laptop and screen view sideways. I do this with my display driver,
and then set the Zoom to 93% whenever I open a doc. The problem I'm
running into is that I'm using CTRL+W to close the doc, but if I miss
the CTRL I put a W in the doc - and now I have to re-execute a Close
command AND tell it not to save changes. Just a few seconds and a
minor annoyance - but one I'd like to program out so I can change docs
quickly with a bare minimum of fuss.

Ed
 
L

Larry Serflaten

Hi, Dave. I've mid-posted some reponses.

What are you using for your existing keystroke routine?

I don't have my laptop with me right now - I _knew_ I shouldn't have
tried this without the code!! 8>(
If you're not using GetAsyncKeyState have a look at it, using that with a
timer and GetForegroundWindow will allow you to monitor for keystrokes while
Word is active with the focus.

So a timer set for a small interval will catch keystrokes in the
active app while VB is background? Cool! I guess I thought that when
VB went to the background it kind of went dormant. But no, that
wouldn't make sense - the code is still active and running.
Are you certain about the wisdom of using any key to kill Word, are all the
documents to be viewed less than one screen? Because if they are not,
scrolling with the keyboard will with your plan kill Word.

Actually, all the docs are exactly one screen - as long as I turn the
laptop and screen view sideways. I do this with my display driver,
and then set the Zoom to 93% whenever I open a doc. The problem I'm
running into is that I'm using CTRL+W to close the doc, but if I miss
the CTRL I put a W in the doc - and now I have to re-execute a Close
command AND tell it not to save changes. Just a few seconds and a
minor annoyance - but one I'd like to program out so I can change docs
quickly with a bare minimum of fuss.

Ed
 
L

Larry Serflaten

Actually, all the docs are exactly one screen - as long as I turn the
laptop and screen view sideways. I do this with my display driver,
and then set the Zoom to 93% whenever I open a doc. The problem I'm
running into is that I'm using CTRL+W to close the doc, but if I miss
the CTRL I put a W in the doc - and now I have to re-execute a Close
command AND tell it not to save changes. Just a few seconds and a
minor annoyance - but one I'd like to program out so I can change docs
quickly with a bare minimum of fuss.


Have you tried Alt+F4? I'm not sure, but nieither of those should
add anything to your document....

LFS
 
E

Ed from AZ

Have you considered using a Word viewer to look at the files, it should load
far faster and editing the file deliberately or by accident is not an
option:

No, I didn't think of the Viewer! That may be a good option, Dave.
Thanks!
I can still see circumstances where allowing any key press would not be
good, how about setting the keyboard monitor to watch the Escape key and
have it close on that keystroke.
Another issue is that the keystroke will still be transmitted through to
Word so if you do hit a letter it will be added to the document so you will
still get the Save/Cancel dialogue.

I'm using this to show me music that's on a single page. When it's
time to change songs, I need to close the doc and oen the next one.
I've got an external keyboard connected to a laoptop turned sideways.
CTRL+W and ALT+F4 both work and are options I currently use. Much
faster would be to simply hit the keyboard and close it.

I'd thought of the number pad 0 - right by my right hand and never
needed for this application. But that would need to be captured by
the VB app, which is not active when Word is showing.

Even better would be any key press at all. But that would have to be
captured by Word. Maybe a specific template with that code in it used
just for this application? Can I code keystroke monitoring into a
Word template?

Ed
 
N

Nobody

Ralph answered that. You can find samples by searching for it, but it
doesn't work with every key. If you prefer something that works with any
key, search the web for "vb SetWindowsHookEx WH_KEYBOARD_LL". Unlike other
hooks, the "_LL" version can reside in your EXE, so there is no need to make
a separate DLL. This has the same difficulty level as subclassing.

See RegisterHotKey().

Is that a normal VB6 or Windows XP OS function?

Ed
 
M

Mike Williams

I'm using this to show me music that's on a single page. When
it's time to change songs, I need to close the doc and open the
next one. I've got an external keyboard connected to a laptop
turned sideways. CTRL+W and ALT+F4 both work and are
options I currently use. Much faster would be to simply hit the
keyboard and close it. I'd thought of the number pad 0 - right
by my right hand and never needed for this application. But
that would need to be captured by the VB app, which is not
active when Word is showing. Even better would be any key
press at all. But that would have to be captured by Word . . .

As others here have already said there are lots of different ways of reading
and reacting to the keyboard which work fine in your VB app even if some
other app, such as your Word document, has the focus so you will be able to
close your Word doc (and load another if you wish) in response to any key
you want, or even to a press of any key on the keyboard. If you want to
ensure that your Word document does not also respond to any keys you might
press so that you don't get the "Do you want to save the changed document"
problem (and if you continue to use MS Word rather than a non editable
viewer) then a little more work is required (one way would be to intercept
the keys at a low level as already mentioned by "Nobody" and prevent Word
from acting on them). However, I personally think that you would be better
off ditching MS Word altogether and tackling the job a different way, unless
of course you actually need Word for an important part of the job you are
doing (so far you've only given us a rough outline of what your program is
designed to do).

You've said that you are using Word to display a single page which contains
some music and at various points in time you want to react to the keyboard
in order to cause Word to move to (or to load) the next page. Why are you
using Word to display the pages of music? Are these pages of music something
that you have created manually or by some other process, or are they
actually generated by your VB program? If they are sheets of music that you
have created using MS Word, or perhaps by some other program and then saved
them as Word documents, then why do you not instead create the pages as
graphic files, perhaps jpegs or, better still, metafiles (.emf files). Such
image files can be displayed by your VB program either directly on the Form
or into a PictureBox on the Form, without MS Word being involved at all and
you can switch between them any time you wish using any key(s) you wish. If
you have created your music pages in some other application then perhaps
that application has a facility to save them as metafiles. If not, or if you
have created them yourself in MS Word, then there are ways of persuading
Word to output them as metafiles for you. Once you have done that, you will
be able to load and display the metafiles under total control of your own VB
program, without anything else (such as MS Word or a Word viewer or anything
else) being involved at all, and it will be extremely easy to code. Is there
some reason why you cannot do it that way? It's definitely the way I would
tackle it if I were you.

Mike
 
E

Ed from AZ

Hi, Mike. I never thought of that approach. Almost all of these
files are actual Word docs, created and saved in Word by someone else
and passed along to me. My app has a list of all these docs by name;
I can type in the name and pull up a doc, then close it and pull up
the next one.

With a graphic or metafile (I'll have to look into that - I've got
Word 2000), I'm assuming I would create a separate form with a single
control, load the metafile into the control and make that form
visible. SInce VB is still the running app, I can then code anything
I want to control the viewing and closing. Does that sound right?

Ed
 
M

Mike Williams

Hi, Mike. I never thought of that approach. Almost all
of these files are actual Word docs, created and saved in
Word by someone else and passed along to me. With a
graphic or metafile (I'll have to look into that - I've got
Word 2000), I'm assuming I would create a separate form
with a single control, load the metafile into the control and
make that form visible. SInce VB is still the running app,
I can then code anything I want to control the viewing and
closing. Does that sound right?

Yep. That's about it. The first step would be to convert your Word docs into
suitable graphic files that can be loaded into a standard VB PictureBox
(.bmp, .jpg, .gif, .wmf or .emf). Any other kind of graphic file would
require you to use a third party control instead of a standard VB PictureBox
to display them, so it is probably be best to stick to one of those I've
mentioned. The first three file types mentioned are types of bitmap and the
last two are types of metafile. Bitmaps of line drawings and text and stuff
don't scale very well when you resize them whereas metafiles rescale fine,
so I would suggest either .wmf or .emf. Of the two .emf is the most
versatile. So, all you need to do is find a way of converting your existing
MS Word docs to .emf files and you will then be able to very easily display
the metafile at any size you require in a VB PictureBox.
I've got Word 2000

I use Word 2007 Ultimate myself and I've just checked now and it does not
allow me to convert or save a Word document as any of the above graphic
formats. If 2007 Ultimate does not do it then I'm fairly sure Word 2000 will
not do it either (although it is always worth you checking that out). It
does allow me to save as various kinds of web pages (which you could open in
a Browser control on your VB Form) but web pages don't preserve the
formatting of your Word document and they're not scaleable in ther way that
an image file is, so I wouldn't bother with them.

If you happen to have the full Office Suite, including MS Publisher, then
you could use Publisher to create your metafile. Just press CtrlA in your
Word document to select all followed by CtrlC to copy it to the clipboard.
Then in Publisher use the File / Paste Special menu and select Picture
(Enhanced Metafile) from the list. This will paste the Word document exactly
as it is laid out in Word into the Publisher document. You will then be able
to use the Publisher File / Save As menu and select Enhanced Metafile (.emf)
as the file type. I'm not sure if Publisher 2000 would offer the same
functionality as my own Publisher 2007 does, but its worth checking out.

If you don't have MS Publisher or anything else that offers the same
functionality then you could download and install a virtual Printer Driver
that can create metafiles. One such driver is PoleStar Virtual printer and
you'll find it at:

http://www.polestarsoft.com/virtual_printer/

It is completely freeware and there are no time limitations or anything. It
installs itself as a printer driver and once you have installed it (and set
up its options to your liking, including setting it to produce emf files
rather than the various other files it is capable of outputting) you can use
it to create an .emf file from any application that is capable of printing
(which means just about anything!). Just open your Word doc as normal and
use its File / Print menu. In the Print Dialog select the PoleStar Virtual
Printer (instead of your usual printer) and it will create and save an .emf
file for you. It only claims to be for Win 2000/ Win XP but it is possible
to get it going on Vista with a bit of work. I managed to get it running
okay on Vista myself today, and it works when set to save .emf files to a
default folder, but it does not work when you set it to bring up a file save
box. It produces good emf files though.

Just one point about emf files is that many applications that produce them
(such as MS Publisher and this PoleStart Printer Driver) do not give you an
option regarding how you want them to save text. This means that they save
it in the usual default way as a description of the Font and font details
used in various parts of the document, together with the character codes
etc. This means that such metafiles will only display correctly on machines
which themselves actually have all the fonts you have used installed. In
your case I imagine you have used some special music fonts (?). If so then
they are likely to be unavailable on machines other than your own. If that
is the case, and if you want the image to display properly on machines other
than your own, you have a choice of loading the .emf file into something
like Corel Draw and saving it out from Corel Draw selecting the "save text
as curves" option or alternatively adding code to your VB program to
temporarily install the font on the user's machine (l;icence restrictions
permiting) or you can save the file as some other type of graphic that does
not use fonts (jpeg for example). I would suggest sticking to .emf though
and using one of the other options. If there are only standard fonts in your
Word doc then of course it won't be a problem anyway.

Anyway, I've covered just a few of your options and there are certainly many
others. I'm off on holiday in the morning and so I won't see any of your
responses, but there are lots of others here who will help you with this.

Mike
 
E

Ed from AZ

Have a good holiday, Mike! I think I can get going from here. I have
Publisher; also IrfanView and Jasc Paint Shop Pro. Or I could just
install the PoleStar printer driver. I should be able to find a way to
iterate through all the docs in my folder and save out as either wmf
or emf.

Thanks so very much!
Ed
 
D

Dave O.

Mike Williams said:
Yep. That's about it. The first step would be to convert your Word docs
into suitable graphic files that can be loaded into a standard VB
PictureBox

I wonder if he could save the documents as RTF, if so he could show them in
the VB RTFbox, if ( and it's quite a big "if") RTF preserves the formatting
correctly it might be a lot easier than all that mucking about with images.

Regards
Dave O.
 
N

Nobody

Dave O. said:
I wonder if he could save the documents as RTF, if so he could show them
in the VB RTFbox, if ( and it's quite a big "if") RTF preserves the
formatting correctly it might be a lot easier than all that mucking about
with images.

Both MS Word and WordPad can convert from DOC to RTF.
 
D

Dave O.

Nobody said:
Both MS Word and WordPad can convert from DOC to RTF.

I know that, my dog knows that and I don't even own a dog.

The uncertainty is whether the layout and formatting is fully preserved in
RTF because RTF does not support all the pretty features that Word can
provide. Also if RTF can does the VB RTF box show them because it in turn
does not support everything it is possible to do with RTF.




Dave O.
 
R

Ralph

Dave O. said:
I know that, my dog knows that and I don't even own a dog.

The uncertainty is whether the layout and formatting is fully preserved in
RTF because RTF does not support all the pretty features that Word can
provide. Also if RTF can does the VB RTF box show them because it in turn
does not support everything it is possible to do with RTF.

I agree with you on the 'uncertainty'.

Strange things can happen once you start piddling about with .doc to .rtf
and back again. RTF standards, and how well the developers adhered to those
standards, vary between RTF 'controls', versions of WordPad, and versions of
Word.

It is useful to note that WordPad doesn't use .doc internally. It is pure
..rtf. So incoming .doc files are 'converted' to rtf for display. If you edit
a .doc file with WordPad and save out again as a .doc file - you have
effectively performed two conversions. (Although the WordPad team does takes
some shortcuts in order to be compliant with .doc standards.)

For anything other than simple formats, surprises can occur if users are
sharing documents and using both WordPad and Word on the same document. It
can become a digital game of jungle telegraph. <grin>

-ralph
 
E

Ed from AZ

I wonder if he could save the documents as RTF, if so he could show

Not a problem to iterate through all docs and SaveAs RTF. And
probably easier than trying to convert to EMF. I don't do a lot of
VB6 (mostly Word and Excel macros), so I wasn't even aware of a
separate RTFbox control!

There are no special fonts. Everything is standard system fonts. The
layouts do include tables just to keep lines separated and aligned.
Don't know if that would cause a problem in the RTF conversion.

Ed
 

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

Top