delphi word

什么是(OLE)自动化? 什么是自动化服务器? 什么是自动化客户端? ( What is (OLE) Automation? What is Automation Server? What is Automation Client? )

Suppose you are developing an HTML editor like HTML Kit. As like any other textual editor your application should contain some kind of spell checking system. Why buy spell checking components or write them from scratch when you can easily use MS Word?

假设您正在开发类似HTML KitHTML编辑器。 与其他任何文本编辑器一样,您的应用程序应包含某种拼写检查系统。 当您可以轻松使用MS Word时,为什么要购买拼写检查组件或从头开始编写呢?

OLE自动化 ( OLE Automation )

one application can control another 一个应用程序可以控制另一台 automation client 自动化客户端 automation server 自动化服务器

Automation (also known as OLE Automation) is a feature that programs use to expose their objects to development tools, macro languages, and other programs that support Automation. For example, Microsoft Outlook may expose objects for sending and receiving e-mail, for scheduling, and for contact and task management.

自动化(也称为OLE自动化)是程序用来将其对象公开给支持自动化的开发工具,宏语言和其他程序的功能。 例如,Microsoft Outlook可能会公开用于发送和接收电子邮件,进行计划以及进行联系人和任务管理的对象。

By using Word Automation (server), we can use Delphi (client) to dynamically create a new document, add some text we want to spell check, and then have Word check the spelling. If we keep Microsoft Word minimized, our users might never know! Thanks to Microsoft Word's OLE interface, we can take a side trip from Delphi and look at ways to cheat when developing our version of Notepad editor :)

通过使用Word Automation(服务器),我们可以使用Delphi(客户端)动态创建一个新文档,添加一些我们想要拼写检查的文本,然后让Word检查拼写。 如果我们使Microsoft Word最小化,我们的用户可能永远不知道! 多亏了Microsoft Word的OLE界面,我们可以从Delphi旁走一遍,并研究开发我们的记事本编辑器版本时作弊的方法:)

There's only one glitch ;) Users of the application need to have Word installed. But don't let this stop you.

只有一个小故障;)应用程序的用户需要安装Word。 但是不要让这阻止了你。

Of course, to fully master the use of Automation in your applications, you must have detailed working knowledge of the applications you are integrating - in this case the MS Word.

当然,要完全掌握自动化在您的应用程序中的使用,您必须对要集成的应用程序(在本例中为MS Word)有详细的使用知识。

In order for your "Office" programs to work, the user must own the application that acts like Automation server. In our case MS Word must be installed on the user's machine.

为了使“ Office”程序正常运行,用户必须拥有与自动化服务器类似的应用程序。 在我们的情况下,必须在用户计算机上安装MS Word。

连接到Word:“ Hello Word”早期绑定与后期绑定 ( Connecting to Word: "Hello Word" Early Binding vs. Late Binding )

There are several main steps and three main ways to automate Word from Delphi.

从Delphi自动化Word有几个主要步骤和三种主要方法。

Delphi> = 5-Office XX服务器组件 ( Delphi >= 5 - Office XX Server Components )

TWordApplication TWordApplication TWordDocument TWordDocument

Delphi 3,4-早期绑定 ( Delphi 3,4 - Early Binding )

Type libraries 类型库

To use Word's type library in Delphi (version 3 or 4) select the Project | Import Type Library… menu and choose the file msword8.olb located in Microsoft Office's "Office" directory. This will create the file "Word_TLB.pas" which is the object pascal translation of the type library. Include Word_TLB in the uses list of any unit that will be accessing Word properties or methods. Referencing Word methods using the type library is called early binding.

要在Delphi(版本3或版本4)中使用Word的类型库,请选择“项目” |“项目”。 导入类型库…菜单,然后选择位于Microsoft Office的“ Office”目录中的文件msword8.olb。 这将创建文件“ Word_TLB.pas”,它是类型库的对象pascal翻译。 在将要访问Word属性或方法的任何单元的使用列表中包括Word_TLB 。 使用类型库引用Word方法称为早期绑定

Delphi 2-后期绑定 ( Delphi 2 - Late Binding )

Late binding 后期装订

should be avoided, if possible, since it's much easier and faster to use type libraries - the compiler helps by catching errors in the source. When using late binding Word is declared to be a variable of Variant type. This in particular means than to call methods and access properties you must know what they are.

如果可能的话,应该避免使用它,因为使用类型库要容易得多,而且速度更快-编译器通过捕获源中的错误来提供帮助。 使用后期绑定时,Word被声明为Variant类型的变量。 特别是,这意味着要调用方法和访问属性,您必须知道它们是什么。

静默启动(自动)单词 ( Launching (Automating) Word Silently )

The example in this article will use "server" components provided with Delphi. If you have some earlier version of Delphi I suggest you should use early binding with Word type library.

本文中的示例将使用Delphi提供的“服务器”组件。 如果您有早期版本的Delphi,建议您将早期绑定与Word类型库一起使用。

uses Word_TLB;
...
var
WordApp : _Application;
WordDoc : _Document;
VarFalse : OleVariant;
begin
WordApp := CoApplication.Create;
WordDoc := WordApp.Documents.Add(EmptyParam, EmptyParam) ;
 {
spell check code as described
later in this article
}
VarFalse:=False;
WordApp.Quit(VarFalse, EmptyParam, EmptyParam) ;
end; 

EmptyParam 空参数

To automate Word with a Variant variable (late binding) use this code:

要使用Variant变量( 后期绑定 )自动执行Word,请使用以下代码:

uses ComObj;
...
var
WordApp, WordDoc: Variant;
begin
WordApp := CreateOleObject('Word.Application') ;
WordDoc := WordApp.Documents.Add;
{
spell check code as described
later in this article
}
WordApp.Quit(False)
end; 

“轻松”的方式 ( The "Easy" Way )

these methods and defines several versions with varying numbers of parameters.

这些方法,并定义了多个具有不同数量参数的版本。

拼写检查项目-TWordApplication,TWordDocument ( The Spell Check Project - TWordApplication, TWordDocument )

To build a spell checking project we'll need two forms: one used to edit the text and the other to see the spelling suggestions... but, let's go from the beginning.

要构建拼写检查项目,我们需要两种形式:一种用于编辑文本,另一种用于查看拼写建议...但是,让我们从头开始。

Start Delphi. Create a new project with one blank form (form1, by default). This will be the main form in the spell checking with MS Word project. Add one TMemo (Standard tab) and two TButtons to the form. Add some text to the Memo filling the Lines property. Of course, with some typo errors. Select the Servers tab and add TWordApplication and TWordDocument to the form. Change the name of TWordApplication component from WordApplication1 to WordApp, WordDocument1 to WordDoc.

启动Delphi。 使用一个空白表单(默认为form1)创建一个新项目。 这将是使用MS Word项目进行拼写检查的主要形式。 将一个TMemo (标准选项卡)和两个TButton添加到窗体。 在“备注”中添加一些文本,以填充“线条”属性。 当然,还有一些拼写错误。 选择服务器选项卡,然后将TWordApplicationTWordDocument添加到窗体。 将TWordApplication组件的名称从WordApplication1更改为WordApp,将WordDocument1更改为WordDoc。

TWordApplication,TWordDocument ( TWordApplication, TWordDocument )

The published property ConnectKind is used to control whether we connect to a newly launched Word instance or to an existing instance that is already running. Set ConnectKind to ckRunningInstance.

发布的属性ConnectKind用于控制我们是连接到新启动的Word实例还是连接到已经运行的现有实例。 将ConnectKind设置为ckRunningInstance。

When we open or create a file in Word, we create a Document object. A common task when using automating Word is to specify an area in a document and then do something with it, such as insert text and spell check it. An object that represents a contiguous area in a document is called Range.

当我们在Word中打开或创建文件时,我们将创建一个Document对象。 使用自动Word时,常见的任务是在文档中指定一个区域,然后对其进行处理,例如插入文本并对其进行拼写检查。 表示文档中连续区域的对象称为范围。

拼写检查项目-拼写检查/替换 ( The Spell Check Project - Spell Check / Replace )

The idea is to loop through the text in the Memo and parses it into space delimited words. For each word, we call MS Word to spell check it. Word's Automation model contains the SpellingErrors method that lets you check the spelling of text contained in some Range.

想法是循环浏览备忘录中的文本,并将其解析为以空格分隔的单词。 对于每个单词,我们称其为MS Word进行拼写检查。 Word的自动化模型包含SpellingErrors方法,该方法使您可以检查某些Range中包含的文本的拼写。

Range is defined to contain only the word just parsed out. The SpellingErrors method returns a collection of misspelled words. If this collection contains more that zero words we move on. A call to the GetSpellingSuggestions method, passing in the incorrectly spelled word, fills a SpellingSuggestions collection of suggested replacement words.

范围定义为仅包含刚解析出的单词。 SpellingErrors方法返回拼写错误的单词的集合。 如果此集合包含的单词数超过零,则我们继续。 传入错误拼写的单词的对GetSpellingSuggestions方法的调用将填充SpellingSuggestions集合中的建议替换单词。

We pass this collection to the SpellCheck form. That is the second form in our project.

我们将此集合传递给SpellCheck表单。 那是我们项目中的第二种形式。

To add a new form to a project use File|New Form. Let it have the 'frSpellCheck' name. Add three TBitBtn components on this form. Two EditBox-es and one ListBox. Note the three more Labels. The "Not in dictionary" label is "connected" with the edNID edit box. The edNID simply display the misspelled word. The lbSuggestions list box will list the items in SpellingSuggestions collection. The selected spelling suggestion is placed in the edReplaceWith edit box.

要将新表单添加到项目中,请使用文件|新建表单。 使其具有“ frSpellCheck”名称。 在此表单上添加三个TBitBtn组件。 两个EditBox-es和一个ListBox。 注意另外三个标签。 “不在词典中”标签与edNID编辑框“连接”。 edNID仅显示拼写错误的单词。 lbSuggestions列表框将列出SpellingSuggestions集合中的项目。 所选的拼写建议将放置在edReplaceWith编辑框中。

The three BitButtons are used to Cancel the spell checking, Ignore the current word and to Change the misspelled word with the one in the edReplaceWith edit box. The BitBtn components ModalResult property is used when referring to what the user has clicked. The "Ignore" button has its ModalResult property set to mrIgnore, "Change" to mrOk and "Cancel" to mrAbort.

这三个BitButton用于取消拼写检查,忽略当前单词以及使用edReplaceWith编辑框中的一个来更改拼写错误的单词。 引用用户单击的内容时,将使用BitBtn组件的ModalResult属性。 “忽略”按钮的ModalResult属性设置为mrIgnore,“更改”为mrOk,“取消”为mrAbort。

The frSpellCheck has one Public string variable called sReplacedWord. This variable returns the text in the edReplaceWith when the user presses the "Change" button.

frSpellCheck具有一个名为sReplacedWord的公共字符串变量。 当用户按下“更改”按钮时,此变量将返回edReplaceWith中的文本。

最后:Delphi源代码 ( Finally: Delphi Source Code )

Here goes the parse-and-spell-check procedure:

这是解析和拼写检查过程:

procedure TForm1.btnSpellCheckClick (Sender: TObject) ;
var colSpellErrors : ProofreadingErrors;
colSuggestions : SpellingSuggestions;
j : Integer;
StopLoop : Boolean;
itxtLen, itxtStart : Integer;
varFalse : OleVariant;
begin
WordApp.Connect;
WordDoc.ConnectTo(WordApp.Documents.Add(EmptyParam, EmptyParam)) ;
//main loop
StopLoop:=False;
itxtStart:=0;
Memo.SelStart:=0;
itxtlen:=0;
while not StopLoop do begin
{parse the memo text into words.}
itxtStart := itxtLen + itxtStart;
itxtLen := Pos(' ', Copy(Memo.Text,1+itxtStart, MaxInt)) ;
if itxtLen = 0 then StopLoop := True;
Memo.SelStart := itxtStart;
Memo.SelLength := -1 + itxtLen;
if Memo.SelText = '' then Continue;
WordDoc.Range.Delete(EmptyParam,EmptyParam) ;
WordDoc.Range.Set_Text(Memo.SelText) ;
{call spell check}
colSpellErrors := WordDoc.SpellingErrors;
if colSpellErrors.Count <> 0 then begin
colSuggestions := WordApp.GetSpellingSuggestions (colSpellErrors.Item(1).Get_Text) ;
with frSpellCheck do begin
edNID.text := colSpellErrors.Item(1).Get_Text;
{fill in the list box with suggestions}
lbSuggestions.Items.Clear;
for j:= 1 to colSuggestions.Count do
lbSuggestions.Items.Add(VarToStr(colSuggestions.Item(j))) ;
lbSuggestions.ItemIndex := 0;
lbSuggestionsClick(Sender) ;
ShowModal;
case frSpellCheck.ModalResult of
mrAbort: Break;
mrIgnore: Continue;
mrOK:
if sReplacedWord <> '' then begin
Memo.SelText := sReplacedWord;
itxtLen := Length(sReplacedWord) ;
end;
end;
end;
end;
end;
WordDoc.Disconnect;
varFalse:=False;
WordApp.Quit(varFalse) ;
Memo.SelStart := 0;
Memo.SelLength := 0;
end;

词库? 词库! ( Thesaurus? Thesaurus! )

As a bonus the project has the code to use Word's Thesaurus. Using the thesaurus is quite easier. We don't parse the text, for the selected word the CheckSynonyms method is called. This method displays its own selection dialog. Once a new word is selected, the Word Documents Range contents is used to replace the original word.

作为奖励,该项目具有使用Word同义词库的代码。 使用同义词库非常容易。 我们不解析文本,对于所选单词,将调用CheckSynonyms方法。 此方法显示其自己的选择对话框。 一旦选择了新单词,Word文档范围内容将用于替换原始单词。

翻译自: https://www.thoughtco.com/spell-checking-from-delphi-code-1058149

delphi word

delphi word_使用MS Word从Delphi代码进行拼写检查-Delphi中的Office Automation相关推荐

  1. python代码大全p-21行Python代码实现拼写检查器

    引入 大家在使用谷歌或者百度搜索时,输入搜索内容时,谷歌总是能提供非常好的拼写检查,比如你输入 speling,谷歌会马上返回 spelling. 下面是用21行python代码实现的一个简易但是具备 ...

  2. python重复三角形代码_用于检查Python中两个三角形的一致性的程序

    在本教程中,我们将检查两个三角形的一致性.我们将检查SSS,SAS和AAA.基于这些标准证明了三角形的相似性. 我们必须根据定理检查不同的条件.在下面的代码中检查它们. 示例def side_side ...

  3. Word处理控件Aspose.Words功能演示:使用 C++ 创建 MS Word 文档 (DOC/DOCX)

    Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word.此 ...

  4. delphi 以前的记录,调试,代码,记录,排错等

    经常看到一些程序里面用到如: {$ifdef win16},{$ifdef win32}之类的信息, 可是这些好像并没有定义,不知道在哪里可以找到这些条件编译的定义或者是说明具体讲述win16代表什么 ...

  5. Delphi实现条码生成与打印实现代码

    条形码或称条码(barcode)是将宽度不等的多个黑条和空白,按照一定的编码规则排列,用以表达一组信息的图形标识符.条形码技术主要原理是利用了光的反射,白色物体能反射各种波长的可见光,黑色物体则吸收各 ...

  6. Word处理控件Aspose.Words功能演示:使用Java合并MS Word文档

    在各种情况下,可能需要合并多个MS Word文档,例如减少文档数量,在单个文件中保留相似种类的内容(即发票)等.许多在线应用程序使您可以合并两个或多个MS Word文档但是,您可能需要在自己的Web或 ...

  7. 文档处理教程:使用Java拆分MS Word文档

    在各种情况下,可能需要将MS Word文档拆分为多个文档.例如,您可能需要为Word文档中的每个页面,每个部分或页面集合创建一个单独的文档.为了自动进行文档拆分,本文介绍了如何使用Java以编程方式拆 ...

  8. linux 设置媒介类型,CUPS Linux:帮助打印这些媒体类型:MS Excel,MS Word和HTML

    我需要在 Linux CUPS服务器中打印MS Excel,MS Word和HTML文件.当我尝试打印这些媒体类型后发生,这是我发现到现在: >对于MS Excel和MS Word文件: 当我尝 ...

  9. Word处理控件Aspose.Words功能演示:使用 Java 在 MS Word 文档中进行邮件合并

    Aspose.Words 是一种高级Word文档处理API,用于执行各种文档管理和操作任务.API支持生成,修改,转换,呈现和打印文档,而无需在跨平台应用程序中直接使用Microsoft Word.此 ...

最新文章

  1. Hyperledger Fabric PHP SDK
  2. 字符串常量是一个字符数组
  3. 三十八、判断服务是否运行及定位问题
  4. 【Android 安装包优化】Android 中使用 SVG 图片 ( Android 5.0 以下的矢量图方案 | 矢量图生成为 PNG 图片 )
  5. python七:编码
  6. Poj - 3254 Corn Fields (状压DP)(入门)
  7. elasticsearch的cross_fields查询
  8. LeetCode 1111. 有效括号的嵌套深度(奇偶分离)
  9. 搭载麒麟990 5G SoC 华为MatePad Pro 5G全球首发
  10. 点击button自动提交表单原因及解决方案
  11. 【OpenCV/C++】KNN算法识别数字的实现原理与代码详解
  12. TreeMap、HashMap、LinkedHashMap的区别
  13. python range 和 xrange 区别
  14. 10.数据结构 --- 内部排序
  15. Element 表单样式调整
  16. oracle命令切换用户,Oracle 常用命令
  17. Vue实战篇一: 使用Vue搭建注册登录界面
  18. 在有n个学生的成绩表里,每条信息由姓名与分数组成,要求:1按分数高低次序,输出每个学生的名字,分数相同的为同一名次,2按名次输出每个学生的姓名与分数。
  19. 弗雷德里克·特曼:硅谷之父、斯坦福大学前副校长——(转自新浪网)
  20. 大学生信息安全(学习笔记一)

热门文章

  1. 一位台湾读者写的《I. M. Wright's Hard Code》第一章閱讀札記
  2. springboot 集成百度编辑器ueditor
  3. 打印服务器 linux,建立Windows环境下的Linux打印服务器
  4. 班组沟通与管理培训PPT模板
  5. 让32位应用程序不再为2G内存限制苦恼
  6. Java又双叒叕“凉”了?
  7. HMS生态的华为主题样式太丰富了
  8. respond_to的解释
  9. 1. 微博大学数学答疑系列(1)
  10. scribe、chukwa、kafka、flume日志系统对比