views:

2566

answers:

4

Hello everyone, 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.

A: 

I have no experience from integrating with Microsoft Office, but I guess there are some APIs around that you can use for this.

However, if what you want to accomplish is a rudimentary way of printing formatted output and exporting it to a file that can be handled in Word, you might want to look into the RTF format. The format is quite simple to learn, and is supported by the RtfTextBox (or is it RichTextBox?), which also has some printing capabilities. The rtf format is the same format as is used by Windows Wordpad (write.exe).

This also has the benefit of not depending on MS Office in order to work.

norheim.se
+3  A: 

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.

Franci Penov
+1  A: 

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)

Peter Olsson
+1  A: 

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.

Kasprzol