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);
}
}
}
|