VC uses the word replacement function to print the invoice

Source: Internet
Author: User

VCUse Word replacement to print invoices

 

From Network

 

When developing a project with VC, You need to print the invoice. Because there are many types of invoices, and some have text, some are blank, and some are variable and dynamic. If VC is used to directly control its output, it is a very annoying thing, and the workload is huge. Therefore, considering that the Office Word text operation software has outstanding text operation functions, and it has related VBA functions for secondary development. Of course, the corresponding operation functions will also be provided in VC. As long as word is installed on the computer, you only need to import other functions into the development program.

The related idea is to make an invoice template in word, and then set the words that need to be dynamically modified to a specific sign, and replace it with the word replacement function in VC, this saves a lot of trouble and simplifies the operation. In this way, the invoice format and text location can be changed simply by changing the Word template design.

 

// PreparationsReference blog article manipulate MS word 123 in Visual C ++

1. create (or open existing) an MFC project
2 Ctrl + W execute classwizard-> automation-> Add class... /from a type library... in the Office2000 directory, find msword9.olb C:/program files/Microsoft Office/office/msword9.olb (the file name varies by version)-> select the desired class, alternatively, use the mouse and shift key to select all options.
3. initialize COM. Method 1: Find the initinstance () function of the app and add the call of the afxoleinit () function to it.
4. In the CPP file that needs to call the office function
# Include <atlbase. h> // use the ccomvariant template class to facilitate variable operations of the variant type.
# Include "header file. H" // The specific header file name is determined by the file name of the load type library. (You can see the double-point packaging file)
// For example, if msword9.olb is used, the header file is msword9.h

// Related variables
Find fndindoc;
Range myrange;
_ Application MyApp;
Documents mydocs;
_ Document mydoc;
Replacement rpindoc;

// Open Word
MyApp. createdispatch ("word. application ");
MyApp. setvisible (true );

// Open the document

************************* *******************/
Cstring filename ("test.doc ");
Tchar exefullpath [max_path];
Cstring strpath;
Getmodulefilename (null, exefullpath, max_path );
Strpath = (cstring) exefullpath;
Int position = strpath. reversefind ('//');
Strpath = strpath. Left (Position + 1 );
Filename = strpath + filename;
/*************************************** *************************/

// Colevariant filename ("C: // test.doc"); // note the full path of the file name.
// If the file cannot be found, an error will occur.
Colevariant filename (filename); // put test.doc in the current path

Colevariant vtrue (short) True ),
Vfalse (short) False ),
Vopt (long) disp_e_paramnotfound, vt_error );
Mydocs = MyApp. getdocuments ();
Mydoc = mydocs. Add (filename, vopt );

// Using the replacement function, the selection of relevant parameters is the most difficult
Myrange = mydoc. getcontent ();
Fndindoc = myrange. getfind ();
Fndindoc. clearformatting ();
Rpindoc = fndindoc. getreplacement ();
Rpindoc. clearformatting ();
 
Cstring replacestr ("# user name #"); // replaced
Cstring replacestrwith ("Qiu 19"); // replace

Colevariant text (replacestr); // replace
Colevariant matchcase (short) False );
Colevariant matchwholeword (short) False );
Colevariant matchwildcards (short) False );
Colevariant matchsoundslike (short) False );
Colevariant matchallwordforms (short) False );
Colevariant forward (short) True );
Colevariant wrap (short) 1); // you can use msgbox (wdfindcontinue) to obtain
Colevariant format (short) False );
Colevariant replacewith = (replacestrwith); // replace
Colevariant Replace (short) 2); // use msgbox (wdreplaceall) to obtain
Colevariant matchkashida = (short) False); // The default value of the following four parameters is false.
Colevariant matchdiacritics = (short) False );
Colevariant matchalefhamza = (short) False );
Colevariant matchcontrol = (short) False );
 
Fndindoc. Execute (& text, & matchcase, & matchwholeword, & matchwildcards,
& Matchsoundslike, & matchallwordforms, & forward, & wrap,
& Format, & replacewith, & replace, & matchkashida,
& Matchdiacritics, & matchalefhamza, & matchcontrol );

// Print
Colevariant covtrue (short) True ),
Covfalse (short) False ),
Covoptional (long) disp_e_paramnotfound, vt_error );
 
// Mydoc. printpreview (); // if you want to print the previewed document, add this sentence
// Afxmessagebox ("preview ");
If (MessageBox ("print", "print", mb_iconexclamation | mb_okcancel) = idok)
{
Mydoc. printout (covfalse, // background.
Covoptional, // append.
Covoptional, // range.
Covoptional, // outputfilename.
Covoptional, // from.
Covoptional, //.
Covoptional, // item.
Colevariant (long) 1), // copies.
Covoptional, // pages.
Covoptional, // pagetype.
Covoptional, // printtofile.
Covoptional, // collate.
Covoptional, // activeprintermacgx.
Covoptional, // manualduplexprint.
Covoptional, // printzoomcolumn new with Word 2002
Covoptional, // printzoomrow Ditto
Covoptional, // printzoompaperwidth Ditto
Covoptional); // printzoompaperheight ditto */
}
// Close
Ccomvariant savechanges (false), originalformat, routedocument;
MyApp. Quit (& savechanges, & originalformat, & routedocument );

// Clear data
Myrange. releasedispatch ();
Fndindoc. releasedispatch ();
Rpindoc. releasedispatch ();
Mydocs. releasedispatch ();
Mydoc. releasedispatch ();

 

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.