Manipulating word in. Net

Source: Internet
Author: User
Tags datetime tostring
Word to manipulate word, we need Word's object library file "MSWord." OLB "(Word 2000 is msword9.olb), usually after you have Office Word installed, you can find the file under the Office10 folder in the Office installation directory, and when we bring this file into the project, We can use various action functions in the source code to manipulate word. By opening the items in the menu bar > Adding references > Browsing, after you find Msword.olb in the Open Select Components dialog box, you can introduce this object library file as determined, Vs.net will automatically convert the library file into a DLL component. So we just create the Component object in the source code to achieve the purpose of the operation of Word!

The application of namespaces in CS code files, such as using Word, and the following example:

Using System;
Using System.Drawing;
Using System.Collections;
Using System.ComponentModel;
Using System.Windows.Forms;
Using Word;
Namespace Examsecure
{
///
Summary description of the Itemtodoc.
///
public class ItemToDoc:System.Windows.Forms.Form
{
Object strFileName;
Object nothing;
Word.applicationclass mywordapp=new Word.applicationclass ();
Word.Document Myworddoc;
String Strcontent= "";
Private System.ComponentModel.Container components = null;
Public Itemtodoc ()
{
//
Required for Windows Forms Designer support
//
InitializeComponent ();
//
TODO: Add any constructor code after the InitializeComponent call
//
}
[STAThread]
static void Main ()
{
System.Windows.Forms.Application.Run (New Itemtodoc ());
}
///
Clean up all resources that are in use.
///
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (Components!= null)
{
Components. Dispose ();
}
}
Base. Dispose (disposing);
}
#region Windows Form Designer generated code
///
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
///
private void InitializeComponent ()
{
//
Itemtodoc
//
This. AutoScaleBaseSize = new System.Drawing.Size (6, 14);
This. ClientSize = new System.Drawing.Size (292, 273);
This. Name = "Itemtodoc";
This. Text = "Itemtodoc";
This. Load + = new System.EventHandler (this. Itemtodoc_load);
}
#endregion
private void Itemtodoc_load (object sender, System.EventArgs e)
{
WriteFile ();
}
private void WriteFile ()
{

strfilename=system.windows.forms.application.startuppath+ "\ Test Database" "+getrandomstring () +" ". Doc";
Object Nothing=system.reflection.missing.value;
Myworddoc=mywordapp.documents.add (ref nothing,ref nothing,ref nothing,ref Nothing);

#region write read data from a database to a Word file
Strcontent= "question Bank \n\n\r";
WriteFile (strcontent);

strcontent= "Question Bank";
WriteFile (strcontent);

#endregion

To save the contents of a WordDoc Document object as a doc document
Myworddoc.saveas (ref strfilename,ref nothing,ref nothing,ref nothing,ref nothing,ref Nothing,ref Nothing, Ref nothing,ref Nothing,ref Nothing,ref nothing,ref nothing,ref nothing,ref Nothing);
Close WordDoc Document Object
Myworddoc.close (ref nothing, ref no, ref nothing);
Close the WordApp Component Object
Mywordapp.quit (ref nothing, ref no, ref nothing);
}
///
Gets a string that is immediately
///
///
private String getrandomstring ()
{
DateTime Inow=datetime.now;
String strdate=inow.tostring ("Yyyymmddhhmmffff");

Random ran=new Random ();
int Iran=convert.toint32 (10000*ran. Nextdouble ());
String strran=iran.tostring ();
Insufficient number of digits to supplement 0
int iranlen=strran.length;
for (int i=0;i<4-iranlen;i++)
{
strran= "0" +strran;
}
return Strdate+strran;
}
///
Write a string to a Word file
///
The string to write
private void WriteFile (String str)
{
MYWORDDOC.PARAGRAPHS.LAST.RANGE.TEXT=STR;
}
}
}



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.