SoFunction
Updated on 2025-03-06

A comprehensive example of a C# operation word document

I have studied how to output Word documents using VS2008 (C# language). Here are a few summary:

1. Very simple.

2. Development and operating environment requirements. The operating system is: Windows XP (install .net framework2.0)/Vista/Win7; the fully installed version of Word2003 must be installed in the operating system. It must be emphasized here that Word2003 is fully installed, because software development and operation require a com component: Microsoft word 11.0 Object Library. If it is not the fully installed version of Word2003, you can download this com component and manually install this com component. The download address is:/download/en/?displaylang=en&id=20923, the file is not big, only about 4M.

3. C# project settings. The project settings here are to add com components. The steps are: In Project Explorer, "Add Reference" -> "com" tab -> Select Microsoft word 11.0 Object Library in the drop-down list. OK, it looks the same as adding a normal dll, but in fact, vs2008 completes a series of complex operations on .net calling the com component in this process. Fortunately, we don't have to worry about this.

4. Next is to write the code. Here, using Word's com object is very smooth like using a general non-com object, as if it doesn't matter what it is. In order to make the code simpler, you can add a statement like this at the top of the source code file: using Word = ;

5. It is best to have a certain understanding of the word object model, so that you will not be so "confused" when writing code. There are several more important objects in the wore object model, namely Application, Document, Selection, Range, Bookmark, and other objects, such as Paragraph, Section, Table level. When I first started learning, I felt that the objects of Selection, Range, and Bookmark were a bit confusing. Selection may be easy to understand, which means the current selection area. If there is no selection, it means the location of the cursor. Range and Bookmark are actually very similar in many places, but there are some differences. I won’t talk about it here, just google it.

6. During the process of writing code, some operations that you often want to implement, but because you are not familiar with word objects, you don’t know how to implement them with code. For example, setting page headers, adding page numbers, etc., if you manually operate it in a Word program, it is of course very simple, but it may not be so easy for beginners to implement it with code. In this case, there are generally two methods to choose: one is "Baidu/google method", and the other is also the one I recommend, using Word's "recording macro" function to record the operations you want to implement into a macro, and then look at the code in the macro, the code in the macro is actually almost the code you want (but the syntax is a little different).

7. The following is an example. This example includes the editing and layout of some commonly used figures, texts, tables, formulas, as well as the operations of page settings, headers, and page numbers. There are comments inside and they are written very clearly.

using System;
using ;
using ;
using ;
using ;
using ;
using ;
using Word = ;
using ; 

namespace WindowsFormsApplication1
{
  public partial class Form1 :. Form
  {

    [DllImport(" ")]
    public static extern int ShellExecute(IntPtr hwnd, String lpszOp, String lpszFile, String lpszParams, String lpszDir, int FsShowCmd); 
    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
      // Create a new document      // newapp = new ();// Use this sentence to initialize it       newapp = new ();
       newdoc;
      object nothing=;// Used as the default parameter for function      newdoc = (ref nothing, ref nothing, ref nothing, ref nothing);//Generate a word document       = true ;//Whether to display the word program interface
      //Page settings      // =  ;
      // = (21.0f);
      // = (29.7f);
       = .wdPaperA4;
       = ;
       = 57.0f;
       = 57.0f;
       = 57.0f;
       = 57.0f;
       = 30.0f;//Header position
      //Set the header       = ;//View style.       = ;//Enter the header settings, where the header margin has been completed in the page settings       = ;
      //Insert the header picture      string headerfile = "d:\\";
      (headerfile, );
       shape1= (headerfile, ref nothing, ref nothing, ref nothing);
       = 30;
       = 80;
      ("China Construction Northeast Courtyard");
      //Remove the horizontal line of the header      [].LineStyle = ;
      [].Visible = false;
       = ;//Exit header settings
      //Add page number       pns= [1].Headers[].PageNumbers;
       = ;
       = 0;
       = false;
       = ;
       = false;
       = 0;
      object pagenmbetal=;
      object first=true; 
      [1].Footers[ ].(ref pagenmbetal, ref first);

      //Text settings (Selection represents the current selection set. If there is no object currently selected, it means setting the cursor)       = 14;
       = 0;
       = ;
       = "Song-style";

      //Parameter settings       = ;
       = 20;
       = ;
       = 30;
      ( );
      
      

      //Insert formula      object oEndOfDoc="\\endofdoc";
       rang1 = .get_Item(ref oEndOfDoc).Range;
      object fieldType = ;
      object formula = @"eq \i(a,b,ξxdx)";
      object presrveFormatting = false;
       = ();
       = 14;
       = 0;
       = 0;
       = ;
       = "Song-style";
       = ;
       = ;
      (rang1, ref fieldType, ref formula, ref presrveFormatting);

      //Replace the first three words of the document with "asdfasdf" and set its color to blue      object start=0;
      object end=3;
       rang2 = (ref start, ref end);
       = ;
       = "as sign";

      //Replace "as" at the beginning of the document with "Yuan Bo"       = 0;
       = 2;
       = "This is one";
      ("Book");
      //();
      object codirection = ;
      (ref codirection);//Set the starting point and end point of rang1 at the starting point or end point
      //Bold the first three characters      (ref start, ref end).Bold = 1;
      object rang = rang2;
      ("yb",ref rang);
      
      
      object unite = ;
      (ref unite, ref nothing);//Move the cursor to the end of the article       = 10;
      ("...............................(Mode1)\n");


      //Insert the picture      (ref unite, ref nothing);//Move the cursor to the end of the article      //(ref unite, ref nothing);//Move the cursor to the beginning of the text       = ;
       = ;
      object LinkToFile = false;
      object SaveWithDocument = true;
      object Anchor = ;
      string picname = "d:\\";
      (picname, .IMG_2169);
      (picname, ref LinkToFile, ref SaveWithDocument, ref Anchor);
      [1].Height = 200;
      [1].Width = 200;
      ("\n");
      (ref unite, ref nothing);//Move the cursor to the end of the article       = ;
       = 10;
      ("Picture 1 Yuan Ye\n");
       = ;
      ("\n"); 
      ("\n");

      //In this way, you can also insert formulas, and this method is simpler       = 14;
      (ref formula, ref nothing);
       = 10;
      ("..............................(Mode2)\n");
       = ;
      ("Table 1 Electronic Products\n");

      //Insert the table       table1 = (, 4, 3, ref nothing, ref nothing);
      [1].Cell(1, 1). = "Product\nProject";
      [1].Cell(1, 2). = "computer";
      [1].Cell(1, 3). = "cell phone";
      [1].Cell(2, 1). = "weight(kg)";
      [1].Cell(3, 1). = "price(Yuan)";
      [1].Cell(4, 1). = "Common Information";
      [1].Cell(4, 2). = "Information A";
      [1].Cell(4,3). = "Information B";
      

      ();
       = ;//The entire table is centered       = ;
       = ;
       = ;
       = 40;
      [2].Height = 20;
      [3].Height = 20;
      [4].Height = 20;
       = ;
      =150;
      [1].Width = 75;
      (1, 1). = ;
      (1, 1).[2]. = ;

      
      

      //Table header slash      (1, 1).Borders[ ].Visible = true;
      (1, 1).Borders[].Color = ;
      (1, 1).Borders[].LineWidth = .wdLineWidth050pt;

      //Table border      [ ].Visible = true;
      [].Color = ;
      [].LineWidth = .wdLineWidth050pt;

      [].Visible = true;
      [].Color = ;
      [].LineWidth = .wdLineWidth050pt;

      [].Visible = true;
      [].Color = ;
      [].LineWidth = .wdLineWidth050pt;
      [].LineStyle = ;

      [].Visible = true;
      [].Color = ;
      [].LineWidth = .wdLineWidth050pt;
      [].LineStyle = ;

      [ ].Visible = true;
      [].Color = ;
      [].LineWidth = .wdLineWidth050pt;
      [].LineStyle = ;

      [ ].Visible = true;
      [].Color = ;
      [].LineWidth = .wdLineWidth050pt;
      [].LineStyle = ;

      //Merge cells      [1].Cell(4, 2).Merge((4, 3));

      //Delete the picture      (headerfile);
      (picname);


      //Save the document      object name = "c:\\";
      (ref name, 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,
             ref nothing, ref nothing);
      
      //Close the document      object saveOption = ;
      (ref nothing , ref nothing, ref nothing);
      (ref saveOption, ref nothing, ref nothing);
      newdoc = null;
      newapp = null;
      ShellExecute(, "open", "c:\\", "", "", 3);
    }

    private void outpicture(string filename, bmap)
    {
      (filename);
    }

    private void delpictfile(string filename)
    {
      (filename);
    }
   
  }

}