C # Fill the word table with VBA, and transfer the cell focus or insert text at will, as well as related attributes such as the color and bold of the text.

Source: Internet
Author: User
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:

Related Article

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.