Re: How to change focus from .net application back to Word

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



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



Relevant Pages

  • Truth or Consequences, NM - A lesson in terror
    ... David Ray gets 224 years -- Victims weep, ... movies with children in the central roles, ... percentage of my Being am I truly in control? ... I encouraged your spirit to contact me and let me ...
    (talk.religion.newage)
  • Truth or Consequences, NM - A lesson in terror
    ... David Ray gets 224 years -- Victims weep, ... movies with children in the central roles, ... percentage of my Being am I truly in control? ... I encouraged your spirit to contact me and let me ...
    (soc.women)
  • Re: Opinion wanted
    ... the details including the nature of my own app (which you mischaraterized ... correct the current control (provided that control is invalid but invalid ... causes you to validate controls that have already been ... largely discreditted for good reason and has very little use these days. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: Honestly Curious
    ... Mac apps do not need this. ... you don't have to run an app at all. ... You don't have to look for the right app to uninstall, ... full control over what is being removed from your HD. ...
    (comp.sys.mac.advocacy)
  • Re: Visual Basic for Autorun?
    ... If changes are required they can't be specific to one app. ... Obviously this doesn't apply to all usercontrols and should ... everything is in the one language. ... >I've done it fairly extensively and it works quite well as long as you keep good control over it. ...
    (microsoft.public.vb.general.discussion)