Creating, opening and printing a word file from C++
Asked Answered
A

6

17

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.

Alesandrini answered 28/9, 2008 at 9:44 Comment(0)
I
15

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.

Innutrition answered 28/9, 2008 at 10:1 Comment(1)
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.Primacy
M
4

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.

Merri answered 28/4, 2011 at 7:8 Comment(0)
O
2

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.

Own answered 28/9, 2008 at 10:16 Comment(0)
C
1

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)

Correspondence answered 28/9, 2008 at 10:2 Comment(0)
G
0

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.

Gapin answered 28/9, 2008 at 9:58 Comment(0)
F
0

My solution to this is to use the following command:

start /min winword <filename> /q /n /f /mFilePrint /mFileExit

This allows the user to specify a printer, no. of copies, etc.

Replace <filename> with the filename. It must be enclosed in double-quotation marks if it contains spaces. (e.g. file.rtf, "A File.docx")

It can be placed within a system call as in:

system("start /min winword <filename> /q /n /f /mFilePrint /mFileExit");

Here is a C++ header file with functions that handle this so you don't have to remember all of the switches if you use it frequently:

/*winword.h
 *Includes functions to print Word files more easily
 */

#ifndef WINWORD_H_
#define WINWORD_H_

#include <string.h>
#include <stdlib.h>

//Opens Word minimized, shows the user a dialog box to allow them to
//select the printer, number of copies, etc., and then closes Word
void wordprint(char* filename){
   char* command = new char[64 + strlen(filename)];
   strcpy(command, "start /min winword \"");
   strcat(command, filename);
   strcat(command, "\" /q /n /f /mFilePrint /mFileExit");
   system(command);
   delete command;
}

//Opens the document in Word
void wordopen(char* filename){
   char* command = new char[64 + strlen(filename)];
   strcpy(command, "start /max winword \"");
   strcat(command, filename);
   strcat(command, "\" /q /n");
   system(command);
   delete command;
}

//Opens a copy of the document in Word so the user can save a copy
//without seeing or modifying the original
void wordduplicate(char* filename){
   char* command = new char[64 + strlen(filename)];
   strcpy(command, "start /max winword \"");
   strcat(command, filename);
   strcat(command, "\" /q /n /f");
   system(command);
   delete command;
}

#endif
Faun answered 30/11, 2011 at 21:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.