I am trying to opening Word file in asp.net application. I Try Following Code For That

private Word.ApplicationClass WordApp = new Word.ApplicationClass();
    protected void Button1_Click(object sender, EventArgs e)
    {
        Installation.doc";
        object fileName = Server.MapPath("/WordSample/WordFiles/Test.doc");
        object readOnly = false;
        object isVisible = true;
        
        object missing = System.Reflection.Missing.Value;
        
        WordApp.Visible = true;
        
        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);
        
        aDoc.Activate();
        
        WordApp.Selection.TypeText("Copyright C# Corner");
        WordApp.Selection.TypeParagraph();
    }

All this works fine in View In Browser, But when I publish that Page The file

not opening and on Click of Button, Browser running continuosly, but cant find file. Is this error with path or else?

Recommended Answers

All 5 Replies

change this line

object fileName = Server.MapPath("/WordSample/WordFiles/Test.doc");

to

object fileName = Server.MapPath("~") & "WordSample\WordFiles\Test.doc";

I try that, but File Not Found error comes......

TRY WITH BELOW LINE:

object fileName = Server.MapPath("WordFiles") + "\\" + "Test.doc";

I tried this but unfortunately, this one also dont work

TRY TO WRITE THE VALUE OF Server.MapPath("WordFiles") then check with path then print whole file path then check with server.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.