我有三个相关的问题。从C++创建,打开和打印word文件
我想用C++创建一个名称为word的文件。我希望能够将打印命令发送到此文件,以便打印文件时用户不必打开文档并手动执行,而且我希望能够打开文档。打开文档应该打开单词,然后打开文件。
我有三个相关的问题。从C++创建,打开和打印word文件
我想用C++创建一个名称为word的文件。我希望能够将打印命令发送到此文件,以便打印文件时用户不必打开文档并手动执行,而且我希望能够打开文档。打开文档应该打开单词,然后打开文件。
我没有与Microsoft Office集成的经验,但我想有一些API可以用于此目的。
但是,如果您想要完成的是打印格式化输出并将其导出为可以在Word中处理的文件的基本方式,则可能需要查看RTF格式。这种格式很容易学习,并且由RtfTextBox(或者它是RichTextBox?)支持,它也具有一些打印功能。 rtf格式与Windows写字板(write.exe)使用的格式相同。
这也有利于不依赖MS Office工作。
您可以使用Office Automation执行此任务。您可以通过C++在http://support.microsoft.com/kb/196776和http://support.microsoft.com/kb/238972找到关于Office自动化的常见问题解答。
请记住,要使用C++执行Office自动化,您需要了解如何使用COM。
这里是如何执行的话usignÇ各种任务的一些例子++:
大部分样品的展示了如何使用做MFC,但使用COM来操作Word的概念是相同的,即使您直接使用ATL或COM。
您可以使用自动化功能打开MS Word(在后台或前台),然后发送所需的命令。
一个很好的起点,是知识库文章Office Automation Using Visual C++
某些C源代码都可以在How To Use Visual C++ to Access DocumentProperties with Automation(标题说C++,但它是纯C)
当你有文件,只是想打印出来,然后在Raymond Chen的博客上看this entry。您可以使用动词“打印”进行打印。
有关详细信息,请参阅shellexecute msdn entry。
作为对similar question的回答发布,我建议您查看this page,作者解释了他在服务器上生成Word文档所用的解决方案,没有MsWord可用,没有自动化或第三方库。
我的这个解决方案是使用下面的命令:
start /min winword <filename> /q /n /f /mFilePrint /mFileExit
这允许用户指定一台打印机,没有。拷贝等。
用文件名替换<filename>
。如果它包含空格,它必须用双引号括起来。 (如file.rtf
,"A File.docx"
)
它可以被放置在系统调用中为:
system("start /min winword <filename> /q /n /f /mFilePrint /mFileExit");
下面是与处理这个,所以你不必记住所有的功能的C++头文件开关,如果你经常使用它:
/*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
这是一个很好的回答问题。我想指出其他问题与其他问题相关的其他问题,这不适用于服务器端用户或没有用户登录的情况。这不是这个问题的情况,但还有其他问题链接这里是关于服务器端使用的,在这些情况下,Office自动化并不合适。对于在桌面上打印,它非常适合。 – 2011-04-28 16:49:47