none
MS Word Document.SaveAs error in vba

    Question

  • Hello all,

    Basically, when using vba to save a document via the Word Document.SaveAs method, the method causes a "Server threw an exception" error. The basic code that causes error:

    Dim app 
    
    Dim doc
    
    dim path
    
    
    
    path = "%USERPROFILE%\Documents\"
    
    Sett app = CreateObject(Word.Application)
    
    Set doc = app.Documents.Add()
    
    doc.SaveAs path & "New Document.docx"
    
    doc.Close
    
    app.Quit
    
     
    
    Set doc = Nothing
    
    Set app = Nothing 

    After much searching,  I found this solution (via the link that follows), but does nothing more than ignore the error and save again. Here is the code: (found near the bottom of the linked page)

    In XL VBA, Word Document SaveAs throws error - The Code Cage Forums

    Do
    
    On Error Resume Next
    
    objDoc.SaveAs strPath & ".doc"
    
    On Error GoTo 0
    
    strDoc = objDoc.Name
    
    Loop While strDoc = "Document1"
    
    

    This error only occurs on our work machines (Large Domain, many levels of group policy, same HDD image - Vista OS and Office 2007 Pro). I have not been able to reproduce on home machines (Vista home & XP & Office 2007).

    I have tried many things to fix this, i.e. rename Normal.dot template, verify that it is not file  permissions, uninstalled and reinstalled Office, used VBSript, etc. the only way I have found to work is the above - "Ignore Error - Save Again!" - paradigm.

    Although this solution works, I would never had been able to make the jump from - "Got an error, debug, find reason, fix it" to "Ahhh, just ignore the error, it will go away if we save again".  It just seems fundementally wrong to me.

    thanks and any thoughts?

    Some additonal information.

    Trying both late binding and early binding does not resolve the issue.

    I have also tried the following code:

    Dim app 
    Dim doc
    
    Set app = CreateObject("Word.Application") 
    app.Visible = True
    Set doc = app.Documents.Add()
    
    

    This opens a new document and leaves the Word Application running. From here if I were to save the file manually (file -> saveas), it saves the file withut error.

    Also, I noticed that if I open the "Word Options" Dialog (Office Button -> Word Options ) the Navigation bar only allows me to highlight the 'Popular' tab, none of the other tabs ('Display', 'Poofing', etc) will not higlight when rolled over and do not function when clicked.

    Not sure if this idicates anything in particular.

    Again, thanks for ant ideas.

    • Edited by RMInbox Thursday, May 13, 2010 6:37 AM Added Additonal Details
    Friday, May 07, 2010 8:09 AM

All replies

  • Hello,

    Have you tried Office 2007 SP2? I also could not repro this issue, seems like it someting wrong with the envrionment. So please try Office SP2 first, if you haven't got it installed.

    Thanks.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Monday, May 10, 2010 9:29 AM
  • Try :

    Dim app
    Dim doc
    Dim path
    
    'path = "%USERPROFILE%\Documents\"
    path = "c:" & Environ("HOMEPATH") & "\Documents\"
    'Sett app = CreateObject(Word.Application)
    Set app = CreateObject("Word.Application")
    Set doc = app.Documents.Add()
    doc.SaveAs path & "New Document.docx"
    doc.Close
    app.Quit
     
    Set doc = Nothing
    Set app = Nothing
    

    HTH

    Daniel

    Monday, May 10, 2010 10:20 AM
  • Alternately, you may use :

    path = Environ$("USERPROFILE") & "\Documents\"

    Daniel

    Monday, May 10, 2010 10:30 AM
  • Tim Li,

    Thanks for the response. We are running Office 2007 Pro SP 2 on Vista Enterprise SP 1. I thnk that it is  an environment issue, because when I tried to update to Vista SP 2, it failed to install. I will continue to try to find out what exactly is the issue though.

    Thursday, May 13, 2010 5:44 AM
  • Hello Daniel,

    The code I poseted was actully VB Script, that is why I used %USERPROFILE% for the file path. Your suggestion does provide the correct path when used in VBA. Unfortunately, the 'SaveAs' method is still generating the following error:

    Run-time error '-2147417851 (80010105)'
    Method 'SaveAs' object '_Document' failed

    Any other suggestions woud be welcome, thanks!

    Thursday, May 13, 2010 5:49 AM
  • Hello,

    Maybe, it should be "MyDocuments", instead of "Documents" ?

    HTH

    Daniel

    Thursday, May 13, 2010 8:50 AM
  • I'm getting the same issue with a script I am writing. I am trying to convert a batch file that ran ruby to apply macros to all DOCs in a folder so that it will work with Word 2007. i ahve written a vbscript that does everything i need except when pulling up the documents. It will pull up a document, apply a macro and then it bombs when trying to save. This is where i get the "Server threw an exception" error. Any more ideas or maybe a script that will do what i am asking.
    Tuesday, May 18, 2010 2:57 PM