none
Problem using Word.Document.SaveAs() RRS feed

  • Question

  • I have the following code segment:

     

     Word.Application wordApp = new Word.Application();

     Word.Document wordDoc = new Word.Document();

     wordApp.Visible = false;

     wordDoc = wordApp.Documents.Add(ref templatePath, ref miss, ref miss, ref miss);

     wordDoc.Activate();

     wordDoc.Bookmarks.get_Item(ref FileNumItem).Range.Text = fileNum;

     wordDoc.Bookmarks.get_Item(ref InsuredNameItem).Range.Text = mw.txtInsuredName.Text;

          //Save it using the SaveAs filename

      wordDoc.SaveAs(ref saveAs, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss,

                                 ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);

     

    This code works just fine on my development machine as well as 1 of the test machines.  However, I am getting a "Word did not save the document." error on 2 other test machines.  All machines are Windows XP SP3 with Office 2010 Standard.

    Both of the offending machines are also throwing "This file is read-only." on a Word.Document.ExportAsFixedFormat() method.

    All machines have read and write privileges on the required directory.

    • Moved by Larcolais Gong Friday, December 17, 2010 5:39 AM (From:Visual C# General)
    Wednesday, December 15, 2010 12:07 AM

Answers

  • Hi Neva

    <<Word.Document wordDoc = new Word.Document();>>

    Remove the above line from your code, completely, then test again.

    Office does not support creating "new" objects (with the exception of the Application object). Use only the ADD method, which will generate a new document.

    Yes, I know it's confusing, coming from the .NET way of thinking; the Office applications were designed with sealed classes, fifteen years ago.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Thursday, December 23, 2010 9:58 AM
    Friday, December 17, 2010 9:03 AM
    Moderator

All replies

  •  

    Hi Nevas92,

    I'm having a similar problem.  With the following code, I'll occasionally get "This file is read-only."  I'll run the same code just a little bit later, and it'll work fine.  I'm basically opening a word document (which is not open) and then exporting to PDF.  The PDF file does not exist on the filesystem prior to exporting (ie. I'm not over-writing).  Nothing else has the file in question open.

    If I find something.  I'll let you know.

     

    string paramExportFilePath = outputFilePath;
    
    object paramMissing = Type.Missing;
    
    WdExportFormat paramExportFormat = WdExportFormat.wdExportFormatPDF;
    bool paramOpenAfterExport = false;
    WdExportOptimizeFor paramExportOptimizeFor = WdExportOptimizeFor.wdExportOptimizeForPrint;
    WdExportRange paramExportRange = WdExportRange.wdExportAllDocument;
    int paramStartPage = 0;
    int paramEndPage = 0;
    WdExportItem paramExportItem = WdExportItem.wdExportDocumentContent;
    bool paramIncludeDocProps = true;
    bool paramKeepIRM = true;
    WdExportCreateBookmarks paramCreateBookmarks = WdExportCreateBookmarks.wdExportCreateWordBookmarks;
    bool paramDocStructureTags = true;
    bool paramBitmapMissingFonts = true;
    bool paramUseISO19005_1 = false;
    
    object paramWdUnits = WdUnits.wdStory;
    object paramWdMovementType = WdMovementType.wdMove;
    object paramWdBreakType = WdBreakType.wdPageBreak;
    
    		
    // Export it in the specified format.
    if (wordDocument != null)
    {
    	wordDocument.ExportAsFixedFormat(paramExportFilePath,
     		paramExportFormat, paramOpenAfterExport,
    paramExportOptimizeFor, paramExportRange, paramStartPage,
    paramEndPage, paramExportItem, paramIncludeDocProps,
    paramKeepIRM, paramCreateBookmarks, paramDocStructureTags,
    paramBitmapMissingFonts, paramUseISO19005_1,
    ref paramMissing);
    }
    

     


    Wednesday, December 15, 2010 4:06 PM
  • Hi Nevas92,

    I will help you moving your thread into Office forum.

    Best Regards,

    Larcolais


    Larcolais Gong[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 17, 2010 5:38 AM
  • Hello Nevas92,

    Thanks for posting. I read your post carefully, but I think your question is unclear for me so that currently I cannot give much helpful information on this. To provide further assistance, would you mind clarifying some points?

    • Are all these machines with Office 2010 installed? Is there any difference?
    • You are using SaveAs method and ExportAsFixedFormat. Are they all used to save a document as pdf file? Would you please sharing the code about ExportAsFixedFormat?
    • Does this issue happen every time? Or it only worked fine the first time?

    Also here are some resources about ExportAsFixedFormat method. Hope they could help you more or less.

    SaveAs PDF options:
    http://social.msdn.microsoft.com/forums/en-US/vsto/thread/45ee9c7a-4a06-47f2-b718-aad60f5ac57c/.
    COM Exception w/ ExportAsFixedFormat:
    http://social.msdn.microsoft.com/Forums/en/vsto/thread/08a938fb-6bae-4ef0-a04f-aed91d0c063f.

    We are looking forward to you reply. Have a nice day!


    Bessie Zhao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, December 17, 2010 8:31 AM
  • Hi Neva

    <<Word.Document wordDoc = new Word.Document();>>

    Remove the above line from your code, completely, then test again.

    Office does not support creating "new" objects (with the exception of the Application object). Use only the ADD method, which will generate a new document.

    Yes, I know it's confusing, coming from the .NET way of thinking; the Office applications were designed with sealed classes, fifteen years ago.


    Cindy Meister, VSTO/Word MVP
    • Marked as answer by Bessie Zhao Thursday, December 23, 2010 9:58 AM
    Friday, December 17, 2010 9:03 AM
    Moderator