Re: How to change focus from .net application back to Word
- From: "wilsond" <wilsond@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Tue, 24 Jan 2006 12:36:04 -0800
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" wrote:
> 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" <wilsond@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:DE811191-9A19-4860-82B8-693C5F8BA77F@xxxxxxxxxxxxxxxx
> >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
> >
>
>
>
.
- Follow-Ups:
- Re: How to change focus from .net application back to Word
- From: John Murray
- Re: How to change focus from .net application back to Word
- References:
- How to change focus from .net application back to Word
- From: John Murray
- Re: How to change focus from .net application back to Word
- From: John Murray
- How to change focus from .net application back to Word
- Prev by Date: Re: How to change focus from .net application back to Word
- Next by Date: Querying Ranges
- Previous by thread: Re: How to change focus from .net application back to Word
- Next by thread: Re: How to change focus from .net application back to Word
- Index(es):
Relevant Pages
|