VC ++ uses automation to operate Word and Excel

Source: Internet
Author: User
For VC operations on Word and Excel, I have copied online materials and have my own understanding and experience. 1. In general, the more direct way for VC to operate Word and Excel is to use Automation. It is automation to translate into Chinese. The first thing you need to do is add the header files required for the Word and Excel operations for your project. The method is as follows: For vc6.0, Open Class Wizard and select automation. The following page is displayed: click "add class". A drop-down menu is displayed, with only two items, select "from a type lib". A dialog box is displayed, prompting you to enter the typelib file. These files are generally in the installation directory of your office and are named MSWord. click "OK" After selecting olb. The following dialog box is displayed: select all classes and click "OK". Then, you can generate two required files, MSWord. H and MSWord. CPP. (The name can be modified ). The Excel method is similar. Using automation to perform similar operations on Word and Excel. The class structure is as follows: Application // represents the program workbooks // represents all Word documents workbook // a single word document

......

Worksheets // a set of EXCEL tables. worksheet // a set of tables.

......

Range // Activity Area

Cells/cell Cells

Rows/row

Columns/column

Tables/table

Bookmarks/bookmark tag

Save saveas

Printout Printing

 

 

I won't talk much about the rest. Just give it to the code.

About word:

Coinitialize (null); // initialize the COM component, required

_ Application wordapp;

If (! Wordapp. createdispatch ("word. application") // link the wordapp to the word Application

{

Couninitialize ();

Return false;

}

 

...

Documents docs = wordapp. getdocuments (); // obtain the Word Document Set

_ Document DOC;

Colevariant vtoptional (long) disp_e_paramnotfound, vt_error );

Colevariant vttrue (short) True), vtfalse (short) False );

 

Cstring filename; // name of the file to be opened

If (filename. Find (". Dot", 0)> 0) // If the suffix is. Dot, it indicates a Word template.

{

// Add the template to the word

Colevariant covfilename;

Covfilename. Vt = vt_bstr;

Covfilename. bstrval = reportfilename. allocsysstring ();

Doc = docs. Add (covfilename, vtoptional); // call add

}

Else

{

// If a common document is a. DOC file, create a new document and add the file to it.

Doc = docs. Add (vtoptional, vtoptional );

// The selection will be released automatically.

Selection oselection;

Oselection = wordapp. getselection ();

// Insert an object

Oselection. insertfile (filename. getbuffer (reportfilename. getlength (), vtoptional, vtoptional );

Filename. releasebuffer ();

Oselection. detachdispatch ();

}

 

Of course, you can use open to open a file. There is a corresponding open method.

 

Obtain the version number of word. 2000 corresponds to 9.0, 2002 corresponds to 10.0, 2003 corresponds to 11.0, 2007 corresponds to 12.0

Cstring sversion = wordapp. getversion ();

Colevariant tablebookmarkname (bookmarkname );

// Obtain the word tag

Bookmarks wordbookmarks = Doc. getbookmarks ();

// Obtain the specified tag Based on the Tag Name

Bookmark wordbookmark;

Wordbookmark = wordbookmarks. Item (tablebookmarkname );

// Obtain the region

Range;

Range = wordbookmark. getrange ();

// Obtain the table connected by the Region

Tables Ts = range. gettables ();

Table T = ts. Item (1 );

Rows = T. getrows ();

Row row = rows. Item (1); // The index is starts 1, 2...

Cells = row. getcells ();

Int colcount = cells. getcount ();

 

Cell cell = T. Cell (2, K + 1 );

Range = cell. getrange ();

Cstring INH = range. gettext ();

Row = rows. Add (vtoptional );

// Set text

Range. settext (inhnew );

 

Save the file. Note that different versions may lead to different interfaces.

Colevariant savefilename, fileformat;

 

If (sversion = "9.0 ")

{

Doc. saveas9 (savefilename, vtoptional,

Vtoptional, vtoptional );

}

Else if (sversion = "10.0 ")

{

Doc. saveas (& vneuername, & vtoptional,

Vtoptional, vtoptional );

}

Else if (sversion = "11.0 ")

{

Doc. saveas (& vneuername, & vtoptional,

Vtoptional, vtoptional );

}

 

// Print the file Doc. printout (vtoptional, region, vtoptional, vtoptional );

Exit:

Wordapp. Quit (vtoptional, vtoptional, vtoptional );

You can also use shellexcute to open and print a word file.

 

 

 

 

 

 

 

 

 

 

 

 

 

 


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.