Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to change focus from .net application back to Word

40 views
Skip to first unread message

John Murray

unread,
Jan 21, 2006, 10:51:18 AM1/21/06
to

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


wilsond

unread,
Jan 23, 2006, 7:07:02 PM1/23/06
to
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

John Murray

unread,
Jan 24, 2006, 1:08:03 PM1/24/06
to
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

"wilsond" <wil...@discussions.microsoft.com> wrote in message
news:DE811191-9A19-4860...@microsoft.com...

wilsond

unread,
Jan 24, 2006, 3:36:04 PM1/24/06
to
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

John Murray

unread,
Jan 30, 2006, 10:54:08 AM1/30/06
to
Thank you, David. This is exactly what I needed.

John


0 new messages