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
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