線上訂房服務-台灣趴趴狗聯合訂房中心
上鎖的討論區 上鎖的討論區 瀏覽次數:6247
推到 Plurk!
推到 Facebook!

Delphi&Word第一章Word部分

 
delphiwww
資深會員


發表:145
回覆:363
積分:368
註冊:2002-03-13

發送簡訊給我
#1 發表時間:2002-10-24 16:43:50 IP:202.145.xxx.xxx 未訂閱
如題 連上WordApplication 開啟舊檔或開新檔案 結束程式 列印與預覽列印 設定字型 插入文字 表格 段落 圖形 切換文章內容與頁眉/頁尾
附加檔案:22331_word01word.zip
領航天使
站長


發表:12216
回覆:4186
積分:4084
註冊:2001-07-25

發送簡訊給我
#2 發表時間:2002-10-24 21:09:58 IP:192.168.xxx.xxx 未訂閱
站長將Word檔中的文字列出,方便網友先預覽,但是完整內容請下載Word檔!
目錄        下一節
相關元件:WordApplication, WordDocumnet, WordFont, Opendialog, SaveDialog, Edit, Label, Button, GroupBox, Combobox, Pagecontrol, Tabsheet, RadioGroup, ToolBar, ToolButton, FontDialog。
一、Delphi與Word
在設計Delphi & Word時,筆者一直在想,大家可能需要什麼,因此整個畫面,並沒有特殊的設計,基本畫面設計如圖1。
 
圖1 程式畫面
連上WordApplication
A.        在FormCreate時,自動連上Word。
procedure TForm1.FormCreate(Sender: TObject);
begin
   try
      Wordapplication1.Connect;
      appcon:=true;
      ToolButton1.Down:=Wordapplication1.Visible;
      if ToolButton1.Down then
         ToolButton1.Caption:='顯示'
      else
         ToolButton1.Caption:='隱藏';
   except
      MessageDlg('無法連上Word', mtError, [mbOk], 0);
      Abort;
   end;
end;
B.        控制WordApplication的顯示或隱藏
procedure TForm1.ToolButton1Click(Sender: TObject);
begin
   if ToolButton1.Down then
      ToolButton1.Caption:='顯示'
   else
      ToolButton1.Caption:='隱藏';
   Wordapplication1.Visible := ToolButton1.Down;
end;
開啟舊檔或開新檔案
A.        開啟舊檔:
procedure TForm1.ToolButton2Click(Sender: TObject);
var Template:OleVariant;
begin
   if OpenDialog1.Execute then
   begin
      Template:=OpenDialog1.FileName;
      WordApplication1.Documents.Open(template,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam,EmptyParam,EmptyParam, EmptyParam,EmptyParam);
      whenopen(true);
   end;
end;
B.        開新檔案
procedure TForm1.ToolButton3Click(Sender: TObject);
var Template,NewTemplate,ItemIndex,_WordDocumentType,_Visible:OleVariant;
begin
   NewTemplate:=false;   //範本為True 文件為False
   Template := EmptyParam;
   _WordDocumentType:=wdTypeDocument;
   _Visible:=true; //沒有用處 可以直接填入emptyparam
   WordApplication1.Documents.Add(Template, NewTemplate, _WordDocumentType,_Visible);
   ItemIndex:=1;
//請注意,當WordDocument產生時,其ItemIndex為Insert,因此永遠唯一,另外對於Office的Array型態參數,大都是從1開始與Delphi從0開始不同
   WordDocument1.ConnectTo(WordApplication1.Documents.Item (ItemIndex));
   whenopen(true);
end;
C.        WhenOpen Procedure:主要是程式版面控制,
procedure TForm1.whenopen(open_type:boolean);
var s:string;
    i:integer;
begin
   if open_type then
   begin
      WordFont1.ConnectTo(WordApplication1.Selection.Font);
      edit2.text:=showfont; //顯示目前字型
   end;   
   Button12Click(nil);
   ToolButton2.Enabled:=not open_type;
   ToolButton3.Enabled:=not open_type;
   ToolButton5.Enabled:=open_type;
   ToolButton6.Enabled:=open_type;
   ToolButton7.Enabled:=open_type;
   GroupBox1.Enabled:=open_type;
   GroupBox2.Enabled:=open_type;
   GroupBox3.Enabled:=open_type;
   GroupBox4.Enabled:=open_type;
   GroupBox5.Enabled:=open_type;
   GroupBox5.Enabled:=open_type;
   PageControl1.Enabled:=open_type;
   if PageControl1.Enabled then
      PageControl1.ActivePageIndex:=0;
end;
結束程式
A.        關閉WordDocument與WordApplicaiton:在本程式中,主要是以不存檔的方式離開,自於其他的方式,請參考後續的Word與Excel的自我學習方式。
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var _savechange:OleVariant;
begin
   _savechange:=wdDoNotSaveChanges;
   try
      if not (ToolButton2.Enabled) then 
         WordDocument1.Close(_savechange);
   finally
      if appcon then
      begin
         if WordApplication1.Documents.Count=0 then
            WordApplication1.Quit;
         WordApplication1.Disconnect;
      end;
   end;
end;
B.        程式中使用者關閉WordDocument時
procedure TForm1.WordDocument1Close(Sender: TObject);
begin
   whenopen(false);
end;
C.        程式中使用者關閉WordApplication時
procedure TForm1.WordApplication1Quit(Sender: TObject);
begin
   appcon:=false;
end;
D.        另存新檔
procedure TForm1.ToolButton5Click(Sender: TObject);
var _FileName:OleVariant;
begin
   if SaveDialog1.Execute then
   begin
      _FileName:=SaveDialog1.FileName;
      WordDocument1.SaveAs(_FileName);
   end;
end;
列印與預覽列印
A.        列印
procedure TForm1.ToolButton6Click(Sender: TObject);
begin
   WordDocument1.PrintOut;
end;
B.        預覽列印
procedure TForm1.ToolButton7Click(Sender: TObject);
begin
   WordDocument1.PrintPreview;
end;    以下的內容開始與Word目前的文件有關,有些是以目前的
設定字型
Word本身的字型設定很多,本程式中,只使用Delphi所附的FontDialog來選取字型,讀者假如還要了解其他的字型設定,可以參考Delphi本所所附的WordApplication範例,就可以了解了,其存在Demos\ActiveX\OleAuto\SrvComp的目錄下。
A.        取得目前字型:
function TForm1.showfont:string;
begin
   WordFont1.ConnectTo(WordApplication1.Selection.Font);
   result:=WordFont1.NameFarEast;
   if not (result=WordFont1.Name) then
      result:=result ',' WordFont1.Name;
   if WordFont1.Underline=2 then
      result:=result ',底線';
   if WordFont1.Bold= 1 then
      result:=result ',粗體';
   if WordFont1.Italic=1 then
      result:=result ',斜體';
   result:=result ',' floattostr(WordFont1.Size) '號字';
end;
B.        字型選擇:
procedure TForm1.Button1Click(Sender: TObject);
var i:integer;
    s:string;
begin
   if not (edit2.Text='') then
   begin
      s:=edit2.text;
      i:=pos(',',s);
      if i>0 then
         FontDialog1.Font.Name:=copy(s,1,i-1);
      FontDialog1.Font.Style:=[];
      if pos('底線',s)>0 then
         FontDialog1.Font.Style:=FontDialog1.Font.Style [fsUnderline];
      if pos('粗體',s)>0 then
         FontDialog1.Font.Style:=FontDialog1.Font.Style [fsBold];
      if pos('斜體',s)>0 then
         FontDialog1.Font.Style:=FontDialog1.Font.Style [fsItalic];
      while (i>0) do
      begin
         s:=copy(s,i 1,length(s)-i);
         i:=pos(',',s);
      end;
      i:=pos('號字',s);
      if i>0 then
         FontDialog1.Font.Size:=strtoint(copy(s,1,i-1));
   end;
   if FontDialog1.Execute then
   begin
      s:=FontDialog1.Font.Name;
      if fsUnderline in FontDialog1.Font.Style then
         s:=s ',底線';
      if fsBold in FontDialog1.Font.Style then
         s:=s ',粗體';
      if fsItalic in FontDialog1.Font.Style then
         s:=s ',斜體';
      s:=s ',' floattostr(FontDialog1.Font.Size) '號字';
      edit2.text:=s;
   end;
end;
C.        設定:
procedure TForm1.Button2Click(Sender: TObject);
var s:string;
    i:integer;
begin
   if not (edit2.Text='') then
   begin
      WordFont1.ConnectTo(Wordapplication1.Selection.Font);
      s:=edit2.text;
      i:=pos(',',s);
      if i>0 then
         WordFont1.Name:=copy(s,1,i-1);
      if pos('底線',s)>0 then
         WordFont1.Underline := 2;
      if pos('粗體',s)>0 then
         WordFont1.Bold := 1;
      if pos('斜體',s)>0 then
         WordFont1.Italic := 1;
      while (i>0) do
      begin
         s:=copy(s,i 1,length(s)-i);
         i:=pos(',',s);
      end;
      i:=pos('號字',s);
      if i>0 then
         WordFont1.Size :=strtoint(copy(s,1,i-1));
   end;
end;
插入文字
在本段程式中,主要是插入程式後,將游標移到下一行,其共有兩個方法
A.        計算插入的字數,然後用Move移動
B.        直接移到下一行
你可以參考程式中Mark的部分,並自行測試一下。
procedure TForm1.Button3Click(Sender: TObject);
var Linecount:OleVariant;
    _Unit,_Extend:OleVariant;
begin
   WordApplication1.Selection.Range.InsertAfter(edit1.text #13);
//   WordDocument1.Range.InsertAfter(edit1.text #13);
//   Linecount:=length(edit1.text #13); 計算字數
   Linecount:=1;
   _Unit:=wdLine;
   _Extend:=wdMove;  //wdExtend
//   Wordapplication1.Selection.MoveEnd(Template,Linecount);  //移到行尾
//   Wordapplication1.Selection.Move(Template,Linecount);
   Wordapplication1.Selection.MoveDown(_Unit,LineCount,_Extend);
end;
表格
在表格中,本程式並未使用文字轉表格或表格轉文字的函數,不過本程式介紹
1.        插入表格
procedure TForm1.Button4Click(Sender: TObject);
var _DefaultTableBehavior,_AutoFitBehavior:OleVariant;
begin
   WordDocument1.Characters.Last.Select;
   if RadioGroup1.ItemIndex=1 then
      Wordapplication1.Selection.TypeParagraph;
   _DefaultTableBehavior:=wdWord9TableBehavior;
   _AutoFitBehavior:=wdAutoFitFixed;
   WordDocument1.Tables.Add(Wordapplication1.Selection.Range, SpinEdit1.Value,SpinEdit2.Value,_DefaultTableBehavior, _AutoFitBehavior);
   Button5.Enabled:=true;
end;
2.        輸入
procedure TForm1.Button5Click(Sender: TObject);
begin
   if not(Edit3.text='') then
      WordDocument1.Tables.Item (WordDocument1.Tables.Count). Cell(SpinEdit1.Value,SpinEdit2.Value).Range.Text:=Edit3.text;
end;
在輸入的部分,本程式並未控制行列的位置是否錯誤,請各位注意
3.        插入一列
procedure TForm1.Button7Click(Sender: TObject);
var _NumberRows:OleVariant;
begin
   _NumberRows:=1;
   WordApplication1.Selection.InsertRowsAbove(_NumberRows);
end;
段落
A.        取得所有段落名稱
procedure TForm1.Button12Click(Sender: TObject);
var i:integer;
    _index:OleVariant;
begin
   for i:=1 to WordDocument1.Styles.Count do
   begin
      _index:=i;
      ComboBox2.Items.Add(WordDocument1.Styles.Item(_index). NameLocal);
   end;
   if (ComboBox2.Text='') then
   begin
      ComboBox2.Text:='內文';
      ComboBox3.ItemIndex:=ord(WordApplication1.Selection. ParagraphFormat.Alignment);
   end;
end;
請注意在Word中,目前的段落為Paragraph,而所有段落的型態為Style
B.        設定目前段落名稱
procedure TForm1.ComboBox2CloseUp(Sender: TObject);
var _index,_prop:OleVariant;
    s:string;
begin
   _index:=combobox2.ItemIndex 1;
   _prop:=WordDocument1.Styles.Item(_index);
   WordApplication1.Selection.Set_Style(_prop);
   ComboBox3.ItemIndex:=ord(WordApplication1.Selection. ParagraphFormat.Alignment);
//   WordApplication1.Selection.ParagraphFormat.Alignment:= wdAlignParagraphCenter;
   edit2.text:=showfont;
end;
C.        更改目前段落的Alignment
procedure TForm1.ComboBox3CloseUp(Sender: TObject);
begin
   WordApplication1.Selection.ParagraphFormat. Alignment:=Combobox3.ItemIndex;
end;
圖形
圖形可以區分為文字方塊、檔案、線條與快取圖案四種,其中除了線條使用座標的左上與右下的位置外,其他都是使用左上角的座標與寬(Width)、高(Height)來決定大小與位置。因此程式中的Width(SpinEdit5.value)與Height(SpinEdit6.value)對於線條來說為EndX與EndY。
A.        文字方塊
procedure TForm1.Button6Click(Sender: TObject);
var _Left,_Top,_Width,_Height:single;
    _Anchor,_Replace,_Direction:OleVariant;
begin
   if not (Edit4.Text='') then
   begin
      _Left  :=SpinEdit3.Value;
      _Top   :=SpinEdit4.Value;
      _Width :=SpinEdit5.Value;
      _Height:=SpinEdit6.Value;
      _Anchor:=EmptyParam;
      _Replace:=wdReplaceNone;
      _Direction:=wdCollapseStart;
     WordDocument1.Shapes.AddTextbox (msoTextOrientationHorizontal,_Left,_Top, _Width,_Height, _Anchor).Select(_Replace);
      WordApplication1.Selection.ShapeRange.TextFrame. TextRange.Select;
      WordApplication1.Selection.Collapse(_Direction);
      WordApplication1.Selection.Text:=Edit4.text;
   end;
end;
B.        圖片檔案
a.        選取檔案
procedure TForm1.Button8Click(Sender: TObject);
var s:string;
begin
   s:=OpenDialog1.Filter;
   OpenDialog1.Filter:='ALL|*.*';
   if OpenDialog1.Execute then
      edit5.text:=Opendialog1.FileName;
   OpenDialog1.Filter:=s;
end;
b.        插入檔案
procedure TForm1.Button9Click(Sender: TObject);
var _Left,_Top,_Width,_Height,_LinkToFile, _SaveWithDocument,_Anchor:olevariant;
begin
   if Fileexists(edit5.text) then
   begin
      _LinkToFile:=false;
      _SaveWithDocument:=true;
      _Anchor:=EmptyParam;
      if SpinEdit3.Value=0 then
         _Left  :=EmptyParam
      else
         _Left  :=SpinEdit3.Value;
      if SpinEdit4.Value=0 then
         _Top   :=EmptyParam
      else
         _Top   :=SpinEdit4.Value;
      if SpinEdit5.Value=0 then
         _Width :=EmptyParam
      else
         _Width :=SpinEdit5.Value;
      if SpinEdit6.Value=0 then
         _Height:=EmptyParam
      else
         _Height:=SpinEdit6.Value;
      Worddocument1.Shapes.AddPicture(edit5.text,_LinkToFile, _SaveWithDocument,_Left,_Top,_Width,_Height, _Anchor)
   end;
end;
當沒有輸入_Left、_Top、_Width及_Height時,表示為目前游標所在的位置,填入EmptyParam即可
C.        線條箭頭
procedure TForm1.Button10Click(Sender: TObject);
var _BeginX,_BeginY,_EndX,_EndY:Single;
    _Anchor,_Replace:OleVariant;
begin
   _BeginX:=SpinEdit3.Value;
   _BeginY:=SpinEdit4.Value;
   _EndX  :=SpinEdit5.Value;
   _EndY  :=SpinEdit6.Value;
   _Anchor:=EmptyParam;
   _Replace:=wdReplaceNone;
   WordDocument1.Shapes.AddLine(_BeginX,_BeginY,_EndX,_EndY, _Anchor).Select(_Replace)
   if CheckBox1.Checked then
   begin
      WordApplication1.Selection.ShapeRange.Line. BeginArrowheadStyle:= msoArrowheadTriangle;
      WordApplication1.Selection.ShapeRange.Line. BeginArrowheadLength:= msoArrowheadLengthMedium;
      WordApplication1.Selection.ShapeRange.Line. BeginArrowheadWidth:= msoArrowheadWidthMedium;
   end;
   if CheckBox2.Checked then
   begin
      WordApplication1.Selection.ShapeRange.Line. EndArrowheadStyle:= msoArrowheadTriangle;
      WordApplication1.Selection.ShapeRange.Line. EndArrowheadLength:= msoArrowheadLengthMedium;
      WordApplication1.Selection.ShapeRange.Line. EndArrowheadWidth:= msoArrowheadWidthMedium;
   end;
end;
D.        快取圖案:對於每一種圖案的代號,筆者已經複製到Combobox1.Item中,各位讀者Try一下程式就可以明瞭,不過有些圖案,另外還有其他控制,方式請參考Word與Excel的自我學習方式。
procedure TForm1.Button11Click(Sender: TObject);
var _Left,_Top,_Width,_Height:single;
    _Anchor,_Replace:OleVariant;
begin
   _Left  :=SpinEdit3.Value;
   _Top   :=SpinEdit4.Value;
   _Width :=SpinEdit5.Value;
   _Height:=SpinEdit6.Value;
   _Anchor:=EmptyParam;
   _Replace:=wdReplaceNone;
   WordDocument1.Shapes.AddShape(ComboBox1.ItemIndex 1, _Left,_Top,_Width,_Height,_Anchor).Select(_Replace);
end;
切換文章內容與頁眉/頁尾
筆者利用Pagecontro1來製作切換內文與頁眉頁尾的位置,其中插入圖形時,需要更改插入的方式,因此筆者在修改原來少部分的程式。
A.        切換:
procedure TForm1.PageControl1Change(Sender: TObject);
begin
   case Pagecontrol1.ActivePageIndex of
      1:begin  //頁眉
           If WordDocument1.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
              WordDocument1.ActiveWindow.Panes.Item(2).Close;
           If (WordDocument1.ActiveWindow.ActivePane.View.Type_ = wdNormalView) Or (WordDocument1.ActiveWindow.ActivePane.View.Type_ = wdOutlineView) Then
              WordDocument1.ActiveWindow.ActivePane. View.Type_:=wdPrintView;
           WordDocument1.ActiveWindow.ActivePane.View.SeekView:= wdSeekCurrentPageHeader;
        end;
      2:begin  //頁首
           If WordDocument1.ActiveWindow.View.SplitSpecial <> wdPaneNone Then
              WordDocument1.ActiveWindow.Panes.Item(2).Close;
           If (WordDocument1.ActiveWindow.ActivePane.View.Type_ = wdNormalView) Or (WordDocument1.ActiveWindow.ActivePane.View.Type_ = wdOutlineView) Then
              WordDocument1.ActiveWindow.ActivePane. View.Type_:=wdPrintView;
           WordDocument1.ActiveWindow.ActivePane.View.SeekView:= wdSeekCurrentPageFooter;
        end;
   else  //內文
      WordDocument1.ActiveWindow.ActivePane.View.SeekView:= wdSeekMainDocument;
   end;
end;
B.        插入圖形
將WordDocument1.Shapes改成WordApplication1.Selection.HeaderFooter即可,因此對搭入文字方塊來說,改成如下:
if PageControl1.ActivePageIndex=0 then
         WordDocument1.Shapes.AddTextbox (msoTextOrientationHorizontal,_Left,_Top, _Width, _Height,_Anchor).Select(_Replace)
      else
         WordApplication1.Selection.HeaderFooter.Shapes.AddTextbox (msoTextOrientationHorizontal,_Left,_Top, _Width, _Height,_Anchor).Select(_Replace);
目錄        下一節
------
~~~Delphi K.Top討論區站長~~~
yandav
一般會員


發表:16
回覆:20
積分:7
註冊:2002-10-05

發送簡訊給我
#3 發表時間:2002-11-08 10:13:11 IP:210.200.xxx.xxx 未訂閱
請問: 為何我開啟 word.dpr 時,會出現如下之錯誤訊息 Error reading Form1.Font.Name:Invalid property value. Ignore the error and continue? NOTE: Ignore the error may cause components to be deleted or property values to be lost
delphiwww
資深會員


發表:145
回覆:363
積分:368
註冊:2002-03-13

發送簡訊給我
#4 發表時間:2002-11-08 10:20:46 IP:202.145.xxx.xxx 未訂閱
我的Word程式字型是標楷體,你可能沒有, 另外是D6的版本
引言: 請問: 為何我開啟 word.dpr 時,會出現如下之錯誤訊息 Error reading Form1.Font.Name:Invalid property value. Ignore the error and continue? NOTE: Ignore the error may cause components to be deleted or property values to be lost
yandav
一般會員


發表:16
回覆:20
積分:7
註冊:2002-10-05

發送簡訊給我
#5 發表時間:2002-11-08 15:33:52 IP:210.200.xxx.xxx 未訂閱
謝謝! 我是D5的版本, 有辦法正常開啟嗎 ?
delphiwww
資深會員


發表:145
回覆:363
積分:368
註冊:2002-03-13

發送簡訊給我
#6 發表時間:2002-11-12 08:38:09 IP:202.145.xxx.xxx 未訂閱
你Download Word回去依照上面說明,重新作一下應該就可以了
引言: 謝謝! 我是D5的版本, 有辦法正常開啟嗎 ?
系統時間:2024-05-18 18:49:52
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!