C # process the table at the specified position in the Word Document

Source: Internet
Author: User

In a project, you need to develop a small tool to insert the required data into the Word document. This requires that a table be inserted or deleted somewhere in the Word document.

This tool was developed in vs. net2005 and office2007.

1. Insert a bookmarks in the Word documents with the name "TL ";

2. Create a C # project in vs2005 and add a word class library to the reference. Because I use office2007, I chose Microsoft Word 12.0 Object Library ", if you are using office2003, you should select 11.0;

3.CodeAdd a reference to the word class library at the top;

Using Word = Microsoft. Office. InterOP. word;

4. Open a Word document

Object Missingvalue = System. reflection. Missing. value;
Object Mytrue =   False ; // Do not show word window
Object Filename =   @" F: \ doc1.doc " ;
Word. _ application oword =   New Word. applicationclass ();
Word. _ document odoc;
Odoc = Oword. Documents. Open ( Ref Filename, Ref Missingvalue,
Ref Mytrue, Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue );

5. Find the added bookmarks.

Object TMP =   " T1 " ;
Word. Range startrange = Oword. activedocument. bookmarks. get_item ( Ref TMP). range;

6. Delete the table at this location.

Word. Table TBL = Startrange. Tables [ 1 ];
TBL. Delete ();

If the position of the bookmarks is not inserted into the table,ProgramThe table below the location is not deleted, but an exception is thrown and an error is returned.

7. Insert a table and draw lines

// Add Table
Odoc. Tables. Add (startrange, 5 , 4 , Ref Missingvalue, Ref Missingvalue );

// Dashes a table
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdbordertop]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderleft]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderright]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderbottom]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderhorizontal]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdbordervertical]. linestyle = Wdlinestyle. wdlinestylesingle;

The Code is as follows:

Object Missingvalue = System. reflection. Missing. value;
Object Mytrue =   False ; // Do not show word window
Object Filename =   @" F: \ doc1.doc " ;
Word. _ application oword =   New Word. applicationclass ();
Word. _ document odoc;
Odoc = Oword. Documents. Open ( Ref Filename, Ref Missingvalue,
Ref Mytrue, Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue, Ref Missingvalue, Ref Missingvalue,
Ref Missingvalue );
Try
{
Object TMP =   " T1 " ;
Word. Range startrange = Oword. activedocument. bookmarks. get_item ( Ref TMP). range;

// Delete the first table after the specified bookmarks
Word. Table TBL = Startrange. Tables [ 1 ];
TBL. Delete ();

// Add Table
Odoc. Tables. Add (startrange, 5 , 4 , Ref Missingvalue, Ref Missingvalue );

// Dashes a table
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdbordertop]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderleft]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderright]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderbottom]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdborderhorizontal]. linestyle = Wdlinestyle. wdlinestylesingle;
Startrange. Tables [ 1 ]. Borders [wdbordertype. wdbordervertical]. linestyle = Wdlinestyle. wdlinestylesingle;

}
Catch
{
//Exception Handling
}

Object Bsavechange =   True ;
Odoc. Close ( Ref Bsavechange, Ref Missingvalue, Ref Missingvalue );
Odoc =   Null ;
Oword =   Null ;

The code is very simple. During the process of writing this instance, I refer to the following materials:
Word object model Overview

Word task

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.