2011-12-06 96 views
4

我需要使用特定模板從Delphi創建Word文檔,將文檔另存爲具有給定名稱的Word文檔,然後保存具有相同名稱的同一文檔但作爲PDF文件。 (在Word 2007中可以使用SaveAsPdf ...插件完成,在Word 2010中是內置功能)從Delphi創建Word文檔,另存爲Doc和PDF

這怎麼可以用Delphi XE來完成?

+0

我用vbscript做。這會有幫助嗎? –

+1

@TLama沒有任何意圖,對我來說,離開德爾菲已經很長時間了(當然帶着眼淚),我認爲它應該是所有情況。 – Ali

+1

你已經有什麼?你知道如何在Word中打開文檔嗎? 什麼Word版本將在客戶端上可用?如果是Word 2010,它不應該很困難。只需使用COM打開文檔並將其保存爲pdf即可。 – Birger

回答

6

首先使用「組件」|「Microsoft Word 12對象」(MS Word 2007)類型庫導入項目。導入組件菜單項。然後,您可以使用此示例代碼加載MS Word文件,並使用內部PDF轉換器將其另存爲PDF。如果您使用的是Microsoft Word 2010中,加載它的類型庫而不是Word 2007中

unit fMain; 

interface 

uses 
    Windows, SysUtils, Variants, Classes, Controls, Forms, Dialogs, StdCtrls, 
    Word_TLB; 

type 
    TfrmMain = class(TForm) 
    btnLoad: TButton; 
    btnSaveAs: TButton; 
    FileOpenDialog1: TFileOpenDialog; 
    FileSaveDialog1: TFileSaveDialog; 
    procedure btnLoadClick(Sender: TObject); 
    procedure FormClose(Sender: TObject; var Action: TCloseAction); 
    procedure btnSaveAsClick(Sender: TObject); 
    private 
    FWordApp : WordApplication; 
    FWordDoc : WordDocument; 
    procedure InitializeApp; 
    procedure FinalizeApp; 
    function LoadFile(const AFileName: string): WordDocument; 
    procedure SaveAsPdf(ADocument: WordDocument; const AFileName: string); 
    public 
    { Public declarations } 
    end; 

var 
    frmMain: TfrmMain; 

implementation 

uses ComObj; 

{$R *.dfm} 

procedure TfrmMain.btnLoadClick(Sender: TObject); 
begin 
    if FileOpenDialog1.Execute then 
    FWordDoc := LoadFile(FileOpenDialog1.FileName); 
end; 

procedure TfrmMain.btnSaveAsClick(Sender: TObject); 
begin 
    if FileSaveDialog1.Execute then 
    begin 
    if Assigned(FWordDoc) then 
     SaveAsPdf(FWordDoc, FileSaveDialog1.FileName); 
    end; 
end; 

procedure TfrmMain.FinalizeApp; 
var 
    SaveChanges: OleVariant; 
begin 
    if Assigned(FWordApp) then 
    begin 
    SaveChanges := False; 
    FWordApp.Quit(SaveChanges, EmptyParam, EmptyParam); 
    FWordApp := nil; 
    end; 
end; 

procedure TfrmMain.FormClose(Sender: TObject; var Action: TCloseAction); 
begin 
    FinalizeApp; 
end; 

procedure TfrmMain.InitializeApp; 
begin 
    FWordApp := createOleObject('Word.Application') as WordApplication; 
    if Assigned(FWordApp) then 
    begin 
    FWordApp.Visible := False; 
    end 
    else 
    raise Exception.Create('Cannot initialize Word application'); 
end; 

function TfrmMain.LoadFile(const AFileName: string): WordDocument; 
var 
    FileName: OleVariant; 
    Doc : WordDocument; 
begin 
    if not Assigned(FWordApp) then 
    InitializeApp; 

    FileName := AFileName; 
    Doc := FWordApp.Documents.Open(FileName, EmptyParam, EmptyParam, EmptyParam, 
           EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
           EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
           EmptyParam, EmptyParam, EmptyParam, EmptyParam); 
    Result := Doc; 
end; 

procedure TfrmMain.SaveAsPdf(ADocument: WordDocument; const AFileName: string); 
var 
    FileName, 
    FileFormat : OleVariant; 
begin 
    if Assigned(ADocument) then 
    begin 
    FileName := AFileName; 
    FileFormat := wdFormatPDF; 
    ADocument.SaveAs(FileName, FileFormat, EmptyParam, EmptyParam, EmptyParam, 
        EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
        EmptyParam, EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
        EmptyParam); 
    end; 
end; 

end. 

我剛寫的代碼並運行它一次,它的工作原理,但我還沒有徹底的測試,所以可能會有一些毛刺。

+0

OP明確要求使用特定模板創建新的Word文檔...? –

+1

我認爲這是對讀者的一個練習:-) – Birger

+0

@Marjan Venema,我從標題和描述中得到的結果是,他/她在以特定格式保存文檔時遇到問題,而不是創建新文件。我添加了LoadFile()以便能夠測試代碼並確保它實際上將給定的MS Word文檔轉換爲PDF。 – vcldeveloper

0

這段代碼對我來說是訣竅,但正如你所看到的,我必須立即保存文檔,並使用ShellExecute重新打開它。如果沒有,我會提前提到'RPC服務器不可用'錯誤。

procedure TfrmJobsearchAdverts.FileCreate; 
var 
    TempName: OleVariant; 
    FileDoc: OleVariant; 
    FileFormat: OleVariant; 
    Doc : WordDocument; 
    WordTemplate: string; 
    WordFile: string; 
    BookName: OleVariant; 
begin 
    SaveRecord; 
    WordFile := Advert.CreateDocname(qryAdverts.FieldByName('fldadvert_date').AsDateTime); 
    WordTemplate := JobsearchTemplate(Self); 
    if (WordFile <> '') and (WordTemplate <> '') then 
    begin 
     Advert.SaveDocname(qryAdverts.FieldByName('fldadvert_guid').AsString, WordFile); 
     if not Assigned(fWordApp) then 
     InitializeWord; 
     TempName := WordTemplate; 
     FileDoc := WordFile; 
     Doc := FWordApp.Documents.Add(TempName, EmptyParam, EmptyParam, EmptyParam); 
     BookName := 'fldCompany'; 
     if Doc.Bookmarks.Exists(BookName) then 
     Doc.Bookmarks.Item(BookName).Range.Text := Company.Name; 
     BookName := 'fldAddress1'; 
     if Doc.Bookmarks.Exists(BookName) then 
     Doc.Bookmarks.Item(BookName).Range.Text := Company.Address1; 
     BookName := 'fldAddress2'; 
     if Doc.Bookmarks.Exists(BookName) then 
     Doc.Bookmarks.Item(BookName).Range.Text := Company.Address2; 
     BookName := 'fldZip'; 
     if Doc.Bookmarks.Exists(BookName) then 
     Doc.Bookmarks.Item(BookName).Range.Text := Company.Zip; 
     BookName := 'fldCity'; 
     if Doc.Bookmarks.Exists(BookName) then 
     Doc.Bookmarks.Item(BookName).Range.Text := Company.City; 
     FileFormat := wdFormatDocument; 
     Doc.SaveAs(FileDoc, EmptyParam, EmptyParam, EmptyParam, 
       EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
       EmptyParam, EmptyParam, EmptyParam, EmptyParam, 
       EmptyParam, EmptyParam, EmptyParam, EmptyParam); 
     FinalizeWord; 
     Files.ExecuteAndWait(WordFile); 
    end; 
end; 
+0

如果您需要創建並保存多個文檔,則不必爲每個文檔調用InitializeWord和FinializeWord;只需初始化一次Word,並在您不再需要單詞應用程序時完成一次。當你完成一個文檔時,通過調用它的Close方法關閉它。 – vcldeveloper