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