Reputation: 101
Before I begin, I have googled this to death and there are many posts about how to prevent the save prompt. I am having an issue showing the save prompt.
I am building the template editing portion of a document generation system in C#. The system will edit 'dot' and 'dotx' files. Before outlining the problem, the environment I am using for development is running Visual Studio 2010 and Word 2010. It will eventually run on other versions, but i would like to get these versions functional first.
To set the scene I have a form open that has a list of columns returned from a stored procedure(Data Source) to add to the document as bookmarks. I have all of the bookmark and drag/drop operations functional. When I close the application, I catch the 'ApplicationEvents4_DocumentBeforeCloseEventHandler' event to close the form.
When I close the form, i check haw many documents there are open. If only one document is open, I close the application which prompts the user to save changes. If however there are multiple documents open (Most people have a number of different word documents open concurrently), I locate the correct document and close it with the flag set to prompt the user to save changes.
This is where the problem occurs. At this point, the save changes dialog never shows up and everything freezes up in Visual Studio. If I stop the debugging on Visual Studio 2010 the document flashes in the task bar indefinitely, and if you focus on it, it disappears and saves changes without a prompt.
This is the code to handle the form closing event:
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
if (app != null)
{
if (app.Documents.Count < 2)
{
this.TopMost = false;
((Word._Application)app).Quit();
app = null;
}
else
{
foreach (Word.Document document in app.Documents)
{
if (document.FullName.Equals(wordDoc.FullName))
{
object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
((Word._Document)wordDoc).Close(ref saveChanges);
break;
}
}
}
}
}
The problem is this line should show a save changes dialog:
((Word._Document)wordDoc).Close(ref saveChanges);
I have tried debugging this without much luck. Putting a breakpoint on this line and on the
break;
line allows the program to stop at the 'Close' line but when you 'step' forward or 'continue' word becomes unresponsive, so does the form and the breakpoint on the very next line never gets hit.
Any help would be greatly appreciated as something this simple is so annoying to get stuck on.
Upvotes: 1
Views: 4785
Reputation: 65554
To avoid the Prompt or to get the Prompt to you have to set the Saved
property to true or false respectively:
var doco = WordApp.Documents.Add();
doco.Saved = true;
doco.Close(Microsoft.Office.Interop.Word.WdSaveOptions.wdDoNotSaveChanges, Type.Missing, Type.Missing);
Something fishy is going on if Word hangs on the line of code when you try to close the document. I recommend disposing of all the resources properly. Here is a great article on using VSTO Contrib that helps provide this functionality:
http://jake.ginnivan.net/vsto-com-interop
Update:
Enable your VSTO log file by adding the following on your system environment variables:
NAME: VSTO_LOGALERTS VALUE: 1 There might be an exception error that is why your add-in is not loading.
You can check this source for more info on VSTO logging and alerts, but in essence you change two environment variable values depending on what you need to do:
Displaying VSTO Alert Prompts
To display each error in a message box, set the VSTO_SUPPRESSDISPLAYALERTS variable to 0 (zero). You can suppress the messages by setting the variable to 1 (one).
Logging VSTO Alerts to a Log file
To write the errors to a log file, set the VSTO_LOGALERTS variable to 1 (one).
Visual Studio Tools for Office creates the log file in the folder that contains the application manifest. The default name is .manifest.log. To stop logging errors, set the variable to 0 (zero).
Upvotes: 3