none
Show office document in WebBrowser control C# RRS feed

  • Question

  • I'm building a Windows Form app in C# and I have want to open a microsoft office file stored in my hard drive in a WebBrowser control. These are the methods I found :

    • A tutorial from Microsoft but it's outdated, it was for office 2005

    • Use an ActiveX instance but it also seems outdated and the result isn't that fine

    • Convert the file into an html file but the result is bad, some parts of the document are missing
    • Convert the file into a pdf file but it's too long

    I was looking for a more recent and more efficient way to do that, can anybody help me please?

    Wednesday, January 31, 2018 10:38 AM

All replies

  • I'm building a Windows Form app in C# and I have want to open a microsoft office file stored in my hard drive in a WebBrowser control. These are the methods I found :

    • A tutorial from Microsoft but it's outdated, it was for office 2005

    It is not outdated : I use it on Windows 10/Office 2016 and it still works.
    Wednesday, January 31, 2018 10:54 AM
  • Sorry I thought SHDocVw.dll and AxSHDocVw.dll were outdated and I didn't find how to use the AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Event.pDisp() so I concluded it was an old way to do that.

    How did you deal with those dll and that event?

    Wednesday, January 31, 2018 11:27 AM
  • I did nothing particular : I just followed the document and copied the code...

    + registry settings from

    A new application window opens...

    or

    http://www.axelr.com/demos/reprodoc/open.htm

    • Edited by Castorix31 Wednesday, January 31, 2018 11:41 AM
    • Proposed as answer by Cor Ligthert Wednesday, January 31, 2018 1:26 PM
    Wednesday, January 31, 2018 11:39 AM
  • Sorry, I thougt I used the wrong WeBrowser control but thanks for making me notice that. Anyway, the result is pretty bad so I'll look for another solution. Thanks!
    Wednesday, January 31, 2018 12:43 PM
  • Anyway, the result is pretty bad

    What do you mean by "pretty bad" ?

    The Excel or Word document is embedded inside your form or any control.

    Wednesday, January 31, 2018 1:43 PM
  • Hi,

    Check if this helps:

    https://code.msdn.microsoft.com/View-and-Print-Word-Excel-e6317466

    This uses an external component...

    and the result is exactly the same as with the WebBrowser.

    Thursday, February 1, 2018 8:40 AM
  • The layout is average
    Thursday, February 1, 2018 11:47 AM
  • Thanks but I wanted a native solution.
    Thursday, February 1, 2018 11:49 AM
  • The layout is average

    I don't understand : the document is displayed exactly like in Office, but as child window

    (with or without menus/toolbars, can be resized, etc...)


    • Edited by Castorix31 Thursday, February 1, 2018 11:53 AM
    Thursday, February 1, 2018 11:52 AM
  • Hi Bakoura,

    I find one sample that you could take a look:

    delegate void ConvertDocumentDelegate(string fileName);
            public Form3()
            {
                InitializeComponent();
               
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                LoadDocument(@"D:\TestField\Test8.docx");
            }
    
            private void Form3_Load(object sender, EventArgs e)
            {
               
            }
    
            private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                if (tempFileName != string.Empty)
                {
                    // delete the temp file we created. 
                    File.Delete(tempFileName);
    
                    // set the tempFileName to an empty string. 
                    tempFileName = string.Empty;
                }
            }
            string tempFileName = null;
    
            public void LoadDocument(string fileName)
            {
                // Call ConvertDocument asynchronously. 
                ConvertDocumentDelegate del = new ConvertDocumentDelegate(ConvertDocument);
    
                // Call DocumentConversionComplete when the method has completed. 
                del.BeginInvoke(fileName, DocumentConversionComplete, null);
            }
    
            void ConvertDocument(string fileName)
            {
                object m = System.Reflection.Missing.Value;
                object oldFileName = (object)fileName;
                object readOnly = (object)false;
                Microsoft.Office.Interop.Word.Application ac = null;
    
                try
                {
                    // First, create a new Microsoft.Office.Interop.Word.ApplicationClass.
                    ac = new Microsoft.Office.Interop.Word.Application();
    
                    // Now we open the document.
                    Document doc = ac.Documents.Open(ref oldFileName, ref m, ref readOnly,
                        ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                         ref m, ref m, ref m, ref m, ref m, ref m);
    
                    // Create a temp file to save the HTML file to. 
                    tempFileName = GetTempFile("html");
    
                    // Cast these items to object.  The methods we're calling 
                    // only take object types in their method parameters. 
                    object newFileName = (object)tempFileName;
    
                    // We will be saving this file as HTML format. 
                    object fileType = (object)WdSaveFormat.wdFormatHTML;
    
                    // Save the file. 
                    doc.SaveAs(ref newFileName, ref fileType,
                        ref m, ref m, ref m, ref m, ref m, ref m, ref m,
                        ref m, ref m, ref m, ref m, ref m, ref m, ref m);
    
                }
                finally
                {
                    // Make sure we close the application class. 
                    if (ac != null)
                        ac.Quit(ref readOnly, ref m, ref m);
                }
            }
    
            void DocumentConversionComplete(IAsyncResult result)
            {
                // navigate to our temp file. 
                webBrowser1.Navigate(tempFileName);
            }
    
            string GetTempFile(string extension)
            {
                // Uses the Combine, GetTempPath, ChangeExtension, 
                // and GetRandomFile methods of Path to 
                // create a temp file of the extension we're looking for. 
                return Path.Combine(Path.GetTempPath(),
                    Path.ChangeExtension(Path.GetRandomFileName(), extension));
            }

    here is a link:

    http://codinglight.blogspot.sg/2008/10/simple-docbrowser-control.html

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 2, 2018 10:09 AM