Embedding Microsoft word in a C# windows application

S

Sirdots

Hi Everyone,
I have a C# windows application and will want to embed Microsoft word
on it(a form). I want to be able to use the spell checker and see the
squiggy lines if there is an error.
I have seen some samples where you have to use the openDialog to choose
a word document. I dont want this. I will prefer a click on a command
button to automatically open word within the web browser on my form.
Also, I want to have a dropdown menu below the button where users can
select templates for their word documents.
Do yu have any idea?

I just copied some parts of the code that are relevant.

Thanks.


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

//
// TODO: Add any constructor code after InitializeComponent call
//
this.axWebBrowser1.NavigateComplete2 += new
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);

}

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)
{
Object refmissing = System.Reflection.Missing.Value;
oDocument = null;
axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing
, ref refmissing , ref refmissing);
}
}

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_NavigateComplete2Event 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.GetProperty,null,o,null);

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

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

MessageBox.Show("File opened by: " + oName.ToString() );
}
 
S

Sirdots

Hi, I just decided to show the whole code.
Thanks.


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


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

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

//
// TODO: Add any constructor code after InitializeComponent call
//
this.axWebBrowser1.NavigateComplete2 += new
AxSHDocVw.DWebBrowserEvents2_NavigateComplete2EventHandler(this.axWebBrowser1_NavigateComplete2);
this.Load += new System.EventHandler(this.Form1_Load);
this.Closed += new System.EventHandler(this.Form1_Closed);

}

/// <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(EmbedDots));
this.button1 = new System.Windows.Forms.Button();
this.axWebBrowser1 = new AxSHDocVw.AxWebBrowser();
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(16, 16);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// axWebBrowser1
//
this.axWebBrowser1.Enabled = true;
this.axWebBrowser1.Location = new System.Drawing.Point(16, 64);
this.axWebBrowser1.OcxState =
((System.Windows.Forms.AxHost.State)(resources.GetObject("axWebBrowser1.OcxState")));
this.axWebBrowser1.Size = new System.Drawing.Size(800, 432);
this.axWebBrowser1.TabIndex = 1;
//
// EmbedDots
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(832, 494);
this.Controls.Add(this.axWebBrowser1);
this.Controls.Add(this.button1);
this.Name = "EmbedDots";
this.Text = "EmbedDots";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
((System.ComponentModel.ISupportInitialize)(this.axWebBrowser1)).EndInit();
this.ResumeLayout(false);

}
#endregion

static void Main()
{
Application.Run(new EmbedDots());

}

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)
{
Object refmissing = System.Reflection.Missing.Value;
oDocument = null;
axWebBrowser1.Navigate(strFileName, ref refmissing , ref refmissing
, ref refmissing , ref refmissing);
}
}

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_NavigateComplete2Event 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.GetProperty,null,o,null);

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

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

MessageBox.Show("File opened by: " + oName.ToString() );
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top