How to change focus from .net application back to Word

J

John Murray

How do I change the focus from the current active application to a Word
document that is open?

I am writing a .net form to automate MS Word. I have written a .net form
which has a button on it. Once the user clicks the button, the form locates
an active Word application and inserts a string of text into the active word
document at the current insertion point. However, after the insertion, the
focus remains on the .net form application. What code can I execute from
the .net form to cause the Word document to become the active window?

Here is my code for handling the click event. After running this code, the
form application is the active window.

private void insert1_Click(object sender, EventArgs e)
{

//Get reference to Word.Application from the ROT.
Word.Application wordApp =
(Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

wordApp.Visible = true;
wordApp.ActiveDocument.Activate();
wordApp.Selection.TypeText(richTextBox1.Text);

//Release the reference.
wordApp = null;
}


I was under the impression that the call to ActiveDocument.Activate() would
bring word to the front and make it the active window, however, this does
not happen. I have tried this is VStudio 2005 in debug mode and in release
mode to no avail.

Thank you for any hints. Why can't I get the form application to give up
the focus?


John
 
W

wilsond

I developed a Windows User Control that automates some functionality of Word.
I had the same issue that you are experiencing (the app had the focus and not
Word). I don't know if this is the best way to do it or not but, I put code
in the "activated" event of the form.

private void Form1_Activated(object sender, System.EventArgs e)
{
if( myWord.Instance != null )
this.myWord.Focus();
}

David
 
J

John Murray

David,

Thank you for your reply. I am truly ignorant. Anytime a google for
Focus() and C#, I get code that deals with controls. Forgive my plebian
question. What code do you run in your myWord.Focus() function. Focus() is
not a function of the word application, right?

Thanks,

John
 
W

wilsond

John,

My apologies to you. I'm not getting my instance of Word the same way you
are. Your code expects Word to be running already whereas my code early binds
Word and actually starts it. Focus() will not work.

But, you are instantiating the Word app differently than I am in my app. I
have a control built that I dropped on my form (just like a textbox or
button). The way you are getting your instance of Word, it doesn't "belong"
to the form. So there is no Focus(). You will probably have to do something
with the Win32 API to get that window to pop.

For instance:

private void insert1_Click(object sender, EventArgs e)
{

//Get reference to Word.Application from the ROT.
Word.Application wordApp =
(Word.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Word.Application");

wordApp.Visible = true;
wordApp.ActiveDocument.Activate();
wordApp.Selection.TypeText(richTextBox1.Text);

//Release the reference.

int wordWindow = 0;

Win32.FindWindow( "Opusapp", null );

if( wordWindow != 0 )
{
Win32.SetFocus( (System.Intptr)wordWindow );
Win32.ShowWindow( (System.Intptr)wordWindow, SHOW_FULLSCREEN
);
}

wordApp = null;
}

If you're not familiar with using the Win32 API from within C#, insert this
code in your class declaration:

[DllImport("user32.dll")]
public static extern int FindWindow( string strclassName, string
strWindowName );

[DllImport("user32.dll")]
public static extern int SetFocus( System.Intptr );

[DllImport("user32.dll")]
public static extern int ShowWindow( System.Intptr, int );


Then you can call those functions and get Word to pop up front.

David
 

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