从 C++ 创建、打开和打印 word 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/145573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating, opening and printing a word file from C++
提问by user23163
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.
我想用 C++ 中的名称创建一个 word 文件。我希望能够将打印命令发送到该文件,以便在用户不必打开文档并手动执行该文件的情况下打印该文件,并且我希望能够打开该文档。打开文档应该只打开 word,然后打开文件。
回答by Franci Penov
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/196776and http://support.microsoft.com/kb/238972.
您可以使用办公自动化来完成此任务。您可以在http://support.microsoft.com/kb/196776和http://support.microsoft.com/kb/238972 上找到有关使用 C++ 实现办公自动化的常见问题解答。
Keep in mind that to do Office Automation with C++, you need to understand how to use COM.
请记住,要使用 C++ 进行办公自动化,您需要了解如何使用 COM。
Here are some examples of how to perform various tasks in word usign C++:
以下是如何在 word usign C++ 中执行各种任务的一些示例:
- http://support.microsoft.com/kb/220911/en-us
- http://support.microsoft.com/kb/238393/en-us
- http://support.microsoft.com/kb/238611/en-us
- http://support.microsoft.com/kb/220911/en-us
- http://support.microsoft.com/kb/238393/en-us
- http://support.microsoft.com/kb/238611/en-us
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.
这些示例中的大多数展示了如何使用 MFC 进行操作,但使用 COM 操作 Word 的概念是相同的,即使您直接使用 ATL 或 COM。
回答by Benoit
As posted as an answer to a similar question, I advise you to look at this pagewhere the author explains what solution he took to generate Word documents on a server, without MsWord being available, without automation or thirdparty libraries.
作为对类似问题的回答,我建议您查看此页面,其中作者解释了他在服务器上生成 Word 文档所采用的解决方案,没有 MsWord 可用,没有自动化或第三方库。
回答by Kasprzol
When you have the file and just want to print it, then look at this entryat Raymond Chen's blog. You can use the verb "print" for printing.
当您拥有文件并只想打印它时,请查看Raymond Chen 博客中的此条目。您可以使用动词“print”进行打印。
See the shellexecute msdn entryfor details.
有关详细信息,请参阅shellexecute msdn 条目。
回答by Peter Olsson
You can use automation to open MS Word (in background or foreground) and then send the needed commands.
您可以使用自动化打开 MS Word(在后台或前台),然后发送所需的命令。
A good starting place is the knowledge base article Office Automation Using Visual C++
一个好的起点是知识库文章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)
How To Use Visual C++ to Access DocumentProperties with Automation(标题说C++,但它是纯C)中提供了一些C源代码
回答by SteinNorheim
I have no experience from integrating with Microsoft Office, but I guess there are some APIs around that you can use for this.
我没有与 Microsoft Office 集成的经验,但我想您可以使用一些 API。
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).
但是,如果您想要完成的是打印格式化输出并将其导出到可以在 Word 中处理的文件的基本方法,您可能需要查看 RTF 格式。该格式非常易于学习,并且由 RtfTextBox(或者是 RichTextBox?)支持,它还具有一些打印功能。rtf 格式与 Windows 写字板 (write.exe) 使用的格式相同。
This also has the benefit of not depending on MS Office in order to work.
这还有一个好处是不依赖于 MS Office 来工作。
回答by ctype.h
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"
)
替换<filename>
为文件名。如果它包含空格,则必须用双引号括起来。(例如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:
这是一个 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