none
Word.Documents.Save() and .SaveAs() both crash Word RRS feed

  • Question

  • I'm not sure if this is the correct forum to ask this because I'm not using VSTO, I'm using the PIA for Word to interface with my program.

    Anyway, the purpose of the program is to open up a template, insert data previously entered by users, insert a table and a picture, and save the document. Everything works fine except when I try to save the document. It always throws this COMException:
    System.Runtime.InteropServices.COMException occurred
    
      ErrorCode=-2147417851
    
      Message="The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT))"
    
      Source="Microsoft.Office.Interop.Word"
    
      StackTrace:
    
           at Microsoft.Office.Interop.Word.DocumentClass.Save()
    
           at Project_Automation.Form1.cmdFinishDocument_Click(Object sender, EventArgs e) in ...Project Automation\Form1.vb:line 16201
    
    
    The same exception is thrown with .Save() and .SaveAs()


    If I comment the saves out and run the program, it works fine until the end. However, after the document is finalized, the VB solution exits, leaving Word and the document open. When I try to go to Word and save the document there, it causes Word to crash. I have been trying to find the specific error that is causing Word to crash even after my program has completed but I have not yet been able to.

    So basically, I am unable to save the document my program is creating.

    Any ideas? Thank you in advance
    Thursday, February 25, 2010 8:10 PM

All replies

  • Hello James,

    Would you please share the code with us? How you create a new document? by using Documents.Add()? As far as I see, in general, we use Documents.Add to open a new document, and then use SaveAs/Save method to save this document. For Documents.Add method and SaveAs method, please refer to these MSDN pages: http://msdn.microsoft.com/en-us/library/bb237079.aspx and http://msdn.microsoft.com/en-us/library/bb221597.aspx

    Actually, this forum is not the best place for this issue. For Word Automation issue, the best place would be Invate On Office forum or Word newsgroup. For this word newsgroup, you will find an appropriate entry link in this forum's PLEASE READ FIRST.

    Best regards,
    Bessie
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, March 1, 2010 10:19 AM
  • I think I didn't explain correctly. What I am doing in the program is copying a template document I created and then opening that file for Word automation. So currently I am using Word.Documents.Open(strFileName) then making all of the changes and then trying to save the file.

    I tried changing to Word.Documents.Add() but then I got an error once I start making the changes to the document saying I have a bad file name.

    System.Runtime.InteropServices.COMException occurred
      ErrorCode=-2146824128
      HelpLink="C:\Program Files\Microsoft Office\Office12\1033\WDMAIN11.CHM#36928"
      HResult=-2146824128
      IsTransient=False
      Message="Bad file name"
      Source="Microsoft Word"
      StackTrace:
           at Microsoft.Office.Interop.Word.Documents.get_Item(Object& Index)
      InnerException: 
    I am new to manipulating Word through VB so I apologize if I'm missing something basic here.

    This is a small section of the code where I open Word and open the file:

                    gmsWord = CreateObject("Word.Application")
                    gmsWord.Visible = True
                    gmsWord.Documents.Open(strFileName)
    Then I am doing all of the editing inside a With block.

    EDIT: I removed the With block because it had no real purpose. I also tried using .Add after removing the With block and I no longer get the "Bad File Name" exception but I still am unable to save the document. I get the same exception as before.
    • Edited by James.Greene Tuesday, March 2, 2010 3:51 PM New info
    Tuesday, March 2, 2010 2:48 PM
  • Hello again James,

    <<What I am doing in the program is copying a template document I created and then opening that file for Word automation.>>
    If you want to create a new document/template based on a template created before, we could use Documents.Add method to open a new document/template. Please notice that the second parameter is a default one/true. At the end of code block, we need to release these com objects. Code like this,
                
                Dim wordApp As New Word.Application()
                wordApp.Visible = True            
                Dim fileToOpen As Object = "C:\Temp\1.dotx"
                Dim missing As Object = Type.Missing
                Dim newTemplate As Object = False  'if want to open a new template, please set this to True;
                Dim doc1 As Word.Document = wordApp.Documents.Add(fileToOpen, newTemplate, missing, missing)

                Dim filename As Object = "C:\Temp\ABC.docx"  'if want to open a new template, please set the file name to ABC.dotx;
                doc1.Content.Text = "ABC"
                doc1.SaveAs(filename)

                doc1.Close(missing, missing, missing)
                Marshal.ReleaseComObject(doc1)
                wordApp.Quit(missing, missing, missing)
                Marshal.FinalReleaseComObject(wordApp)
      

    Please test in your side. If you have any concern for this, please feel free to follow up.

    Best regards,
    Bessie


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Proposed as answer by Nedash Thursday, April 19, 2012 11:37 PM
    Wednesday, March 3, 2010 7:50 AM
  • Thank you for your reply Bessie.

    However, I tested out your code and I still get the same exception on the save line. I am left to think that it might have something to do with my user permissions on my computer. I have tested it with another user who has the same permissions and the same error occurs. My next step will be to try and get admin rights then test again to see what happens.

    Could it also have something to do with the Word settings?
    Wednesday, March 3, 2010 4:23 PM
  • Hello James,

    Would you confirm one thing for us? Do you release these com objects finally? To release com objects, please refer to this thread: http://social.msdn.microsoft.com/forums/en-US/vsto/thread/a12add6b-99ea-4677-8245-cd667101683e/. When testing, please also check the count of WINWORD.EXE process in Task manager. Here are some threads for the same error which may interest you.

    Excel interop The server threw an exception. (Exception from HRESULT: 0x80010105 (RPC_E_SERVERFAULT)):
    http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/b0ac73a4-7a54-4d10-a1dd-54404ff1678e/.

    System.Runtime.InteropServices.COMException (0x80010105): The server threw an exception. (Exception from HRESULT: 0x80010105: http://social.msdn.microsoft.com/Forums/en-US/vsto/thread/a120240f-0772-4f46-9fe1-5ced12d364cf/.

    As far as I know, it is little related to Rights. However, you could test it in your side.

    If you have any concern/further information for this, please feel free to follow up.

    Best regards,
    Bessie

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, March 4, 2010 5:38 AM
  • Hello James,

    Why don't you use oDoc.Activate() before you call oDoc.Save() method?

    Best regards,
    Maa
    Thursday, March 4, 2010 8:40 AM
  • Thanks for the reply Maa,

    However, that still does not work. Same COMException occurs.

    James
    Thursday, March 4, 2010 4:28 PM
  • Hello again James,

    Which version of Word you are working with? Have you installed any SP? In my side, I am working with Word SP2. I think you could try to reinstall Office 2007 PIA in your side. You also could make a test project which just open an existing word document, make a change, and save it. If reinstalling Office PIA does not help you, I am afraid you need to reinstall Office 2007.

    Best regards,
    Bessie

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, March 5, 2010 5:15 AM
  • I'm using Word 2007 SP2 as well. I would like to try reinstalling the Office PIA. Can you link me to some instruction on doing that? I am not the person who originally installed it so I'm not sure of the process.

    Thanks again!
    Friday, March 5, 2010 2:32 PM
  • It is a profile issue. Try this, it worked for me on Windows 7 64bit machine!

    -  Create a  new "Desktop" directory inside of "C:\Windows\SysWOW64\config\systemprofile\"


    Ned


    • Edited by Nedash Friday, April 20, 2012 6:51 PM
    Thursday, April 19, 2012 11:42 PM