从 C++ 创建、打开和打印 Word 文件
我有三个相关问题。
我想创建一个名称为 C++ 的 Word 文件。 我希望能够将打印命令发送到该文件,以便在用户不必打开文档并手动执行操作的情况下打印该文件,并且我希望能够打开该文档。 打开文档只需打开Word,然后再打开该文件。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用 Office Automation 来完成此任务。 您可以在 http://support.microsoft.com/kb 找到有关使用 C++ 进行办公自动化的常见问题解答/196776 和 http://support.microsoft.com/kb/238972 。
请记住,要使用 C++ 进行办公自动化,您需要了解如何使用 COM。
以下是如何使用 C++ 在 Word 中执行各种任务的一些示例:
这些示例中的大多数都展示了如何使用 MFC 执行此操作,但使用 COM 操作 Word 的概念是相同的,即使您直接使用 ATL 或 COM。
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.
作为类似问题的答案发布,我建议您查看此页面,其中作者解释了他采取的解决方案在服务器上生成 Word 文档,无需使用 MsWord,无需自动化或第三方库。
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.
当您拥有该文件并且只想打印它时,请查看 此条目位于 Raymond Chen 的博客。 您可以使用动词“print”来进行打印。
有关详细信息,请参阅 shellexecute msdn 条目。
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.
您可以使用自动化功能打开 MS Word(在后台或前台),然后发送所需的命令。
一个很好的起点是知识库文章 使用 Visual C++ 的办公自动化
一些 C 源代码是可以在 How To Use Visual C++ to Access DocumentProperties with Automation 中找到(标题说的是 C++,但是很简单 C)
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)
我没有与 Microsoft Office 集成的经验,但我想您可以使用一些 API 来实现此目的。
但是,如果您想要完成的是打印格式化输出并将其导出到可以在 Word 中处理的文件的基本方法,您可能需要研究 RTF 格式。 该格式非常简单易学,并且受 RtfTextBox(或者是 RichTextBox?)支持,并且还具有一些打印功能。 rtf 格式与 Windows 写字板 (write.exe) 使用的格式相同。
这还有一个好处,就是不依赖 MS Office 来工作。
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.
我的解决方案是使用以下命令:
这允许用户指定打印机,不。 副本等。
将
替换为文件名。 如果包含空格,则必须用双引号引起来。 (例如file.rtf
、“A File.docx”
)它可以放置在系统调用中,如下所示:
这是一个 C++ 头文件,其中包含处理此问题的函数如果您经常使用它,则不必记住所有开关:
My solution to this is to use the following command:
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:
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: