Word Interop: Unable to save text in a document
-
Hi Got stuck here. Word document gets saved in the correct directory with the correct name but the typed text doesn't persist. Please help. (*I have tested that the text is actually typed in the active document). private void SaveDoc() { Word.Application wdApp; Word.Document wdDoc; wdApp = new Word.Application(); wdDoc = wdApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); //type some text wdApp.Selection.TypeText("Some Heading")); //set rtf doc paragraph 1 style to normal object wdParaStyleName = "Heading 1" wdDoc.Paragraphs[1].set_Style(ref wdParaStyleName); //save rtf doc wdApp.ChangeFileOpenDirectory("C:\\test\\"); object wdFileName = "TestDoc.rtf" object saveFmt = Word.WdSaveFormat.wdFormatRTF; wdApp.ActiveDocument.SaveAs(ref wdFileName, ref saveFmt, ref falseO, ref missing, ref falseO, ref missing, ref trueO, ref falseO, ref falseO, ref trueO, ref falseO, ref missing, ref falseO, ref falseO, ref missing, ref falseO); //Quit Word object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; wdApp.Quit(ref saveChanges, ref missing, ref missing); } Using doc.save() prompts the save dialog which defeats the purpose of automation. Thanks GJ
-
Hi Got stuck here. Word document gets saved in the correct directory with the correct name but the typed text doesn't persist. Please help. (*I have tested that the text is actually typed in the active document). private void SaveDoc() { Word.Application wdApp; Word.Document wdDoc; wdApp = new Word.Application(); wdDoc = wdApp.Documents.Add(ref missing, ref missing, ref missing, ref missing); //type some text wdApp.Selection.TypeText("Some Heading")); //set rtf doc paragraph 1 style to normal object wdParaStyleName = "Heading 1" wdDoc.Paragraphs[1].set_Style(ref wdParaStyleName); //save rtf doc wdApp.ChangeFileOpenDirectory("C:\\test\\"); object wdFileName = "TestDoc.rtf" object saveFmt = Word.WdSaveFormat.wdFormatRTF; wdApp.ActiveDocument.SaveAs(ref wdFileName, ref saveFmt, ref falseO, ref missing, ref falseO, ref missing, ref trueO, ref falseO, ref falseO, ref trueO, ref falseO, ref missing, ref falseO, ref falseO, ref missing, ref falseO); //Quit Word object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; wdApp.Quit(ref saveChanges, ref missing, ref missing); } Using doc.save() prompts the save dialog which defeats the purpose of automation. Thanks GJ
mailtogj wrote:
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
Documentation: This example includes the wdDoNotSaveChanges argument of the Close method to close without saving changes or prompting the user. Try using the SaveAs() method.
Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]
-
mailtogj wrote:
object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges;
Documentation: This example includes the wdDoNotSaveChanges argument of the Close method to close without saving changes or prompting the user. Try using the SaveAs() method.
Check out the CodeProject forum Guidelines[^] The original soapbox 1.0 is back![^]