John Simons

I'm using Microsoft Visual Studio Tools for Office, and I have the following problem:

If I create a new Word Template project (Visual C# | Office | Word Template) and then press F5 to start it up, it all works and MSWord2003 starts up as expected. But then if I right click on any existing Word doc file (no VSTO) and select Print from the context menu, my VSTO Template closes unexpectedly.

I wonder if this only occurs to me or is indeed a bug.
Can someone else try and report the result

I've also tried to put a break point on BeforeClose event of the Document and it doesn't get fired!!


Regards
John





Re: Visual Studio Tools for Office Printing Word docs closes my VSTO template

Cindy Meister

hi John

Let me see if I understand the scenario correctly:

You have a Word template solution open in the VSTO designer. You press F5 (so you're in Debug.mode) and a new document based on the template is created in a Word window. While this is the case, you right-click a "plain vanilla" Word document in Windows Explorer and choose "Print". The VSTO file closes, but Word remains open and the document prints

If I perform the above actions (Windows XP SP2), the VSTO document remains open.

Do you get any kind of error messages Have you enabled "Break when exceptions cross AppDomain or managed/native boundaries" in Tools/Options/Debugging/General to make sure you're picking up all errors Word/Office may be generating

Does this happen with any and all VSTO template projects on your machine, or just this particular one

If "any and all", try renaming Normal.dot to NormalOLD.dot. The start the Word application (not as a VSTO designer). This will generate a "clean" copy of Normal.dot. Now create a new VSTO template project (that will base on this new Normal.dot) and see if the problem persists






Re: Visual Studio Tools for Office Printing Word docs closes my VSTO template

John Simons

Hi Cindy,

Thanks for getting back to me on this. See my answers below.


Cindy Meister wrote:

hi John

Let me see if I understand the scenario correctly:

You have a Word template solution open in the VSTO designer. You press F5 (so you're in Debug.mode) and a new document based on the template is created in a Word window. While this is the case, you right-click a "plain vanilla" Word document in Windows Explorer and choose "Print". The VSTO file closes, but Word remains open and the document prints


That is exactly what I'm doing.
I have a bit more info, The VS is still in debug mode, to stop it I can either close the Word Application or press the stop button in VS.


Cindy Meister wrote:

If I perform the above actions (Windows XP SP2), the VSTO document remains open.

Do you get any kind of error messages Have you enabled "Break when exceptions cross AppDomain or managed/native boundaries" in Tools/Options/Debugging/General to make sure you're picking up all errors Word/Office may be generating


No, I don't get any error messages even after I enabled "Break when exceptions cross AppDomain or managed/native boundaries".


Cindy Meister wrote:

Does this happen with any and all VSTO template projects on your machine, or just this particular one


I've just tried an Excel VSTO Template and it works.
But for Word VSTO Templates it doesn't (any Word VSTO template).


Cindy Meister wrote:

If "any and all", try renaming Normal.dot to NormalOLD.dot. The start the Word application (not as a VSTO designer). This will generate a "clean" copy of Normal.dot. Now create a new VSTO template project (that will base on this new Normal.dot) and see if the problem persists



Tried that, it didn't fix the problem .

Any other ideas Cindy or anyone




Re: Visual Studio Tools for Office Printing Word docs closes my VSTO template

Cindy Meister

Hi John

<<I've just tried an Excel VSTO Template and it works.
But for Word VSTO Templates it doesn't (any Word VSTO template).

Cindy Meister wrote:

If "any and all", try renaming Normal.dot to NormalOLD.dot. The start the Word application (not as a VSTO designer). This will generate a "clean" copy of Normal.dot. Now create a new VSTO template project (that will base on this new Normal.dot) and see if the problem persists

Tried that, it didn't fix the problem .>>

Would have been too easy... <sigh> The usual trouble-shooting step - to start Word in "Safe Mode" won't work when debugging VSTO. Going on the theory that the problem is in Word, the first thing to try would be (from within Word, no VS involved) to use the Help/Detect and Repair feature. This would find and replace any files in the Word installation that may be damaged.

The next thing to try is to remove (you can back it up, first) the Word/Data key in the Registry, under HKCU/Software/Microsoft/Office/[version number]. This contains most of the customizations ("options", toolbars) and is notoriously prone to corruption.

The other thing - and this may be harder to track down - could be third-party software interfering. This could be an Add-in, anti-virus software or a doc management system. You might try booting Windows in Safe Mode and see if that has any effect.






Re: Visual Studio Tools for Office Printing Word docs closes my VSTO template

John Simons

Hi Cindy,

I'm sorry for not replying for so long.
Thanks very much for trying to help me fix this problem.

I finally worked out what the problem is

In my VSTO word template I have the following code in the startup event:
private void ThisDocument_Startup(object sender, System.EventArgs e)
{
this.Saved = true;
}

And because of this Word reuses the window!!!
Do you know if there is a way to stop word from re-using my word template window even if the Saved property is set to true

btw, you should now be able to replicate this problem by following the instructions on the first post + add the above code to the startup event.




Re: Visual Studio Tools for Office Printing Word docs closes my VSTO template

Cindy Meister

Hi John

Bingo! Yes, I see it. OK, this looks like VSTO is duplicating what happens when Word starts up with a new, default document. In that case, opening a document will close that document, as long as you haven't done any editing.

I was seeing the problem because I was suppressing the changes like this:

Word.Template tmpl = (Word.Template) this.InnerObject.get_AttachedTemplate();
tmpl.Saved = true;

If you work with "this" you're not actually working with the document or template object, but a VSTO host object. In the case of a VSTO template, "this" turns out to be the VSTO host object for the document, itself, and not your template. So you're telling Word that the new document VSTO just created in the new Word instance has no changes. And thus Word feels free to close that document when you open a different one (for whatever reason).






Re: Visual Studio Tools for Office Printing Word docs closes my VSTO template

John Simons

Hi,

Me again! Just to let you know that I've fixed my problem.
The whole thing was happening because I'm using Word just as a front end to enter some data and then I actually save/load that data via web services, and therefore I was not committing any physical files to disk. And I think this is where the problem is or was!
The way I fixed it was by calling the SaveAs method in the Startup event and save a .doc file to a temp location so that MSWord does not use my window even if I programmatically set Saved = true.

Cheers
John