Search code examples
c#.netms-wordoffice-interopfilestream

File Stream issue file not opening


Here I'm writing parameters to a word file that is already created and saving it to a new file after creating that relevant file should open:

public void CreateWordDocument(object fileName,
                                    object saveAs, string nbt, string vat,string total)
    {
        Word._Application wApp = new Word.Application();
        //Set Missing Value parameter - used to represent
        // a missing value when calling methods through
        // interop.
        object missing = System.Reflection.Missing.Value;

        //Setup the Word.Application class.
        Word.Application wordApp = 
            new Word.ApplicationClass();

        //Setup our Word.Document class we'll use.
        Word.Document aDoc = null;

        // Check to see that file exists
        if (File.Exists((string)fileName))
        {
            DateTime today = DateTime.Now;

            object readOnly = false;
            object isVisible = false;
            //Set Word to be not visible.
            wordApp.Visible = false;
            //Open the word document
            aDoc = wordApp.Documents.Open(ref fileName, ref missing, 
                ref readOnly, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref isVisible, ref missing, ref missing, 
                ref missing, ref missing);
            // Activate the document
            aDoc.Activate();                
            // Find Place Holders and Replace them with Values.
            this.FindAndReplace(wordApp, "NBT",nbt);
            this.FindAndReplace(wordApp, "VAT", vat);
            this.FindAndReplace(wordApp, "TOTAL", total)                                                
        }
        else
        {
            MessageBox.Show("File dose not exist.");
            return;
        }
        //Save the document as the correct file name.
        aDoc.SaveAs(ref saveAs, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing, 
                ref missing, ref missing, ref missing, ref missing);
        //Close the document - you have to do this.
        aDoc.Close(ref missing, ref missing, ref missing);
        MessageBox.Show("File created.");
        wordApp.Visible = true;            
        //using (var filestresam = new FileStream(@"D:\Invoice\new.docx", FileMode.Open, FileAccess.Read));
        using (FileStream stream =File.Open(@"D:\Invoice\new.docx",FileMode.Open,FileAccess.Read,FileShare.Read));

    }
    private void FindAndReplace(Word.Application WordApp, 
                                object findText, 
                                object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object nmatchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        WordApp.Selection.Find.Execute(ref findText,
            ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike,
            ref nmatchAllWordForms, ref forward,
            ref wrap, ref format, ref replaceWithText,
            ref replace, ref matchKashida,
            ref matchDiacritics, ref matchAlefHamza, 
            ref matchControl);
    }

every this is working perfectly except for the

using (FileStream stream =File.Open(@"D:\Invoice\new.docx",FileMode.Open,FileAccess.Read,FileShare.Read));

the relevent file is not opening no errors shown also can't find the issue


Solution

  • You seem to expect that File.Open() actually opens the file with the program associated with the file's extension.

    That's not what that method does, see MSDN: File.Open Method:

    Opens a FileStream on the specified path.

    In fact you're trying to Open file with associated application:

    System.Diagnostics.Process.Start(@"D:\Invoice\new.docx");
    

    Do note that this will not work in an ASP.NET context, but only in applications that actually run on the client.