Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 25th, 2006, 05:18 PM
Registered User
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem opening Excel Doc when using AxBrowser

Hi,
   I've a windows form in which i open Excel documents using the AxBrowser web control and C#. The Excel document opens inside the form, but if i try to open an Excel file on my Desktop by double-clicking it while my application has an Excel file open inside the form, an MS Excel window opens but doesn't open the file on my desktop and just hangs there.

In order to get the Excel file to open inside the windows form, i had to modify my Windows Explorer File options as below:

On Windows Explorer, click Tools, Folder Options, File Types, Select extension XLS, Click Advanced, check 'Browse in same Window'.

I've attached a sample code snippet below. Any help will be highly appreciated, as i do want to be able to open other Excel documents which this application is running, with an excel open inside the form.



using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;

namespace WebBrowserTest
{
    /// <summary>
    /// Summary description for Form1.
    /// </summary>
    public class Form1 : System.Windows.Forms.Form
    {
        private AxSHDocVw.AxWebBrowser axWebBrowser1;
        private Object oDocument;
        private System.Windows.Forms.OpenFileDialog openFileDialog1;
        private System.Windows.Forms.Button button1;
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null)
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
            this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
            this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
            this.button1 = new System.Windows.Forms.Button();
            ((System.ComponentModel.ISupportInitialize)(this.a xWebBrowser1)).BeginInit();
            this.SuspendLayout();
            //
            // axWebBrowser1
            //
            this.axWebBrowser1.Enabled = true;
            this.axWebBrowser1.Location = new System.Drawing.Point(0, 32);
            this.axWebBrowser1.OcxState = ((System.Windows.Forms.AxHost.State)(resources.Get Object("axWebBrowser1.OcxState")));
            this.axWebBrowser1.Size = new System.Drawing.Size(888, 400);
            this.axWebBrowser1.TabIndex = 0;
            this.axWebBrowser1.NavigateComplete2 += new AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Even tHandler(this.axWebBrowser1_NavigateComplete2);
            //
            // button1
            //
            this.button1.BackColor = System.Drawing.Color.LightSteelBlue;
            this.button1.Location = new System.Drawing.Point(632, 4);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(96, 24);
            this.button1.TabIndex = 1;
            this.button1.Text = "Browse Files";
            this.button1.Click += new System.EventHandler(this.button1_Click);
            //
            // Form1
            //
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.BackColor = System.Drawing.Color.Goldenrod;
            this.ClientSize = new System.Drawing.Size(888, 438);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.axWebBrowser1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.Closed += new System.EventHandler(this.Form1_Closed);
            ((System.ComponentModel.ISupportInitialize)(this.a xWebBrowser1)).EndInit();
            this.ResumeLayout(false);

        }
        #endregion

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {

            String strFileName;

            //Find the Office document.
            openFileDialog1.FileName = "";
            openFileDialog1.ShowDialog();
            strFileName = openFileDialog1.FileName;

            //If the user does not cancel, open the document.
            if(strFileName.Length != 0)
            {
                Cursor.Current = Cursors.WaitCursor;
                Object refmissing = System.Reflection.Missing.Value;
                oDocument = null;
                axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing , ref refmissing , ref refmissing);
                Cursor.Current = Cursors.Default;
            }
        }

        public void Form1_Load(object sender, System.EventArgs e)
        {
            button1.Text = "Browse";
            openFileDialog1.Filter = "Office Documents(*.doc, *.xls, *.ppt)|*.doc;*.xls;*.ppt" ;
            openFileDialog1.FilterIndex = 1;
        }

        public void Form1_Closed(object sender, System.EventArgs e)
        {
            oDocument = null;
        }

        public void axWebBrowser1_NavigateComplete2(object sender, AxSHDocVw.DWebBrowserEvents2_NavigateComplete2Even t e)
        {

            //Note: You can use the reference to the document object to
            // automate the document server.

            Object o = e.pDisp;

            oDocument = o.GetType().InvokeMember("Document",BindingFlags.G etProperty,null,o,null);

            Object oApplication = o.GetType().InvokeMember("Application",BindingFlag s.GetProperty,null,oDocument,null);

            Object oName = o.GetType().InvokeMember("Name",BindingFlags.GetPr operty ,null,oApplication,null);

        }

    }
}
 
Old September 19th, 2006, 06:02 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I have exactly the same problem. Did you already found a solution/workaround for it? I am very curious!

Best regards,
Frank.
 
Old September 19th, 2006, 06:07 AM
Registered User
 
Join Date: Sep 2006
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Just a remark I want to make, if you do this trick with an instance of IE it DOES work. Somehow it probably has to do with the AxBrowser Web control and how it is beeing programmed in the .NET environment.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in opening excel file in MS Excel 2000 kallol Visual C++ 0 November 16th, 2007 05:48 AM
Opening a doc file using a jsp karthik_talasu Javascript 3 June 27th, 2006 08:02 AM
Problem in opening .doc file kumar_raj13 C# 1 March 13th, 2006 12:57 PM
opening doc (in another server) thisgal1213 Classic ASP Basics 1 January 3rd, 2006 08:55 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.