SendKeys via .NET

M

Mike Loux

Does anybody know why there does not seem to be a .NET equivalent
(using the Primary Interop Assemblies) for SendKeys in Word? It's
very doable in Excel, but apparently not in Word (except via VBA).

I am maintaining a .NET app that accesses Word (update of a legacy
application, so I am locked into using that and not macros or add-ins,
unfortunately) via the (admittedly archaic) DSOFramer control, and I
am trying to programmatically close the Print Preview window if it is
open before attempting to save and close a document externally. If I
do not do this, then the application throws an exception ("Cannot
access document when in modal condition").

I am able to grab the application handle and determine if the
Print Preview window is open, but I cannot figure out how to close
it. If this were Excel, then I could just do a Application.SendKeys
("{Esc}") and all would be well, but I can't seem to do that in Word
(and it's frustrating as heck).

Anybody have ideas? Thanks!
 
M

macropod

Hi Mike,

Ideally, you should avoid Sendkeys. Apart from its unreliability as application versions change, it's not supported under Vista.
 
C

Cindy M.

Hi Mike,
Does anybody know why there does not seem to be a .NET equivalent
(using the Primary Interop Assemblies) for SendKeys in Word? It's
very doable in Excel, but apparently not in Word (except via VBA).
...
I am able to grab the application handle and determine if the
Print Preview window is open, but I cannot figure out how to close
it. If this were Excel, then I could just do a Application.SendKeys
("{Esc}") and all would be well, but I can't seem to do that in Word
(and it's frustrating as heck).

Why don't you use the SendKeys command that's part of the .NET
Framework? (Part of System.Windows.Forms namespace, as I recall -
certainly under "Windows", in any case).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
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

Manvir Singh

Hi Mike,

I am not sure about the SendKeys functionality from .Net perspective, however you can try modifying the DSOFramer sample's source as below, to achieve this:

In CDsoFramerControl::Save(....) method, you can add following code block under " if ((m_fModalState) || (m_fNoInteractive)) "

HRESULT hr = m_pDocObjFrame->ExitPrintPreview(TRUE);

if (FAILED(hr)) {
return ProvideErrorInfo(DSO_E_INMODALSTATE);
}

Basically, we are trying to forcefully exit Print Preview mode, before proceeding with the save functionality.

Also, please note that DSOFramer sample control is provided "AS IS" with no warranty or support from Microsoft. It is a demonstration, provided for informational purposes only.

Regards,
Manvir
 

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