pKulandrei

Hi everyone,

I created a add-in for Word using vsto. I got a form that show to the user and when a buttoon on that for was clicked, the add will add a new document using this.Documents.Add(<params>). The new document was successfully created but it was not the active window. The first document when I opened word was still the active window. I'm using Document.Activate() to make the new document active but its not working. I'm using C#.

Button_Click()

{

frm.Hide()

Application.DoEvents();

CreateDoc();

}

void CreateDoc

{

docNew = this.Documents.Add(<params>);

docNew.Activate();

}

I hope you can help me with this.



Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

Ji Zhou – MSFT

Hi,

Are you using VSTO

Codes below work fine in my side, the new document is the activate one:

Code Snippet

private Word.Document newDoc = null;

private object missing = Type.Missing;

private void button1_Click(object sender, EventArgs e)

{

this.Hide();

Application.DoEvents();

CreateDoc();

}

private void CreateDoc()

{

newDoc = Globals.ThisAddIn.Application.Documents.Add(ref missing, ref missing, ref missing,ref missing);

newDoc.Activate();

}

Code Snippet

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

Form1 myForm = new Form1();

myForm.Show();

}

It seems that your code are not written in VSTO SE. How do you create the Word Add In project And where do you put the CreateDoc() function(Why this pointer can represent application object)

Wish you clarify, so we can help on this issue in future :-)

Thanks

Ji






Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

pKulandrei

Ji,

I'm using VSTO and I think it's VSTO SE since I downloaded the latest version. Just to make sure, how will I confirm that I got VSTO SE installed

All the functions and forms I used are all contained in a class library written in C#. Its a separate dll that is being use by the add-in. The add-in will just call the function that loads the form, and when a button on that for was clicked, it will use the library function that create the document and make it active. The CreateDoc() function was contained on the library.

I create the project using the templates that I downloaded from microsoft web sites. I create the new add by clicking on File --> New Project --> Visual C# --> Office --> 2007 Add-ins.





Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

Ji Zhou – MSFT

Hi,

I am still a little confused about your scenario. Did you write the dll or get it from somewhere It makes no sense that you can use this statement this.Documents.Add to interact with the Office application. Could you please post the exact codes in your class library :-)

Thanks

Ji






Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

Cindy Meister

pKulandrei wrote:

Ji,

I'm using VSTO and I think it's VSTO SE since I downloaded the latest version. Just to make sure, how will I confirm that I got VSTO SE installed

All the functions and forms I used are all contained in a class library written in C#. Its a separate dll that is being use by the add-in. The add-in will just call the function that loads the form, and when a button on that for was clicked, it will use the library function that create the document and make it active. The CreateDoc() function was contained on the library.

I create the project using the templates that I downloaded from microsoft web sites. I create the new add by clicking on File --> New Project --> Visual C# --> Office --> 2007 Add-ins.

What are you passing to this.Documents.Add as parameters This could make a difference. For example, if you're creating the new document with the Visible parameter set to false...






Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

pKulandrei

Yes, it is a separate dll that I wrote in C#. I'm passing a reference of the instance of Microsoft.Office.Interop.Word.Application from the add-in to the class library. I used a class libabry to re-use codes since most of our add-ins will do similar things.

Here is the code that adds the new document:

object objTemplate = @"c:\mytemplates\newdoctemp.dot";

docNew = refWordInst.Documents.Add(ref objTemplate, ref missing, ref missing, ref missing);





Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

Ji Zhou – MSFT

Hi pKulandrei,

I am trying to reproduce your issue with performing below steps, but it works for me. Newly Added Document is the Active Window. If any misunderstanding, please point it out without hesitation.

1. I Create a ClassLibrary project named ClassLibrary1, and expose a static method to Create document, codes are as follows:

Code Snippet

using System;

using System.Collections.Generic;

using System.Text;

using Office = Microsoft.Office.Core;

using Word = Microsoft.Office.Interop.Word;

namespace ClassLibrary1

{

public class Class1

{

private static object missing = Type.Missing;

public static void CreateDoc(Word.Application myApp)

{

object objTemplate = @"c:\test.dot";

Word.Document docNew = myApp.Documents.Add(ref objTemplate, ref missing, ref missing, ref missing);

docNew.Activate();

}

}

}

2. Build the ClassLibrary to a dll.

3. Create a Word add in project, and add the dll as a reference in this project.

4. Add a winform in the add in project, and then create a button on the winform. In the button click event handler function, call CreateDoc which we defined in Class1. All codes are as follows:

Code Snippet

using ClassLibrary1;

namespace WordAddIn2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

this.Hide();

Application.DoEvents();

Class1.CreateDoc(Globals.ThisAddIn.Application);

}

}

}

5. In ThisAddIn_Startup of the Add In project, create the Form1's instance and show the Form.

Code Snippet

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

Form1 frm = new Form1();

frm.Show();

}

6. When I press F5 to test the Add In, I see that with the Word starting, a form with a button shows up. I click the button, the program create a document from the template locates at C:\test.dot. And the new document is the active one.

I have test that in such machine environment: Windows Xp Sp2, VSTO SE, both Word2003 and Word 2007. If there's any difference between my codes and yours, please clarify for future researching.

Thanks

Ji






Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

pKulandrei

Thanks Ji for spending some time to reproduce the issue!

The form was also contained in the class library.

using System;

using System.Collections.Generic;

using System.Text;

using Office = Microsoft.Office.Core;

using Word = Microsoft.Office.Interop.Word;

namespace ClassLibrary1

{

public class Class1

{

private static object missing = Type.Missing;

private static Form1 gfrmForm1;

Word.Application gWordInst;

public static void InitLib(ref Word.Application wordInstFromAddin)

{

//other codes exist here

gWordInst = wordInstFromAddin;

gfrmForm1 = new Form1();

}

public static void CreateDoc()

{

object objTemplate = @"c:\test.dot";

Word.Document docNew = gWordInst.Documents.Add(ref objTemplate, ref missing, ref missing, ref missing);

docNew.Activate();

}

public static void ShowForm()

{

//other code here

gfrmForm1.ShowDialog();

}

//This function is being called when the CreateDoc button on Form1 is click

public static void CallCreateDoc()

{

//other codes here

gfrmForm1.Hide();

Application.DoEvents();

CreateDoc();

gfrmForm1.Close(); //still have the same result even if I removed this line

}

}

}

Code on the add-in.

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

Class1.InitLib(ref this); //Or this is something like Class1.InitLib(ref this.Application);

}

The add is creating a new button on the ribbon. When that button is click, it will call this function contained on the add-in.

private void ShowFormOnLib()

{

Class1.ShowForm();

}

I hope this will help reproduce the issue.

Thanks!





Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

Ji Zhou – MSFT

Hi again,

Sadly to say, no, it could not be achieved if you only want to use ShowDialog method. Because ShowDialog method makes the form to be a model one. The word application can not get the focus, so the Active function will be ignored. If you want the newly added document be the active one, you must use frm.Show() statement.

Thanks

Ji






Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

pKulandrei

oh, we want the form to be modal. Can I use frm.Show() to make the form modal If yes, does it have the same effect with ShowDialog How will I use frm.Show() to make the form modal

Thanks!





Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

Cindy Meister

Looking at your code, you're creating and activating the document when the user is finished with the form

Can you write any information the form contains for creating the new document to public variable, or a property, or an XML file (main concern: it should be persisted). Then close the form and have the procedure that called the form do the document creation, using that information. Since the modal form will be unloaded that should let the document display. Or, at least, have the calling procedure use the Activate method on the newly created document.

//This function is being called when the CreateDoc button on Form1 is click

public static void CallCreateDoc()

{

//other codes here

gfrmForm1.Hide();

Application.DoEvents();

CreateDoc();

gfrmForm1.Close(); //still have the same result even if I removed this line

}






Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

pKulandrei

Yes, I'm creating and activating the document after the user is finished with the form. I will try the approach your suggesting.

Thanks!





Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

Ji Zhou – MSFT

Hi again,

Try these codes which work in my side:

Class1.cs

Code Snippet

namespace ClassLibrary1

{

public class Class1

{

public static object missing = Type.Missing;

public static Form1 gfrmForm1;

public static Word.Application gWordInst;

public static bool flag = false;

public static void InitLib(ref Word.Application wordInstFromAddin)

{

gWordInst = wordInstFromAddin;

gfrmForm1 = new Form1();

}

public static void CreateDoc()

{

object objTemplate = @"c:\test.dot";

Word.Document docNew = gWordInst.Documents.Add(ref objTemplate, ref missing, ref missing, ref missing);

docNew.Activate();

}

public static void ShowForm()

{

gfrmForm1.ShowDialog();

}

}

}

Form1.cs in ClassLibrary1

Code Snippet

namespace ClassLibrary1

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void button1_Click(object sender, EventArgs e)

{

Class1.gfrmForm1.Close();

Class1.CreateDoc();

Class1.flag = true;

}

}

}

ThisAddIn.cs in WordAddIn

Code Snippet

namespace WordAddIn2

{

public partial class ThisAddIn

{

private void ThisAddIn_Startup(object sender, System.EventArgs e)

{

((Word.ApplicationEvents4_Event)this.Application).WindowActivate += new Microsoft.Office.Interop.Word.ApplicationEvents4_WindowActivateEventHandler(ThisAddIn_WindowActivate);

Class1.InitLib(ref this.Application);

}

void ThisAddIn_WindowActivate(Microsoft.Office.Interop.Word.Document Doc, Microsoft.Office.Interop.Word.Window Wn)

{

if (Class1.flag)

{

object n = 1;

this.Application.Documents.get_Item(ref n).Activate();

}

Class1.flag = false;

}

}

}

In Ribbon.cs

Code Snippet

public void ButtonClicked(Office.IRibbonControl control)

{

Class1.ShowForm();

}

Thanks

Ji






Re: Visual Studio Tools for Office Newly Added Document is NOT the Active Window

pKulandrei

It's now working. Thanks Ji and Cindy!!!