delphi操作word

來源:互聯網
上載者:User

delphi操作word

一、Delphi程式啟動Word
採用CreateOleObjects的方法來啟動Word,調用VBA代碼,具體實現過程為:
首先使用GetActiveOleObject('Word.Application')判斷當前記憶體中是否存在Word程式,如果存在,
則直接連接,如果沒有Word程式,則使用CreateOleObject('Word.Application')啟動Word

二、Delphi程式建立Word文稿
格式:WordDocuments.Add(Template,NewTemplate,DocumentType,Visible)
Template: 使用模板的名稱,
NewTemplate: 建立文檔的類型,True表示為模板,False表示為文檔
DocumentType: 文件類型,預設為空白文檔
Visible: 打撈的視窗是否可見

舉例:Doc_Handle:=Word_Ole.Documents.Add(Template:='C:/Temlate.dot',NewTemplate:=False);

三、Delphi程式開啟Word文稿
格式:WordDocuments.Open(FileName,ConfirmConversions,ReadOnly,PassWordDocument,
PasswordTemplate,Revent,WritePasswordDocument,WritePassWordTemplate,
Format,Encoding,Visible)

FileName: 文檔名(包含路徑)
Confirmconversions: 是否顯示檔案轉換對話方塊
ReadOnly: 是否以唯讀方式開啟文檔
AddToRecentFiles: 是否將檔案添加到"檔案"菜單底部的最近使用檔案清單中
PassWordDocument: 開啟此文檔時所需要的密碼
PasswordTemplate: 開啟此模板時所需要的密碼
Revert: 如果文檔已經,是否重新開啟文檔
WritePasswordDocument: 儲存對文檔更改時所需要的密碼
WritePasswordTemplate: 儲存對模板變更時所需要的密碼
Format: 開啟文檔時所需使用的檔案轉換器
Encoding: 所使用的文檔字碼頁
Visible: 開啟文檔的視窗是否可見

舉例:
Doc_Handle:=Word_Ole.Documents.open(FileName:=Doc_File,ReadOnly:=False,
AddToRecentFiles:=False);

四、Delphi程式儲存Word文稿
格式:WordDocuments.SaveAs(FileName, FileFormat, LockComments, Password,
AddToRecentFiles, WritePassword, ReadOnlyRecommended,
EmbedTrueTypeFonts, SaveNativePictureFormat, SaveFormsData,
SaveAsAOCELetter)

FileName: 檔案名稱。預設為當前檔案夾和檔案名稱。
FileFormat 文檔儲存的格式。
LockComments 如果為 True,則此文檔只允許進行批註。
Password 開啟文檔時的口令。
AddToRecentFiles 如果為True,則將文檔添至"檔案"菜單中最近使用的文檔列表中。
WritePassword 儲存對文檔的修改所需的口令。
ReadOnlyRecommended 如果為 True,在每次開啟文檔時,Word 將建議使用者採用唯讀方式。
EmbedTrueTypeFonts 如果為 True,則將文檔與 TrueType 字型一起儲存。
SaveNativePictureFormat 如果為 True,則從其他系統平台(例如 Macintosh)匯入的圖形僅儲存其 Windows 版本。
SaveFormsData 如果為 True,則將表單中使用者輸入的資料存為一條資料記錄。
SaveAsAOCELetter 如果文檔包含一個附加,當此屬性值為 True 時,將文檔存為一篇 AOCE 信箋(同時儲存郵件)。

舉例:
Word_Ole.Documents.SaveAs(FileName:=Doc_File,FileFormat=wdFormatDocument,
AddToRecentFiles=False);

設定紙張大小

  Wordapplication1.ActiveDocument.PageSetup.PageWidth:=XXX;

  Wordapplication1.ActiveDocument.PageSetup.PageHeight:=XXX;

  Wordapplication1.ActiveDocument.PageSetup.LeftMargin := XX;

  //設定左邊距

  Wordapplication1.ActiveDocument.PageSetup.rightMargin := XX; 

插入頁碼

  var fpage,pagea:olevariant;

  fpage:=true;

  pagea:=wdAlignPageNumberCenter;

  Wordapplication1.activedocument.sections.item(1).Footers.item(1).PageNumbers.Add(pagea,fpage);

設定頁面橫向列印

  s:=Wordapplication1.selection.start;

  e:=Wordapplication1.selection.start;

  aa:=wdSectionBreakNextPage;

  Wordapplication1.ActiveDocument.Range(s,e).InsertBreak(aa);

  Wordapplication1.Selection.Start:=Wordapplication1.Selection.Start + 1;

  s:=Wordapplication1.Selection.start;

  e:=Wordapplication1.ActiveDocument.Content.End_;

  Wordapplication1.ActiveDocument.Range(S,e).PageSetup.Orientation:=wdOrientLandscape;

設定字型、字型大小

  Wordapplication1.Selection.Font.Size:=18;

  Wordapplication1.Selection.Font.Name := '黑體';

  Wordapplication1.Selection.TypeParagraph;

  Wordapplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphCenter;

  Wordapplication1.Selection.TypeParagraph;

  Wordapplication1.Selection.TypeText(dbedit4.text);

插入表格

  Wordapplication1.Selection.Font.Size :=10;

  adoquery2.Active:=false;

  adoquery2.active:=true;

  doc:=Wordapplication1.activedocument;

  counts:=adoquery2.RecordCount;

  //記錄數決定表格的行數

  t:=doc.tables.Add(Wordapplication1.selection.range,counts+1,5);//5列

  t.cell(1,1).range.text:= '單位';

  t.Cell(1,1).Width:=120;

  t.cell(1,1).range.Paragraphs.Alignment:= wdAlignParagraphCenter;

  t.cell(1,2).range.text:= '姓名';

   ... ...

  //依次寫入其他欄位的表頭

  for i:=2 to counts+1 do

  begin

  t.cell(i,1).range.text:=adoquery2.field

  byname('dw').asstring;

  t.Cell(i,1).Width:=120;

  t.cell(i,1).range.Paragraphs.Alignment:=

   wdAlignParagraphCenter;

  t.cell(i,2).range.text:=adoquery2.field

  byname('xm').asstring;

  ... ...

  Adoquery2.next;

  End;

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.