Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Word Interop: Unable to save text in a document

Word Interop: Unable to save text in a document

Scheduled Pinned Locked Moved C#
helpcomtestingtools
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mailtogj
    wrote on last edited by
    #1

    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

    L 1 Reply Last reply
    0
    • M mailtogj

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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![^]

      M 1 Reply Last reply
      0
      • L Lost User

        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![^]

        M Offline
        M Offline
        mailtogj
        wrote on last edited by
        #3

        object saveChanges = Word.WdSaveOptions.wdDoNotSaveChanges; This saves without the inserted text. SaveAs() also doesn't work for inserting text. Could i possibly catch the save as event for the word application.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups