c# - Microsoft.Office.Interop.Word.Document won't prompt to save changes to single document without _Application.Quit() -
before begin, have googled death , there many posts how prevent save prompt. having issue showing save prompt.
i building template editing portion of document generation system in c#. system edit 'dot' , 'dotx' files. before outlining problem, environment using development running visual studio 2010 , word 2010. run on other versions, these versions functional first.
to set scene have form open has list of columns returned stored procedure(data source) add document bookmarks. have of bookmark , drag/drop operations functional. when close application, catch 'applicationevents4_documentbeforecloseeventhandler' event close form.
when close form, check haw many documents there open. if 1 document open, close application prompts user save changes. if there multiple documents open (most people have number of different word documents open concurrently), locate correct document , close flag set prompt user save changes.
this problem occurs. @ point, save changes dialog never shows , freezes in visual studio. if stop debugging on visual studio 2010 document flashes in task bar indefinitely, , if focus on it, disappears , saves changes without prompt.
this code handle 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 line should show save changes dialog:
((word._document)worddoc).close(ref savechanges);
i have tried debugging without luck. putting breakpoint on line , on
break;
line allows program stop @ 'close' line when 'step' forward or 'continue' word becomes unresponsive, form , breakpoint on next line never gets hit.
any appreciated simple annoying stuck on.
to avoid prompt or prompt have set saved
property 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 going on if word hangs on line of code when try close document. recommend disposing of resources properly. here great article on using vsto contrib helps provide functionality:
http://jake.ginnivan.net/vsto-com-interop
update:
enable vsto log file adding following on system environment variables:
name: vsto_logalerts value: 1 there might exception error why add-in not loading.
you can check source more info on vsto logging , alerts, in essence change 2 environment variable values depending on need do:
displaying vsto alert prompts
to display each error in message box, set vsto_suppressdisplayalerts variable 0 (zero). can suppress messages setting variable 1 (one).
logging vsto alerts log file
to write errors log file, set vsto_logalerts variable 1 (one).
visual studio tools office creates log file in folder contains application manifest. default name .manifest.log. stop logging errors, set variable 0 (zero).
Comments
Post a Comment