locked
Problem when saving a Word Document in C# RRS feed

  • Question

  • I have the following problem:

    I open a Word-document programmatically with a template for the user to edit and wait for the user to save the document. When the user saves the document; I interfere with it; to save it at a different location. But, when the user opens an other (new or existing) Word-document and saves this before he's finished with 'my' created document; I interfere with the saving of that other document.

    I only want to interfere with the document I programmatically opened; I don't care if the users opens 10 other documents besides 'my' document.

    I'm using the "Microsoft.Office.Interop.Word" version 14 (Office 2010) and I have the following code:

    using Word = Microsoft.Office.Interop.Word;
    
    private Word.Application _wordApp;
    private Word.Document _wordDoc;
    
    _wordApp = new Word.Application();
    _wordApp.Visible = true;
    _wordApp.DocumentBeforeSave += _wordApp_DocumentBeforeSave;
    
    _wordDoc = _wordApp.Documents.Add(Template: TemplatePath);

    private void _wordApp_DocumentBeforeSave(Word.Document wordDoc, ref bool saveAsUI, ref bool cancel)
    {
        saveAsUI = false;
        cancel = true;
    
        wordDoc.SaveAs(FileName: NewFileName);
    }

    When saving; the wordDoc provided in the "DocumentBeforeSave" doesn't equals the _wordDoc of my own.

    Tuesday, June 12, 2012 6:27 AM

Answers

  • Hi Fermin Wenlock,

    I do think this is possible; the path is empty when creating a new document; and the document itself will be "Document1", "Document2", etc.

    So, it is possible to do the following:

    if(_wordDoc.Path + _wordDoc.Name == wordDoc.Path + wordDoc.Name)
    {
        // Execute code
    }

    In the past few days, I've been trying a couple of things and I found "CurrentRsid"; thus the following would also be possible:

    if (_wordDoc.CurrentRsid == wordDoc.CurrentRsid)
    {
        // Execute code
    }
    Someone who can confirm (or deny) that the given solutions won't give unexpected problems?
    • Proposed as answer by Quist Zhang Thursday, June 21, 2012 5:35 AM
    • Marked as answer by Quist Zhang Thursday, June 21, 2012 6:02 AM
    Thursday, June 14, 2012 9:34 AM

All replies

  • Hi Duracell1989,

    Can you make it by judging from _wordDoc's path and name?

    Best regards,
    Fermin

    Thursday, June 14, 2012 8:04 AM
  • Hi Fermin Wenlock,

    I do think this is possible; the path is empty when creating a new document; and the document itself will be "Document1", "Document2", etc.

    So, it is possible to do the following:

    if(_wordDoc.Path + _wordDoc.Name == wordDoc.Path + wordDoc.Name)
    {
        // Execute code
    }

    In the past few days, I've been trying a couple of things and I found "CurrentRsid"; thus the following would also be possible:

    if (_wordDoc.CurrentRsid == wordDoc.CurrentRsid)
    {
        // Execute code
    }
    Someone who can confirm (or deny) that the given solutions won't give unexpected problems?
    • Proposed as answer by Quist Zhang Thursday, June 21, 2012 5:35 AM
    • Marked as answer by Quist Zhang Thursday, June 21, 2012 6:02 AM
    Thursday, June 14, 2012 9:34 AM
  • Hi Duracell1989,

    Thank you for posting in the MSDN Forum.

    According to your description, I suggest you use the former solution.

    Regards,
    Quist


    Quist Zhang [MSFT]
    MSDN Community Support | Feedback to us

    Thursday, June 21, 2012 5:35 AM