locked
opening worddoc 2007 inside Windows Application./c# RRS feed

  • Question

  • Hi all ,

    I need to open word document inside the webbrowser.  its is acheived when i open word2003 but not word2007.i got some links saying ActiveXwebbrowser  will solve this problem. i tried that also but  its opening as a newwindows explorer not inside the ActiveXwebbrowser.

    am working in windows application using c#.net.

    need help.

    Thanks

    Dev

     


    • Edited by 2011deva Wednesday, December 21, 2011 10:46 AM
    Wednesday, December 21, 2011 10:42 AM

Answers

  • Hi,

    Try the following code. Before that add a reference to Microsoft.Office.Interop.Word in your project

     

    {

     Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

                object nullobj = System.Reflection.Missing.Value;

                object file = @"D:\Codes\exportingfromexc.docx";

                Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(

                        ref file, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj, ref nullobj);

                doc.ActiveWindow.Selection.WholeStory();

                doc.ActiveWindow.Selection.Copy();

                IDataObject data = Clipboard.GetDataObject();

                string text = data.GetData(DataFormats.Html).ToString();

               ((Microsoft.Office.Interop.Word._Document)doc).Close(ref nullobj, ref nullobj, ref nullobj);

                ((Microsoft.Office.Interop.Word._Application)app).Quit(ref nullobj, ref nullobj, ref nullobj);

                webBrowser1.DocumentText = "<html><body>" + text + "</html></body>";

    }

     

    • Marked as answer by Lie You Tuesday, December 27, 2011 2:47 AM
    Thursday, December 22, 2011 4:35 AM

All replies

  • Hii please follow the code to read doc file and show on browser

     

    string strPath ="Test.doc";
    FileStream fStream = new FileStream
    (strPath, FileMode.Open, FileAccess.Read);
    StreamReader sReader = new StreamReader(fStream);
    var str = sReader.ReadToEnd();
    sReader.Close();
    Response.Write(str);

    By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by san Sanz Wednesday, December 21, 2011 12:16 PM
    Wednesday, December 21, 2011 11:17 AM
  • Hi,

     

    Please go through the following link.

    http://support.microsoft.com/kb/304662

     

    Wednesday, December 21, 2011 12:01 PM
  • Hi

    Try the link below, it's about Microsoft Word Documents from ASP.NET

    http://www.codeproject.com/KB/aspnet/wordapplication.aspx


    Thanks
    Md. Marufuzzaman
    Don't forget to click [Vote] / [Good Answer] on the post(s) that helped you.
    I will not say I have failed 1000 times; I will say that I have discovered 1000 ways that can cause failure – Thomas Edison.
    Wednesday, December 21, 2011 12:17 PM
  • Hi San,

    thanks fro ur reply

    i tried but am not able to open that document inside the webbrowser control(windows application).

     

    thanks,

    Dev

    Wednesday, December 21, 2011 1:00 PM
  • hey im sorry i thought of asp

     

    please refer below code

    private void Form1_Load(object sender, EventArgs e)
    {

    string strPath ="Test.doc";
    FileStream fStream = new FileStream
    (strPath, FileMode.Open, FileAccess.Read);
    StreamReader sReader = new StreamReader(fStream);
    var str = sReader.ReadToEnd();
    sReader.Close();


    webBrowser1.DocumentText ="<html><body>"+ str +"</body></html>";
    webBrowser1.Navigating +=
    new WebBrowserNavigatingEventHandler(webBrowser1_Navigating);
    }

    private void webBrowser1_Navigating(object sender,
    WebBrowserNavigatingEventArgs e)
    {
    System.Windows.Forms.HtmlDocument document =
    this.webBrowser1.Document;

    }


    By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    • Proposed as answer by san Sanz Wednesday, December 21, 2011 1:22 PM
    • Edited by san Sanz Wednesday, December 21, 2011 1:23 PM
    Wednesday, December 21, 2011 1:22 PM
  • Hi san,

    thanks for your code,  i tried but facing the same issues, opening  as separate doc not inside the webbrowser control..  this is happening  when i try to open worddoc2007.

     

    Dev.


    • Edited by 2011deva Wednesday, December 21, 2011 1:39 PM
    Wednesday, December 21, 2011 1:39 PM
  • is it fixed now for you or not?

    By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, December 21, 2011 1:40 PM
  • no  my requirement is to open and show worddoc2007 inside the webbrowsercontrol.

    Wednesday, December 21, 2011 1:45 PM
  • do you want to download it and open it by Winword?

    By Sanz If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, December 21, 2011 1:55 PM
  • no need to download. using fileuploaddialog i can choose the file and  i should show the worddoc(2007) inside the webbrowsercontrol. for your information ,  all the above discussed things are working when i choose word2003doc. 

    but am facing the issues only wen i try to open word2007 doc.

    thanks

    Dev

     

    Wednesday, December 21, 2011 2:14 PM
  • you may need to do this:

    http://www.shaunakelly.com/word/sharing/opendocinie.html 


    laurie l clos
    Wednesday, December 21, 2011 3:49 PM
  • Hi,

    Try the following code. Before that add a reference to Microsoft.Office.Interop.Word in your project

     

    {

     Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();

                object nullobj = System.Reflection.Missing.Value;

                object file = @"D:\Codes\exportingfromexc.docx";

                Microsoft.Office.Interop.Word.Document doc = app.Documents.Open(

                        ref file, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj,

                        ref nullobj, ref nullobj, ref nullobj, ref nullobj);

                doc.ActiveWindow.Selection.WholeStory();

                doc.ActiveWindow.Selection.Copy();

                IDataObject data = Clipboard.GetDataObject();

                string text = data.GetData(DataFormats.Html).ToString();

               ((Microsoft.Office.Interop.Word._Document)doc).Close(ref nullobj, ref nullobj, ref nullobj);

                ((Microsoft.Office.Interop.Word._Application)app).Quit(ref nullobj, ref nullobj, ref nullobj);

                webBrowser1.DocumentText = "<html><body>" + text + "</html></body>";

    }

     

    • Marked as answer by Lie You Tuesday, December 27, 2011 2:47 AM
    Thursday, December 22, 2011 4:35 AM