Creating, opening and printing a word file from C++

44,811

Solution 1

You can use Office Automation for this task. You can find answers to frequently asked questions about Office Automation with C++ at http://support.microsoft.com/kb/196776 and http://support.microsoft.com/kb/238972 .

Keep in mind that to do Office Automation with C++, you need to understand how to use COM.

Here are some examples of how to perform various tasks in word usign C++:

Most of these samples show how to do it using MFC, but the concepts of using COM to manipulate Word are the same, even if you use ATL or COM directly.

Solution 2

As posted as an answer to a similar question, I advise you to look at this page where the author explains what solution he took to generate Word documents on a server, without MsWord being available, without automation or thirdparty libraries.

Solution 3

When you have the file and just want to print it, then look at this entry at Raymond Chen's blog. You can use the verb "print" for printing.

See the shellexecute msdn entry for details.

Solution 4

You can use automation to open MS Word (in background or foreground) and then send the needed commands.

A good starting place is the knowledge base article Office Automation Using Visual C++

Some C source code is available in How To Use Visual C++ to Access DocumentProperties with Automation (the title says C++, but it is plain C)

Share:
44,811
Mariia Zhokh
Author by

Mariia Zhokh

Updated on February 25, 2020

Comments

  • Mariia Zhokh
    Mariia Zhokh over 4 years

    I have three related questions.

    I want to create a word file with a name from C++. I want to be able to sent the printing command to this file, so that the file is being printed without the user having to open the document and do it manually and I want to be able to open the document. Opening the document should just open word which then opens the file.

  • JamesUsedHarden
    JamesUsedHarden about 13 years
    This is a great answer to the question asked. I want to point out for others that are linked to this answer from other questions, that this is not appropriate for server-side use or when there is no user logged in. This is not the situation in this question, but there are other questions that link here that were about server-side use and in those cases Office Automation is not appropriate. For printing on a desktop, it is perfectly suited.