Two ways to embed Word documents in a WinForm form

Source: Internet
Author: User

Office is the Office software that we often use in our daily work, however, with the rapid development of information technology, more and more people have been using word writing documents, using Excel to make reports, and using Outlook to send and receive e-mail easily. Everything is easy, and the example will show you how to use C # to display Word documents in the form form, as shown in Figure 1.
 
Figure 1  Embedding Word documents in WinForm forms
  Key Technologies
This example focuses on how to use the WebBrowser control to display Word documents, The key techniques used in this example are explained in detail below. The
WebBrowser control is a standard control for Windows Forms applications, and you can use the WebBrowser control to display Web page information in a form. The Navigate method of the WebBrowser control allows you to load the document at the specified location into the WebBrowser control, which is commonly used in the following syntax format:
public void Navigate (string urlstring)
Parameter Description
urlstring: The URL address of the document to load.
   Description: The WebBrowser control can also load and display Excel files.
  Design Process
(1) opens the visual Studio 2008 development environment, creating a new Windows Forms application named Wordinform.
(2) Change the Name property of the default form Form1 to Frm_main, change the Text property to "embed a Word document in a WinForm form", and add a MenuStrip menu control to the form, add two menu items to the menus "open" and "exit" Used to open the Word document and exit the application, and to add a WebBrowser control to the form that displays the Word document. The main code for the
(3) program is as follows:

The code is as follows Copy Code
private void Open Toolstripmenuitem_click (object sender, EventArgs e)
{
OpenFileDialog p_getfile = new OpenFileDialog (); To create an open File dialog box object
DialogResult p_dr = P_getfile.showdialog (); Show Open File dialog box
if (p_dr = = DialogResult.OK)//Whether click OK
{
Webbrowser.navigate (P_getfile.filename); Open the Word document and display
}
}

Let's look at a complete example

The code is as follows Copy Code
Using System;
Using System.Collections.Generic;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Linq;
Using System.Text;
Using System.Windows.Forms;
Using Microsoft.Office.Core;

Namespace Wordinform
{
public partial class Frm_main:form
{
Public Frm_main ()
{
InitializeComponent ();
}

private void Btn_get_click (object sender, EventArgs e)
{

}

private void Open Toolstripmenuitem_click (object sender, EventArgs e)
{
OpenFileDialog p_getfile = new OpenFileDialog ();//Create Open File dialog box object
DialogResult p_dr = P_getfile.showdialog ()//Show Open File dialog box
if (p_dr = = DialogResult.OK)//click OK
{
Webbrowser.navigate (p_getfile.filename);//Open Word document and display
}
}

private void Exit Toolstripmenuitem_click (object sender, EventArgs e)
{
This. Close ()//closes the current form
}
}
}

Frm_Main.Designer.cs

The code is as follows Copy Code

Namespace Wordinform
{
Partial class Frm_main
{
<summary>
The required designer variable.
</summary>
Private System.ComponentModel.IContainer components = null;

<summary>
Clean up all resources that are in use.
</summary>
<param name= "disposing" > if the managed resource should be freed, true; otherwise, false. </param>
protected override void Dispose (bool disposing)
{
if (disposing && (components!= null))
{
Components. Dispose ();
}
Base. Dispose (disposing);
}

Code generated #region the Windows forms Designer

<summary>
Designer supports the required methods-No
Use the Code Editor to modify the contents of this method.
</summary>
private void InitializeComponent ()
{
THIS.MENUSTRIP1 = new System.Windows.Forms.MenuStrip ();
this. File ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem ();
this. Open ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem ();
this. Exit ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem ();
This. WebBrowser = new System.Windows.Forms.WebBrowser ();
This.menuStrip1.SuspendLayout ();
This. SuspendLayout ();
//
MenuStrip1
//
This.menuStrip1.Items.AddRange (new system.windows.forms.toolstripitem[] {
this. File ToolStripMenuItem});
This.menuStrip1.Location = new System.Drawing.Point (0, 0);
This.menuStrip1.Name = "MenuStrip1";
This.menuStrip1.Size = new System.Drawing.Size (475, 25);
This.menuStrip1.TabIndex = 1;
This.menuStrip1.Text = "MenuStrip1";
//
File ToolStripMenuItem
//
this. File ToolStripMenuItem.DropDownItems.AddRange (new system.windows.forms.toolstripitem[] {
this. Open ToolStripMenuItem,
this. Exit ToolStripMenuItem});
this. File Toolstripmenuitem.name = "File ToolStripMenuItem";
this. File Toolstripmenuitem.size = new System.Drawing.Size (44, 21);
this. File Toolstripmenuitem.text = "file";
//
Open ToolStripMenuItem
//
this. Open Toolstripmenuitem.name = "open ToolStripMenuItem";
this. Open toolstripmenuitem.size = new System.Drawing.Size (152, 22);
this. Open Toolstripmenuitem.text = "open";
this. Open Toolstripmenuitem.click + = new System.EventHandler (this. open Toolstripmenuitem_click);
//
Exit ToolStripMenuItem
//
this. Exit Toolstripmenuitem.name = "Exit ToolStripMenuItem";
this. Exit toolstripmenuitem.size = new System.Drawing.Size (152, 22);
this. Exit Toolstripmenuitem.text = "Exit";
this. Exit Toolstripmenuitem.click + = new System.EventHandler (this. exit Toolstripmenuitem_click);
//
WebBrowser
//
This. Webbrowser.dock = System.Windows.Forms.DockStyle.Fill;
This. Webbrowser.location = new System.Drawing.Point (0, 25);
This. Webbrowser.minimumsize = new System.Drawing.Size (20, 20);
This. Webbrowser.name = "WebBrowser";
This. Webbrowser.size = new System.Drawing.Size (475, 373);
This. Webbrowser.tabindex = 2;
//
Frm_main
//
This. Autoscaledimensions = new System.Drawing.SizeF (6F, 12F);
This. AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
This. ClientSize = new System.Drawing.Size (475, 398);
This. Controls.Add (this. WebBrowser);
This. Controls.Add (THIS.MENUSTRIP1);
This. Mainmenustrip = THIS.MENUSTRIP1;
This. Name = "Frm_main";
This. StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
This. Text = "Embed Word document in WinForm form";
This.menuStrip1.ResumeLayout (FALSE);
This.menuStrip1.PerformLayout ();
This. ResumeLayout (FALSE);
This. PerformLayout ();

}

#endregion

Private System.Windows.Forms.MenuStrip menuStrip1;
Private System.Windows.Forms.ToolStripMenuItem file ToolStripMenuItem;
Private System.Windows.Forms.ToolStripMenuItem open ToolStripMenuItem;
Private System.Windows.Forms.ToolStripMenuItem exit ToolStripMenuItem;
Private System.Windows.Forms.WebBrowser WebBrowser;

}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.