The macros provided by word can provide us with sufficient help and support during VBA calls. , Convenient and fast.
Using Microsoft. Office. InterOP. word;
Using system. IO;
Using system. Web;
Using Microsoft. Win32;
Using system. Text. regularexpressions;
Using system. net;
/// <Summary>
/// Import data to the. DOC file
/// </Summary>
/// <Param name = "ds"> </param>
Public Void Exporttoword ()
{
Object Tempfilename = @" C: \ eeeeee.doc " ;
Object Filename = @" C: \ 555555.doc " ;
Object Tablebehavior = Microsoft. Office. InterOP. Word. wddefaulttablebehavior. wdword9tablebehavior;
Object Autofitbehavior = Microsoft. Office. InterOP. Word. wdautofitbehavior. wdautofitfixed;
Object Unit = Microsoft. Office. InterOP. Word. wdunits. wdline; // Move to the end
Object Extend = System. reflection. Missing. value;
Object Breaktype = ( Int ) Microsoft. Office. InterOP. Word. wdbreaktype. wdsectionbreaknextpage;
Object Count = 7 ;
Object Character = Microsoft. Office. InterOP. Word. wdunits. wdcharacter;
Object Nothing = System. reflection. Missing. value;
Microsoft. Office. InterOP. Word. Application myword = New Microsoft. Office. InterOP. Word. applicationclass ();
Microsoft. Office. InterOP. Word. _ document mydoc = New Microsoft. Office. InterOP. Word. documentclass ();
Object Omissing = System. reflection. Missing. value;
Object Oreadonly = False ;
Mydoc = Myword. Documents. Open ( Ref Tempfilename, Ref Omissing, Ref Oreadonly, Ref Omissing, Ref Omissing,
Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing, Ref Omissing );
Mydoc. Activate ();
Table newtable=Mydoc. Tables [1];
//Set Table Styles
Newtable. Borders. outsidelinestyle=Wdlinestyle. wdlinestylethickthinlargegap;
Newtable. Borders. insidelinestyle=Wdlinestyle. wdlinestylesingle;
/// /Fill in the table content
// Newtable. Cell (1, 1). range. Text = "product details table \ n \ r ssdfgdfg \ n \ r ";
// Newtable. Cell (1, 1). range. Bold = 2; // Set the cell font to bold
/// /Fill in the table content
// Newtable. Cell (1, 1). range. Text = "product details table \ n \ r d \ n \ r ";
// Newtable. Cell (1, 1). range. Bold = 2; // Set the cell font to bold
Object Dirc = Microsoft. Office. InterOP. Word. wdcollapsedirection. wdcollapsestart;
Object Tend = Microsoft. Office. InterOP. Word. wdmovementtype. wdmove;
Newtable. Cell ( 1 , 1 ). Range. Select (); // Get focus
Count = 1 ;
Mydoc. activewindow. selection. moveright ( Ref Character, Ref Count, Ref Tend );
Newtable. Cell ( 1 , 1 ). Range. Text = " Fill in table content F " ;
Newtable. Cell ( 1 , 1 ). Range. Font. Color = Wdcolor. wdcolorred;
Mydoc. activewindow. selection. Collapse ( Ref Dirc );
Mydoc. activewindow. selection. Font. Color = Wdcolor. wdcolorred;
Newtable. Cell ( 1 , 1 ). Range. Select (); // Get focus
Unit = Microsoft. Office. InterOP. Word. wdunits. wdline; // Move to the end
Mydoc. activewindow. selection. endkey ( Ref Unit, Ref Extend );
Mydoc. activewindow. selection. insertafter ( " Second insert " );
Mydoc. activewindow. selection. Font. Color = Wdcolor. wdcolorgreen;
Mydoc. activewindow. selection. Font. Bold = 1 ;
Newtable. Cell ( 1 , 1 ). Range. Select (); // Get focus
Unit = Microsoft. Office. InterOP. Word. wdunits. wdline; // Move to the end
Mydoc. activewindow. selection. endkey ( Ref Unit, Ref Extend );
Mydoc. activewindow. selection. insertafter ( " The third insert " );
Mydoc. activewindow. selection. Font. Color = Wdcolor. wdcolordarkblue;
Mydoc. activewindow. selection. Font. Bold = 1 ;
Newtable. Cell ( 1 , 1 ). Range. Select (); // Get focus
Unit = Microsoft. Office. InterOP. Word. wdunits. wdline; // Move to the end
Mydoc. activewindow. selection. endkey ( Ref Unit, Ref Extend );
Myword. application. selection. typeparagraph (); // Start a new line
Myword. application. selection. paragraphformat. Alignment = Wdparagraphalignment. wdalignparagraphright;
Mydoc. activewindow. selection. insertafter ( " Insert 4th times " );
Mydoc. activewindow. selection. Font. Color = Wdcolor. wdcolordarkgreen;
Mydoc. activewindow. selection. Font. Italic = 1 ;
Newtable. Cell ( 1 , 1 ). Range. Select (); // Get focus
Unit = Microsoft. Office. InterOP. Word. wdunits. wdline; // Move to the end
Mydoc. activewindow. selection. endkey ( Ref Unit, Ref Extend );
Myword. application. selection. paragraphformat. Alignment = Wdparagraphalignment. wdalignparagraphright;
Mydoc. activewindow. selection. insertafter ( " Date: " + Datetime. Today );
Mydoc. activewindow. selection. Font. Color = Wdcolor. wdcolorblue;
Mydoc. activewindow. selection. Font. Bold = 1 ;
// Save the Word file to the specified directory.
Try
{
Mydoc. saveas ( Ref Filename, 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 );
Myword. Visible = True ;
}
Catch
{
Return ;
}
Myword. Visible = True ;
}
Generate cells in the following table: