Delphi中编辑word


Delphi中编辑word
2011年06月20日
   //启动Word 
  try 
  wordapplication1.connect; 
  except 
  messagedlg('word may not be installed', mterror, [mbok], 0); 
  abort; 
  end; 
  //打开文档 
  procedure TForm1.OpenWord(Sfile: string); 
  var 
  FileName, ConfirmConversions, ReadOnly, AddToRecentFiles, 
  PasswordDocument, PasswordTemplate, Revert, 
  WritePasswordDocument, WritePasswordTemplate, Format, 
  Encoding, Visible, OpenAndRepair, 
  DocumentDirection, NoEncodingDialog: OleVariant; 
  ItemIndex: OleVariant; 
  begin 
  FileName := sfile; 
  ConfirmConversions := False; 
  ReadOnly := False; 
  AddToRecentFiles := False; 
  PasswordDocument := ''; 
  PasswordTemplate := ''; 
  Revert := True; 
  WritePasswordDocument := ''; 
  WritePasswordTemplate := ''; 
  Format := wdOpenFormatDocument; 
  //打开文件 
  WordApplication1.Documents.Open(FileName, ConfirmConversions, 
  ReadOnly, AddToRecentFiles, PasswordDocument, PasswordTemplate, 
  Revert, WritePasswordDocument, WritePasswordTemplate, Format, 
  Encoding, Visible, OpenAndRepair, 
  DocumentDirection, NoEncodingDialog); 
  ItemIndex := 1; 
  WordDocument1.ConnectTo(WordApplication1.Documents .Item(ItemIndex)); 
  WordApplication1.Options.CheckSpellingAsYouType := False; 
  WordApplication1.Options.CheckGrammarAsYouType := False; 
  WordApplication1.Connect; 
  WordApplication1.Visible := True; 
  WordApplication1.Disconnect; 
  end; 
  //设置页眉页脚 
  procedure TForm1.Button5Click(Sender: TObject); 
  var 
  mmm, nnn, aaa: OleVariant; 
  begin 
  mmm := wdLine; 
  nnn := 1; 
  aaa := wdFieldPage; 
  worddocument1.ActiveWindow.ActivePane.View.SeekVie w := wdSeekCurrentPageHeader; 
  worddocument1.activewindow.Selection.Move(mmm, nnn); 
  worddocument1.activewindow.Selection.ParagraphForm at.Alignment := wdAlignParagraphCenter; 
  worddocument1.activewindow.Selection.InsertAfter('第'); 
  mmm := wdCharacter; 
  worddocument1.activewindow.Selection.Move(mmm, nnn); 
  worddocument1.activewindow.Selection.Fields.Add(wo rddocument1.activewindow.Selection.Range, aaa, mmm, nnn); 
  aaa := wdFieldNumPages; 
  worddocument1.activewindow.Selection.InsertAfter('页/第'); 
  worddocument1.activewindow.Selection.Move(mmm, nnn); 
  worddocument1.activewindow.Selection.Fields.Add(wo rddocument1.activewindow.Selection.Range, aaa, mmm, nnn); 
  worddocument1.activewindow.Selection.InsertAfter('页'); 
  mmm := wdWord; 
  nnn := 2; 
  worddocument1.activewindow.Selection.Move(mmm, nnn); 
  worddocument1.activewindow.activepane.selection.in sertafter(#13+'武汉大学中南医院'); 
  //结束编辑 
  worddocument1.ActiveWindow.ActivePane.View.SeekVie w := wdSeekMainDocument; 
  end; 
  //替换页眉 
  procedure TForm1.Button6Click(Sender: TObject); 
  var 
  FindText, MatchCase, MatchWholeWord, MatchWildcards, MatchSoundsLike, 
  MatchAllWordForms, Forward, Wrap, Format, ReplaceWith, Replace, 
  MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl:olevariant; 
  i:Single; 
  begin 
  matchcase := false; 
  matchwholeword := true; 
  matchwildcards := false; 
  matchsoundslike := false; 
  matchallwordforms := false; 
  forward := true; 
  wrap := wdfindcontinue; 
  format := false; 
  replace := true; 
  MatchKashida:=True; 
  MatchDiacritics:=True; 
  MatchAlefHamza:=True; 
  MatchControl:=True; 
  findtext := '武汉'; //' ' 
  replacewith := '1234'; 
  worddocument1.ActiveWindow.ActivePane.View.SeekVie w := wdSeekCurrentPageHeader; 
  worddocument1.activewindow.activepane.selection.Fi nd.Execute(Findtext, matchcase, matchwholeword, 
  matchwildcards, matchsoundslike, matchallwordforms, forward, 
  wrap, format, replacewith, replace,MatchKashida, MatchDiacritics, MatchAlefHamza, MatchControl); 
  //结束编辑 
  worddocument1.ActiveWindow.ActivePane.View.SeekVie w := wdSeekMainDocument; 
  end; 
  ______________ 我以前写的,分还是给我吧 
  unit UFS_Word; 
  interface 
  uses 
  Windows, Messages, SysUtils,  Classes, Graphics, Controls, Forms, 
  Dialogs, StdCtrls, comobj,ActiveX,variants; 
  type 
  WdRowAlignment = TOleEnum; 
  const 
  wdAlignRowLeft = $00000000; 
  wdAlignRowCenter = $00000001; 
  wdAlignRowRight = $00000002; 
  // Constants for enum WdTabAlignment 
  type 
  WdTabAlignment = TOleEnum; 
  const 
  wdAlignTabLeft = $00000000; 
  wdAlignTabCenter = $00000001; 
  wdAlignTabRight = $00000002; 
  wdAlignTabDecimal = $00000003; 
  wdAlignTabBar = $00000004; 
  wdAlignTabList = $00000006; 
  // Constants for enum WdVerticalAlignment 
  type 
  WdVerticalAlignment = TOleEnum; 
  const 
  wdAlignVerticalTop = $00000000; 
  wdAlignVerticalCenter = $00000001; 
  wdAlignVerticalJustify = $00000002; 
  wdAlignVerticalBottom = $00000003; 
  // Constants for enum WdCellVerticalAlignment 
  type 
  WdCellVerticalAlignment = TOleEnum; 
  const 
  wdCellAlignVerticalTop = $00000000; 
  wdCellAlignVerticalCenter = $00000001; 
  wdCellAlignVerticalBottom = $00000003; 
  const 
  wdCollapseStart = $00000001; 
  wdCollapseEnd = $00000000; 
  const 
  wdCharacter = $00000001; 
  wdWord = $00000002; 
  wdSentence = $00000003; 
  wdParagraph = $00000004; 
  wdLine = $00000005; 
  wdStory = $00000006; 
  wdScreen = $00000007; 
  wdSection = $00000008; 
  wdColumn = $00000009; 
  wdRow = $0000000A; 
  wdWindow = $0000000B; 
  wdCell = $0000000C; 
  wdCharacterFormatting = $0000000D; 
  wdParagraphFormatting = $0000000E; 
  wdTable = $0000000F; 
  wdItem = $00000010; 
  const 
  wdAlignParagraphLeft        = $00000000; 
  wdAlignParagraphCenter      = $00000001; 
  wdAlignParagraphRight      = $00000002; 
  wdAlignParagraphJustify    = $00000003; 
  wdAlignParagraphDistribute  = $00000004; 
  wdAlignParagraphJustifyMed  = $00000005; 
  wdAlignParagraphJustifyHi  = $00000007; 
  wdAlignParagraphJustifyLow  = $00000008; 
  wdAlignParagraphThaiJustify = $00000009; 
  const 
  wdAdjustNone = $00000000; 
  wdAdjustProportional = $00000001; 
  wdAdjustFirstColumn = $00000002; 
  wdAdjustSameWidth = $00000003; 
  const 
  wdRowHeightAuto = $00000000; 
  wdRowHeightAtLeast = $00000001; 
  wdRowHeightExactly = $00000002; 
  const 
  wdLineStyleNone = $00000000; 
  wdLineStyleSingle = $00000001; 
  wdLineStyleDot = $00000002; 
  wdLineStyleDashSmallGap = $00000003; 
  wdLineStyleDashLargeGap = $00000004; 
  wdLineStyleDashDot = $00000005; 
  wdLineStyleDashDotDot = $00000006; 
  wdLineStyleDouble = $00000007; 
  wdLineStyleTriple = $00000008; 
  wdLineStyleThinThickSmallGap = $00000009; 
  wdLineStyleThickThinSmallGap = $0000000A; 
  wdLineStyleThinThickThinSmallGap = $0000000B; 
  wdLineStyleThinThickMedGap = $0000000C; 
  wdLineStyleThickThinMedGap = $0000000D; 
  wdLineStyleThinThickThinMedGap = $0000000E; 
  wdLineStyleThinThickLargeGap = $0000000F; 
  wdLineStyleThickThinLargeGap = $00000010; 
  wdLineStyleThinThickThinLargeGap = $00000011; 
  wdLineStyleSingleWavy = $00000012; 
  wdLineStyleDoubleWavy = $00000013; 
  wdLineStyleDashDotStroked = $00000014; 
  wdLineStyleEmboss3D = $00000015; 
  wdLineStyleEngrave3D = $00000016; 
  wdLineStyleOutset = $00000017; 
  wdLineStyleInset = $00000018; 
  Type TwdAligment =( 
  wdAlign_ParagraphLeft          , 
  wdAlign_ParagraphCenter        , 
  wdAlign_ParagraphRight          , 
  wdAlign_ParagraphJustify        , 
  wdAlign_ParagraphDistribute    , 
  wdAlign_ParagraphJustifyMed    , 
  wdAlign_ParagraphJustifyHi      , 
  wdAlign_ParagraphJustifyLow    , 
  wdAlign_ParagraphThaiJustify 
  ); 
  type 
  TFS_word =class 
  protected 
  FsERR:widestring; 
  Word, Doc:OleVariant; 
  FisOpened  :boolean; 
  Table:oleVariant; 
  nRows,nCols:integer; 
  procedure    Table_Cell_SetRowAlignment( row,col:integer;//设置当前输出格式 
  wdAlignParagraph:Integer); 
  procedure    Table_Cell_SetVerticalAlignment( row,col:integer; 
  wdAlignParagraph:Integer); //该属性返回或设置文档或某节中每一页上文字的垂直对齐方 
  procedure    Table_LoadPicture_To_Cell(//输出图片 
  row,col:integer; 
  sFileName:string); 
  procedure  ParagGraph_SetAlignment( wdAl : TwdAligment );//输出文本对齐方式 
  public 
  constructor Create ; 
  destructor  Destroy;override; 
  procedure    CreateNew(const FileName: widestring;var sERR: widestring);//建立一个新的Word文件 
  procedure    LoadFromFile(FileName: widestring;var sERR:widestring; const sPassword:string='');//Load出一个已经建立的word文件 
  procedure    CreateAndOpen(const FileName: widestring;var sERR: widestring); //建立并打开一个word文件
  procedure    OutText( const atext:string); //输出文字 
  procedure    OutPicture( sFileName:string); //输出图片 
  procedure    LineFeed( lineFeed_Count:integer=1); overload; //换行 
  procedure    LineFeed(const linen:integer;lineFeed_Count:integer=1);overload; //换几行 
  procedure    GoToBottom;//到文件末尾 
  procedure    ChangPassword( sPassword:string); 
  procedure    Table_AppendTable(hang,Lie:integer);//在现有的表中追加一个表 
  procedure    Table_GotoCell(row,col:integer); //到达指定的格里 
  procedure    Table_Cell_HoriztalAlign_Left(row,col:integer); //设置指定格文本对齐方式(横) 
  procedure    Table_Cell_HoriztalAlign_Right(row,col:integer); 
  procedure    Table_Cell_HoriztalAlign_Center(row,col:integer); 
  procedure    Table_Cell_VerticalAlign_Top(row,col:integer);//设置指定格文本对齐方式(竖) 
  procedure    Table_Cell_VerticalAlign_Center(row,col:integer); 
  procedure    Table_Cell_VerticalAlign_Bottom(row,col:integer); 
  procedure    Table_Cell_Set_border( 
  Row,Col :Integer 
  ); 
  procedure    Table_Cell_Set_border2( 
  Row,Col, 
  cellborder_Left,cellborder_top, 
  cellborder_Right,cellborder_bottom:Integer 
  ); 
  procedure    Table_VerticalAlign_Top( );  //表格文本对齐方式(竖) 
  procedure    Table_VerticalAlign_Center( ); 
  procedure    Table_VerticalAlign_Bottom( ); 
  procedure    Table_HoriztalAlign_Left ; 
  procedure    Table_HoriztalAlign_Right; 
  procedure    Table_HoriztalAlign_Center; 
  procedure    Set_Table_To_Left ; //设置整个表在文本的对齐方式 
  procedure    Set_Table_To_Right; 
  procedure    Set_Table_To_Center; 
  procedure    Table_MergeCell(row,col,row2,col2:integer);  //合并指定列 
  procedure    Table_SetColWidth( colNumber:integer; 
  colWidth_Points:Double); 
  procedure    Table_SetRowHeight( rowNumber:integer;  //合并指行 
  rowHeight_Points:Double); 
  procedure    Table_setBorderStyle(  //设置表处边框类型 
  wdLineStyle_Outer, 
  wdLineStyle_inner:Integer ); 
  procedure    Close(); 
  procedure    ParaGraph_SetFont( FontName:string;  //设置区域字体字号 
  FontSize:Integer; 
  Bold    :Boolean=false; 
  Italic  :Boolean=false 
  ); 
  procedure  ParagGraph_Alignment_Left; 
  procedure  ParagGraph_Alignment_Center; 
  procedure  ParagGraph_Alignment_Right; 
  procedure  InsertDoc( fileName:string); 
  function    CentimetersToPoints( xCentimeters:real):Real; //设置行高以厘米为单位 
  property sERR:widestring read fSERR; 
  end; 
  implementation 
  { TFS_word } 
  procedure TFS_word.Table_AppendTable(hang, Lie: integer); 
  begin 
  GoToBottom; 
  nRows:=hang; 
  nCols:=Lie; 
  try 
  Table:= Word.ActiveDocument.Tables.Add(Range:=Word.Selecti on.Range, 
  NumRows:=hang,  //10行10列表格 
  NumColumns:=lie, 
  DefaultTableBehavior:=1,  // 线黑色 
  AutoFitBehavior:=0); // 设定活动文档中第一张表格的"自动调整"操作,使其可根据文档窗口的宽度自动调整大小。 
  except 
  end; 
  end; 
  function TFS_word.CentimetersToPoints(xCentimeters: real): Real; 
  begin 
  result:= xCentimeters * 28.35; 
  end; 
  procedure TFS_word.Close; 
  begin 
  if  FIsOpened then 
  begin 
  try 
  if sERR='' then 
  begin word.documents.item(1).Save; 
  end; 
  except 
  end; 
  word.quit; 
  try 
  Word:=CreateOleObject('Word.Application'); 
  word.DisplayAlerts:=false; 
  except 
  on E:Exception do 
  begin 
  FsERR:=E.Message; 
  end; 
  end; 
  end; 
  FisOpened:=false; 
  end; 
  constructor TFS_word.Create ; 
  begin 
  inherited; 
  FsERR:=''; 
  Application.ProcessMessages; 
  FisOpened  :=false; 
  try 
  Word:=CreateOleObject('Word.Application'); 
  word.DisplayAlerts:=false; 
  word.visible:=false; 
  except 
  on E:Exception do 
  FsERR:=E.Message; 
  end; 
  Table:=null; 
  Application.ProcessMessages; 
  end; 
  procedure TFS_word.CreateAndOpen(const FileName: widestring; 
  var sERR: widestring); 
  begin 
  CreateNew (FileName,sERR); 
  if sERR='' then 
  LoadFromFile(FileName,sERR); 
  end; 
  procedure TFS_word.CreateNew(const FileName: widestring; 
  var sERR: widestring); 
  var app:OleVariant; 
  begin 
  sERR:=''; 
  try 
  App:=CreateOleObject('Word.application'); 
  App.DisplayAlerts:=false; 
  try 
  App.Documents.Add().SaveAs(FileName); 
  except 
  on E:Exception do 
  sERR:=E.Message; 
  end ; 
  APP.quit; 
  APP:=Null; 
  except 
  on E:Exception do 
  sERR:=E.Message; 
  end; 
  end; 
  destructor TFS_word.Destroy; 
  begin 
  try 
  close; 
  except 
  end; 
  try 
  word.quit; 
  except 
  end; 
  inherited; 
  end; 
  procedure TFS_word.GoToBottom; 
  var k:integer; 
  begin 
  try 
  Repeat 
  K:=word.selection.moveDown(wdscreen,1); 
  until K 代码含义 
  Microsoft Word是一个集成化环境,是美国微软公司的字处理系统,但是它决不仅仅是一个字处理系统,它集成了Microsoft Visual Basic,可以通过编程来实现对Word功能的扩展。 
  Microsoft Visual Basic在word中的代码即Word的宏,通过编写Word宏,可实现一些文档处理的自动化,如实现文档的自动备份、存盘等,可扩展Word文档的功能,因此,能够充分利用Word的特性,甚至使Word成为自己软件的一部分。 
  Word的宏既有有利的一部分,因为它能够帮助我们实现文档的自动化,但是Word的宏也不是纯粹的有利,有时它可能危害我们的文档、计算机系统甚至网络,从最开始的Taiwan NO1宏病毒到现在的Melissa宏病毒,从最开始的简单的提示,耗尽系统资源到现在的乱发电子邮件,将个人的信息发送到网络上,甚至向硬盘的Autoexec.bat(自动批处理文件)中添加Deltree C: -y,破坏整个Windows系统。 
  二、Word中内嵌的Com技术 
  可以说Word是对Com技术支持最好的软件,这样说似乎是太极端了一点,但是Word提供的强大的编程接口技术却能够是我们通过程序控制Word的任何一部分。无论是文件的打开、存盘、打印还是文档中表格的自动绘制。 
  通过编程软件,可以灵活的操纵word,这里只以Borland Delphi为例,进行详细描述: 
  1、 在Delphi中调用Word软件/文件的方法 
  在Word中调用Word软件,归纳起来有三种方法: 
  。通过Delphi的控件TOleContainer 将Word嵌入 
  a.使用Delphi提供的Servers控件调用Word,使用Word的属性 
  b.通过真正的Com技术,将Office软件目录中文件MSWORD9.OLB中的类库全部导入Delphi中,利用Com技术编程 
  c.使用CreateOleObject将启动Word,然后以Ole方式对Word进行控制。 
  2、对几种方法的难易程度的判别 
  a.通过Delphi的控件TOleContainer 将Word嵌入 
  这是最简单的Ole嵌入,能够直接将Word文档调用,只需要使用ToleContainer.Run就可以将Word文档直接启动。且这样启动的Word文档与Delphi程序是一个整体(从界面上看),但是它存在不可克服的缺点,即不能通过Delphi控制Word文档,也就不能实现将灵活操纵Word的目的。 
  b.使用Delphi提供的Servers控件调用Word,使用Word的属性 
  使用Delphi的Servers控件来操纵Word,在编程时Delphi能够实现代码提示,总体上看能够较好的实现Delphi对Word的控制,但是还有一些Word的功能不能在Delphi中调用(比如自己编写的VBA宏代码)。且实现功能时本来在VBA代码中可选则参数在Delphi调用的时候必须添加,否则,连编译都不能通过。本方式启动的Word与Delphi程序分属两个窗体。此办法仅能作为一个参考。 
  c.通过真正的Com技术,将Office软件目录中文件MSWORD9.OLB中的类库全部导入Delphi中,利用Com技术编程利用真正的Com技术,将MsWord9.OLD文件类库导入,然后利用Com技术进行使用。整体上类似使用Delphi的Servers控件,稍微比Servers控件麻烦,优缺点与Servers控件相同。 
  d.使用CreateOleObject将启动Word,然后以Ole方式对Word进行控制。 
  本办法是使用以CreateOleObjects方式调用Word,实际上还是Ole,但是这种方式能够真正做到完全控制Word文件,能够使用Word的所有属性,包括自己编写的VBA宏代码。 
  与Servers控件和com技术相比,本方法能够真正地使用Word的各种属性,和在VBA中编写自己的代码基本一样,可以缺省的代码也不需要使用。本方式启动的Word与Delphi程序分属两个窗体。缺点是使用本方法没有Delphi代码提示,所有异常处理均需要自己编写,可能编写时探索性知识比较多。 
  三、Word宏编辑器 
  Word能够真正地进行VBA代码的编辑,可以编写窗体、函数。 
  进入Word宏编辑器的方法:工具->宏->Visual Basic编辑器,可进入Visual Basic编辑器界面。Word的Visual Basic编辑器界面和真正的Visual Basic编辑器基本相同,在此不再向详述。在VBA代码中,可以添加用户窗体、模块、类模块。用户窗体、模块、类模块的概念和Visual Basic完全相同。注释也与Visual Basic完全相同。 
  可以将光标停留在窗体、模块的任何一个子程序上,直接按"F5"运行当前子程序。 
  四、Word的宏的概述 
  Word充分地将文档编辑和VB结合起来,真正地实现文档的自动化。使用Word编程,类似于使用Visual Basic,所不同的是,在Word中,能够直接运行某一个子程序,直接看见结果,Word的宏,只能解释运行,而Visual Basic,现在已经能够编写成真正的机器码,从代码的保护上来说,应该尽可能地减少Word的VBA代码数量,尤其是关键的代码。 
  VBA宏,可分成四种: 
  1、和命令名相同的宏 
  如FileSave,FileOpen,如果在VBA代码中包含与Word同名的函数,则直接执行这些VBA代码,忽略Word本身的命令。 
  2、Word内特定的宏 
  这些宏包含AutoExec(启动 Word 或加载全局模板)、AutoNew(每次新建文档时)、AutoOpen(每次打开已有文档时)、AutoClose(每次关闭文档时),AutoExit(退出 Word 或卸载全局模板时)。 
  如果VBA代码中含有这些名称的函数,则满足相应的条件,相应代码就自动执行。 
  3、相应事件的VBA宏 
  这些宏是由事件触发的宏,如Document_Close在文档关闭的时候触发事件,Document_New在新建文档的时候触发,Document_Open在打开文档的时候触发。 
  4、独立的宏 
  自己编写的VBA代码,即不属于上面几种情况的VBA代码,可以被其他VBA代码调用,更重要的是,可以被其他程序调用。 
  这样,我们就可以屏弃Word自动执行的宏,通过Delphi直接调用相应宏来达到目的。 
  五、Word命令宏的详细描述
  Word本身的命令函数包含很多,但是无论是word联机帮助还是MSDN帮助,都没有这方面的介绍,因此只能凭自己的实验取探索,初步探测的函数如下: 
  宏名 解释 注释 
  FileNew 新建 
  FileNewDefault 新建空白文档 
  FileSaveAs 另存为 
  FileOpen 打开 
  FileClose 关闭 
  FilePrint 打印 
  FilePrintPreview 打印预览 
  ToolsCustomize 工具栏里面的自定义 
  ToolsOptions 工具选项 
  ToolsRevisions 突出显示修订 
  ToolsReviewRevisions 接受或拒绝修订 
  ToolsRevisionMarksAccept 接受修订 
  ToolsRevisionMarksReject 拒绝修订 
  ToolsRevisionMarksToggle 修订 
  ToolsMacro 宏 
  ToolsRecordMacroToggle 录制新宏 
  ViewSecurity 安全性 
  ViewVBCode 查看VB编辑器环境 
  FileTemplates 模板和可加载项 
  ToolsProtectUnprotectDocument 解除对文档的保护 
  InsertHyperlink 插入超级链接 
  EditHyperlink 编辑超级链接 
  DeleteHyperlink 删除超级链接 
  EditLinks 查看、删除链接 
  EditPasteAsHyperlink 粘贴超级链接 
  FormatStyle 样式 
  EditBookMark 书签 
  OleWord时一些用用的代码 
  [email protected] 
  一、Delphi程序启动Word 
  采用CreateOleObjects的方法来启动Word,调用VBA代码,具体实现过程为: 
  首先使用GetActiveOleObject('Word.Application')判断当前内存中是否存在Word程序,如果存在,则直接连接,如果没有Word程序,则使用CreateOleObject('Word.Application')启动Word 
  二、Delphi程序新建Word文稿 
  格式:WordDocuments.Add(Template,NewTemplate,DocumentTyp e,Visible) 
  Template: 使用模板的名称, 
  NewTemplate: 新建文档的类型,True表示为模板,False表示为文档 
  DocumentType: 文档类型,默认为空白文档 
  Visible: 打捞的窗口是否可见 
  举例:Doc_Handle:=Word_Ole.Documents.Add(Template:='C:\T emlate.dot',NewTemplate:=False); 
  三、Delphi程序打开Word文稿 
  格式:WordDocuments.Open(FileName,ConfirmConversions,Rea dOnly,PassWordDocument, 
  PasswordTemplate,Revent,WritePasswordDocument,Writ ePassWordTemplate, 
  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,FileF ormat=wdFormatDocument, 
  AddToRecentFiles=False); 
  五、从数据库读取文件到本地硬盘和从本地硬盘读取文件到数据库 
  在数据库上使用Image二进制字段保存,使用Stream流的方式。 
  创建文件流: 
  Word_FileStream:=TFileStream.Create(Target_Name,fm OpenWrite or fmCreate); 
  Word_FileStream.Position:=0; 
  保存到数据库的Image字段: 
  TBlobField(AdoQuery1.FieldByName(Column_Name)).Sav eToStream(Word_FileStream); 
  从数据库读取文件到本地硬盘: 
  TBlobField(ADOQuery1.FieldByName(Column_Name)).loa dfromStream(Word_FileStream); 
  释放文件流: 
  Word_FileStream.Free; 
  六、全局消息的定义 
  因为word和Delphi程序是两个软件,相互之间通讯比较麻烦,所以使用全局消息的方法进行。全局消息必须首先注册,Windows返回系统空闲的消息号,当注册的消息相同时,Windows系统返回同一个值,这样就保证了使用这个消息号在两个程序之间通讯。 
  定义消息的办法: 
  szMessageString: pchar = 'XIDIAN_11_Stone'; 
  FMyJoinMessage := RegisterWindowMessage(szMessageString); 
  发送消息的方法: 
  SendMessage(对方句柄,消息,消息附带短变量,消息附带长变量) 
  七、Delphi程序接收消息的方法 
  Delphi接收消息有两种,一是重载特定消息,二是重载WndProc函数,在里面选择相应消息进行处理。 
  法一,每次只能处理一条消息,而法二能够同时处理多条消息。 
  对于法二,声明如下: 
  procedure WndProc(var Message: Tmessage);override 
  必须注意,使用时需要在处理完自己消息处理后继承WndProc(Message)函数,否则系统会崩溃! 
  八、Word中Combo对话框的动态生成以及Change事件 
  建立类模块Combohander,在内部定义事件 
  Public WithEvents ComboBoxEvent As Office.CommandBarComboBox 
  定义Combo控件产生事件的模块 
  Dim ctlComboBoxHandler As New ComboBoxHandler 
  产生Combo对话框 
  Set Cbo_ChooseDoc = CommandBars("添加的菜单").Controls.Add(Type:=msoControlComboBox, Temporary:=True) 
  进行文件句柄设置,以产生Combo_Change事件 
  Set ctlComboBoxHandler.ComboBoxEvent = Cbo_ChooseDoc 
  产生事件后,在类模块Combohander内选择ComboBoxEvent的Change事件,即可书写事件代码 
  Sub ComboBoxEvent_Change(ByVal Ctrl As Office.CommandBarComboBox) 
  九、一些Word的事件 
  VBA代码中处理的Word事件有:Document_Close 
  Application事件中需要处理的有:DocumentBeforeClose,DocumentChange。 
  Document_Close:事件在文档关闭时产生事件 
  DocumentBeforeClose:在文档被关闭以前先于Word判断文档是否保存,给出相应提示并进行相应处理。 
  DocumentChange:文档切换,在文档从自己修改的文稿和其他人修改的文稿之间切换产生事件,主要处理设置文档权限等

猜你喜欢

转载自xpdr14xpdr.iteye.com/blog/1362577