2008-09-28 146 views
16

我有三個相關的問題。從C++創建,打開和打印word文件

我想用C++創建一個名稱爲word的文件。我希望能夠將打印命令發送到此文件,以便打印文件時用戶不必打開文檔並手動執行,而且我希望能夠打開文檔。打開文檔應該打開單詞,然後打開文件。

回答

0

我沒有與Microsoft Office集成的經驗,但我想有一些API可以用於此目的。

但是,如果您想要完成的是打印格式化輸出並將其導出爲可以在Word中處理的文件的基本方式,則可能需要查看RTF格式。這種格式很容易學習,並且由RtfTextBox(或者它是RichTextBox?)支持,它也具有一些打印功能。 rtf格式與Windows寫字板(write.exe)使用的格式相同。

這也有利於不依賴MS Office工作。

15

您可以使用Office Automation執行此任務。您可以通過C++在http://support.microsoft.com/kb/196776http://support.microsoft.com/kb/238972找到關於Office自動化的常見問題解答。

請記住,要使用C++執行Office自動化,您需要了解如何使用COM。

這裏是如何執行的話usignÇ各種任務的一些例子++:

大部分樣品的展示瞭如何使用做MFC,但使用COM來操作Word的概念是相同的,即使您直接使用ATL或COM。

+0

這是一個很好的回答問題。我想指出其他問題與其他問題相關的其他問題,這不適用於服務器端用戶或沒有用戶登錄的情況。這不是這個問題的情況,但還有其他問題鏈接這裏是關於服務器端使用的,在這些情況下,Office自動化並不合適。對於在桌面上打印,它非常適合。 – 2011-04-28 16:49:47

2

當你有文件,只是想打印出來,然後在Raymond Chen的博客上看this entry。您可以使用動詞「打印」進行打印。

有關詳細信息,請參閱shellexecute msdn entry

4

作爲對similar question的回答發佈,我建議您查看this page,作者解釋了他在服務器上生成Word文檔所用的解決方案,沒有MsWord可用,沒有自動化或第三方庫。

0

我的這個解決方案是使用下面的命令:

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