WordApplication1.Documents.add(Template,NewTemplate);
不知道为什么这句在delphi6中编译老是出错阿,而在delphi5中就可以实现这个功能,帮帮我阿!
我还想知道怎么在delphi6中打开一个模板的方法。
谢谢大家看我的贴子。

解决方案 »

  1.   

    用这种方法试试
    var
        Word:Variant;
    begin
        Word:=CreateOLeObject('Word.application');
        Word.vasible:=True;
        Word.Documents.add(Template,NewTemplate);
    end;
      

  2.   

    第二句写错了
    Word.visible:=True;
      

  3.   

    Delphi与Word之间的融合技术
    来自大富翁
    问题:Delphi与Word之间的融合技术 ( 积分:0, 回复:122, 阅读:3354 ) 来自:yzhshi, 时间:2001-11-18 17:52:00, ID:737517
    ([email protected])
    一、VBA代码含义
    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 书签 
     
    来自:yzhshi, 时间:2001-11-18 17:54:00, ID:737522 
    这是我自己写的,结合自己的经验,在这里贡献出来,供大家共享。
    里面有很多用词、观点也许不是特别确切,希望大家指出。
    过几天再将一些具体的代码写出来(目前没有仔细整理)。
     
     
    来自:yzhshi, 时间:2001-11-18 18:04:00, ID:737532 
    OleWord时一些用用的代码
    [email protected]一、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:  打开文档的窗口是否可见
      

  4.   

    netlib(河外孤星)你好阿
    我知道你讲的办法,方法
    我只是想知道为什么在delphi5中可以
    而在delphi6就不行呢?
    帮帮我,我现在在研究这方面的知识。
    明天给分!
      

  5.   

    excel的程序参考下
    //  该文件*作单元封装了大部分的Excel*作
    //  use to manipulate Excel xls File
    //   Dragon P.C. <2000.05.10>
    unit ExcelUnit;interfaceuses
     Dialogs, Messages, SysUtils, Grids, Cmp_Sec, ComObj, Ads_Misc; {!~Add a blank WorkSheet}
     Function ExcelAddWorkSheet(Excel : Variant): Boolean; {!~Close Excel}
     Function ExcelClose(Excel : Variant; SaveAll: Boolean): Boolean; {!~Returns the Column String Value from its integer equilavent.}
     Function ExcelColIntToStr(ColNum: Integer): ShortString; {!~Returns the Column Integer Value from its Alpha equilavent.}
     Function ExcelColStrToInt(ColStr: ShortString): Integer; {!~Close All Workbooks.  All workbooks can be saved or not.}
     Function ExcelCloseWorkBooks(Excel : Variant; SaveAll: Boolean): Boolean; {!~Copies a range of Excel Cells to a Delphi StringGrid.  If successful
     True is returned, False otherwise.  If SizeStringGridToFit is True
     then the StringGrid is resized to be exactly the correct dimensions to
     receive the input Excel cells, otherwise the StringGrid is not resized.
     If ClearStringGridFirst is true then any cells outside the input range
     are cleared, otherwise existing values are retained.  Please not that the
     Excel cell coordinates are "1" based and the Delphi StringGrid coordinates
     are zero based.}
     Function ExcelCopyToStringGrid(
       Excel                 : Variant;
       ExcelFirstRow         : Integer;
       ExcelFirstCol         : Integer;
       ExcelLastRow          : Integer;
       ExcelLastCol          : Integer;
       StringGrid            : TStringGrid;
       StringGridFirstRow    : Integer;
       StringGridFirstCol    : Integer;
       {Make the StringGrid the same size as the input range}
       SizeStringGridToFit   : Boolean;
       {cells outside input range in StringGrid are cleared}
       ClearStringGridFirst  : Boolean
       ): Boolean; {!~Delete a WorkSheet by Name}
     Function ExcelDeleteWorkSheet(
       Excel     : Variant;
       SheetName : ShortString): Boolean; {!~Moves the cursor to the last row and column}
     Function ExcelEnd(Excel : Variant): Boolean; {!~Finds A value and moves the cursor there.
     If the value is not found then the cursor does not move.
     If nothing is found then false is returned, True otherwise.}
     Function ExcelFind(
       Excel       : Variant;
       FindString  : ShortString): Boolean; {!~Finds A value in a range and moves the cursor there.
     If the value is not found then the cursor does not move.
     If nothing is found then false is returned, True otherwise.}
     Function ExcelFindInRange(
       Excel       : Variant;
       FindString  : ShortString;
       TopRow      : Integer;
       LeftCol     : Integer;
       LastRow     : Integer;
       LastCol     : Integer): Boolean; {!~Finds A value in a range and moves the cursor there.  If the value is
     not found then the cursor does not move.  If nothing is found then
     false is returned, True otherwise.  The search directions can be defined.
     If you want row searches to go from left to right then SearchRight should
     be set to true, False otherwise.  If you want column searches to go from
     top to bottom then SearchDown should be set to true, false otherwise.
     If RowsFirst is set to true then all the columns in a complete row will be
     searched.}
     Function ExcelFindValue(
       Excel       : Variant;
       FindString  : ShortString;
       TopRow      : Integer;
       LeftCol     : Integer;
       LastRow     : Integer;
       LastCol     : Integer;
       SearchRight : Boolean;
       SearchDown  : Boolean;
       RowsFirst   : Boolean
       ): Boolean; {!~Returns The First Col}
     Function ExcelFirstCol(Excel : Variant): Integer; 
      

  6.   

    {!~Returns The First Row}
     Function ExcelFirstRow(Excel : Variant): Integer; {!~Returns the name of the currently active worksheet
     as a shortstring}
     Function ExcelGetActiveSheetName(Excel : Variant): ShortString; {!~Gets the formula in a cell.}
     Function ExcelGetCellFormula(
       Excel         : Variant;
       RowNum, ColNum: Integer): ShortString; {!~Returns the contents of a cell as a shortstring}
     Function ExcelGetCellValue(Excel : Variant; RowNum, ColNum: Integer): ShortString; {!~Returns the the current column}
     Function ExcelGetCol(Excel : Variant): Integer; {!~Returns the the current row}
     Function ExcelGetRow(Excel : Variant): Integer; {!~Moves the cursor to the last column}
     Function ExcelGoToLastCol(Excel : Variant): Boolean; {!~Moves the cursor to the last row}
     Function ExcelGoToLastRow(Excel : Variant): Boolean; {!~Moves the cursor to the Leftmost Column}
     Function ExcelGoToLeftmostCol(Excel : Variant): Boolean; {!~Moves the cursor to the Top row}
     Function ExcelGoToTopRow(Excel : Variant): Boolean; {!~Moves the cursor to Home position, i.e., A1}
     Function ExcelHome(Excel : Variant): Boolean; {!~Returns The Last Column}
     Function ExcelLastCol(Excel : Variant): Integer; {!~Returns The Last Row}
     Function ExcelLastRow(Excel : Variant): Integer; {!~Open the file you want to work within Excel.  If you want to
     take advantage of optional parameters then you should use
     ExcelOpenFileComplex}
     Function ExcelOpenFile(Excel : Variant; FileName : String): Boolean; {!~Open the file you want to work within Excel.  If you want to
     take advantage of optional parameters then you should use
     ExcelOpenFileComplex}
     Function ExcelOpenFileComplex(
       Excel        : Variant;
       FileName     : String;
       UpdateLinks  : Integer;
       ReadOnly     : Boolean;
       Format       : Integer;
       Password     : ShortString): Boolean; {!~Saves the range on the currently active sheet
     to to values only.}
     Function ExcelPasteValuesOnly(
       Excel         : Variant;
       ExcelFirstRow : Integer;
       ExcelFirstCol : Integer;
       ExcelLastRow  : Integer;
       ExcelLastCol  : Integer): Boolean; {!~Renames a worksheet.}
     Function ExcelRenameSheet(
       Excel         : Variant;
       OldName       : ShortString;
       NewName       : ShortString): Boolean; {!~Saves the range on the currently active sheet
     to a DBase 4 table.}
     Function ExcelSaveAsDBase4(
       Excel         : Variant;
       ExcelFirstRow : Integer;
       ExcelFirstCol : Integer;
       ExcelLastRow  : Integer;
       ExcelLastCol  : Integer;
       OutFilePath   : ShortString;
       OutFileName   : ShortString): Boolean; {!~Saves the range on the currently active sheet
     to a text file.}
     Function ExcelSaveAsText(
       Excel         : Variant;
       ExcelFirstRow : Integer;
       ExcelFirstCol : Integer;
       ExcelLastRow  : Integer;
       ExcelLastCol  : Integer;
       OutFilePath   : ShortString;
       OutFileName   : ShortString): Boolean; {!~Selects a range on the currently active sheet.  From the
     current cursor position a block is selected down and to the right.
     The block proceeds down until an empty row is encountered.  The
     block proceeds right until an empty column is encountered.}
     Function ExcelSelectBlock(
         Excel    : Variant;
         FirstRow : Integer;
         FirstCol : Integer): Boolean; {!~Selects a range on the currently active sheet.  From the
     current cursor position a block is selected that contains
     the currently active cell.  The block proceeds in each
     direction until an empty row or column is encountered.}
     Function ExcelSelectBlockWhole(Excel: Variant): Boolean; {!~Selects a cell on the currently active sheet}
     Function ExcelSelectCell(Excel : Variant; RowNum, ColNum: Integer): Boolean; {!~Selects a range on the currently active sheet}
     Function ExcelSelectRange(
       Excel    : Variant;
       FirstRow : Integer;
       FirstCol : Integer;
       LastRow  : Integer;
       LastCol  : Integer): Boolean; {!~Selects an Excel Sheet By Name}
     Function ExcelSelectSheetByName(Excel : Variant; SheetName: String): Boolean; {!~Sets the formula in a cell.  Remember to include the equals sign "=".
     If the function fails False is returned, True otherwise.}
     Function ExcelSetCellFormula(
       Excel         : Variant;
       FormulaString : ShortString;
       RowNum, ColNum: Integer): Boolean; {!~Sets the contents of a cell as a shortstring}
     Function ExcelSetCellValue(
       Excel : Variant;
       RowNum, ColNum: Integer;
       Value : ShortString): Boolean; {!~Sets a Column Width on the currently active sheet}
     Function ExcelSetColumnWidth(
       Excel      : Variant;
       ColNum     : Integer;
       ColumnWidth: Integer): Boolean; {!~Set Excel Visibility}
     Function ExcelSetVisible(
       Excel : Variant;
       IsVisible: Boolean): Boolean; {!~Saves the range on the currently active sheet
     to values only.}
     Function ExcelValuesOnly(
       Excel         : Variant;
       ExcelFirstRow : Integer;
       ExcelFirstCol : Integer;
       ExcelLastRow  : Integer;
       ExcelLastCol  : Integer): Boolean; {!~Returns the Excel Version as a ShortString.}
     Function ExcelVersion(Excel: Variant): ShortString; Function IsBlockColSide(
       Excel : Variant;
       RowNum: Integer;
       ColNum: Integer): Boolean; Forward;
       
     Function IsBlockRowSide(
       Excel : Variant;
       RowNum: Integer;
       ColNum: Integer): Boolean; Forward;
    implementation
      

  7.   

    type
     //Declare the constants used by Excel
     SourceType = (xlConsolidation, xlDatabase, xlExternal, xlPivotTable);
     Orientation = (xlHidden, xlRowField, xlColumnField, xlPageField, xlDataField);
     RangeEnd = (NoValue, xlToLeft, xlToRight, xlUp, xlDown);
     ExcelPasteType = (xlAllExceptBorders,xlNotes,xlFormats,xlValues,xlFormulas,xlAll); {CAUTION!!! THESE OUTPUTS ARE ALL GARBLED!  YOU SELECT xlDBF3 AND EXCEL
     OUTPUTS A xlCSV.}
     FileFormat = (xlAddIn, xlCSV, xlCSVMac, xlCSVMSDOS, xlCSVWindows, xlDBF2,
                   xlDBF3, xlDBF4, xlDIF, xlExcel2, xlExcel3, xlExcel4,
                   xlExcel4Workbook, xlIntlAddIn, xlIntlMacro, xlNormal,
                   xlSYLK, xlTemplate, xlText, xlTextMac, xlTextMSDOS,
                   xlTextWindows, xlTextPrinter, xlWK1, xlWK3, xlWKS,
                   xlWQ1, xlWK3FM3, xlWK1FMT, xlWK1ALL);{Add a blank WorkSheet}
    Function ExcelAddWorkSheet(Excel : Variant): Boolean;
    Begin
     Result := True;
     Try
       Excel.Worksheets.Add;
     Except
       MessageDlg('Unable to add a new worksheet', mtError, [mbOK], 0);
       Result := False;
     End;
    End;{Sets Excel Visibility}
    Function ExcelSetVisible(Excel : Variant;IsVisible: Boolean): Boolean;
    Begin
     Result := True;
     Try
       Excel.Visible := IsVisible;
     Except
       MessageDlg('Unable to Excel Visibility', mtError, [mbOK], 0);
       Result := False;
     End;
    End;{Close Excel}
    Function ExcelClose(Excel : Variant; SaveAll: Boolean): Boolean;
    Begin
     Result := True;
     Try
       ExcelCloseWorkBooks(Excel, SaveAll);
       Excel.Quit;
     Except
       MessageDlg('Unable to Close Excel', mtError, [mbOK], 0);
       Result := False;
     End;
    End;{Close All Workbooks.  All workbooks can be saved or not.}
    Function ExcelCloseWorkBooks(Excel : Variant; SaveAll: Boolean): Boolean;
    var
     loop: byte;
    Begin
     Result := True;
     Try
       For loop := 1 to Excel.Workbooks.Count Do
         Excel.Workbooks[1].Close[SaveAll];
     Except
       Result := False;
     End;
    End;{Selects an Excel Sheet By Name}
    Function ExcelSelectSheetByName(Excel : Variant; SheetName: String): Boolean;
    Begin
     Result := True;
     Try
       Excel.Sheets[SheetName].Select;
     Except
       Result := False;
     End;
    End;{Selects a cell on the currently active sheet}
    Function ExcelSelectCell(Excel : Variant; RowNum, ColNum: Integer): Boolean;
    Begin
     Result := True;
     Try
       Excel.ActiveSheet.Cells[RowNum, ColNum].Select;
     Except
       Result := False;
     End;
    End;{Returns the contents of a cell as a shortstring}
    Function ExcelGetCellValue(Excel : Variant; RowNum, ColNum: Integer): ShortString;
    Begin
     Result := '';
     Try
       Result := Excel.Cells[RowNum, ColNum].Value;
     Except
       Result := '';
     End;
    End;{Returns the the current row}
    Function ExcelGetRow(Excel : Variant): Integer;
    Begin
     Result := 1;
     Try
       Result := Excel.ActiveCell.Row;
     Except
       Result := 1;
     End;
    End;{Returns the the current column}
    Function ExcelGetCol(Excel : Variant): Integer;
    Begin
     Result := 1;
     Try
       Result := Excel.ActiveCell.Column;
     Except
       Result := 1;
     End;
    End;{Moves the cursor to the last column}
    Function ExcelGoToLastCol(Excel : Variant): Boolean;
    Begin
     Result := True;
     Try
       Excel.Selection.End[xlToRight].Select;
     Except
       Result := False;
     End;
    End;{Moves the cursor to the last row}
    Function ExcelGoToLastRow(Excel : Variant): Boolean;
    Begin
     Result := True;
     Try
       Excel.Selection.End[xlDown].Select;
     Except
       Result := False;
     End;
    End;{Moves the cursor to the Top row}
    Function ExcelGoToTopRow(Excel : Variant): Boolean;
    Begin
     Result := True;
     Try
       Excel.Selection.End[xlUp].Select;
     Except
       Result := False;
     End;
    End;{Moves the