ommunicating with Other Applicatio...
Communicating with Other Applications
Word Developer Reference |
In addition to working with Microsoft Office Word data, you may want your application to exchange data with other applications, such as Microsoft Excel, Microsoft PowerPoint, or Microsoft Access. You can communicate with other applications by using Automation (formerly OLE Automation) or dynamic data exchange (DDE).
Automating Word from another application
Automation allows you to return, edit, and export data by referencing another application's objects, properties, and methods. Application objects that can be referenced by another application are called Automation objects.
The first step toward making Microsoft Word available to another application for Automation is to make a reference to the Word Application object. In Microsoft Visual Basic, you use the Visual Basic CreateObject or GetObject function to return a reference to the Word Application object. For example, in a Microsoft Office Excel procedure, you could use the following instruction.
Set wrd = CreateObject("Word.Application") |
This instruction makes the Application object in Word available for Automation. Using the objects, properties, and methods of the Word Application object, you can control Word. For example, the following instruction creates a new Word document.
wrd.Documents.Add |
Use the Visible property to make the new document visible after creating it.
wrd.Visible = True |
The CreateObject function starts a Word session that Automation will not close when the object variable that references the Application object expires. Setting the object reference to the Visual Basic Nothing keyword will not close Word. Instead, use the Quit method to close the Word application. The following Microsoft Excel example displays the Word startup path. The Quit method is used to close the new instance of Word after the startup path is displayed.
Set wrd = CreateObject("Word.Application") MsgBox wrd.Options.DefaultFilePath(wdStartupPath) wrd.Quit |
Automating another application from Word
To exchange data with another application using Automation from Word, you first obtain a reference to the application using the CreateObject or GetObject function. Then, using the objects, properties, and methods of the other application, you add, change, or delete information. When you finish making your changes, close the application. The following Word example displays the Microsoft Excel startup path. You can use the Visual Basic Set statement with the Nothing keyword to clear an object variable, which has the same effect as closing the application.
Set myobject = CreateObject("Excel.Application") MsgBox myobject.StartupPath Set myobject = Nothing |
Using dynamic data exchange (DDE)
If an application does not support Automation, DDE may be an alternative. DDE is a protocol that permits two applications to communicate by continuously and automatically exchanging data through a DDE "channel." To control a DDE conversation between two applications, you establish a channel, select a topic, request and send data, and then close the channel. The following table lists the tasks that Word performs with DDE and the methods used to control each task in Visual Basic.
Security Dynamic data exchange (DDE) is an older technology that is not secure. If possible, use a more secure alternative to DDE, such as object linking and embedding (OLE).
Task |
Method |
Starting DDE |
|
Getting text from another application |
|
Sending text to another application |
|
Carrying out a command in another application |
|
Close DDE channel |
|
Close all DDE channels |
在Perl开发中,很可能会碰到一些不同格式的文件——XML、PDF、CSV及RSS文件等,和一些不同的二进制数据格式。Perl应用程序需要操作这些文件,对它们进行读写。
此时,可以求助于全面Perl档案网络(CPAN),简化文件操作工作。CPAN拥有数量庞大、用于处理不同文件格式的Perl模块。本文列出了10个这样的模块,开发人员可以使用它们对自定义文件格式进行操作。
注意:可以从网络直接安装CPAN,下面的链接提供了使用说明:
http://www.cpan.org/misc/cpan-faq.html#How_install_Perl_modules
表 A
包名
描述
DBI
此模块提供了不依赖数据库的包装,允许Perl与数据库进行通讯。它支持数据库连接和管理,支持执行查询、事务、存储程序和出错处理。通过添加数据库驱动程序,它能支持不同数据库,如:MySQL、 PostgreSQL 和Oracle等。
需要从RDBMS中得到数据时,使用此包。
Archive::Tar
此模块通过一个简单的面向对象界面,对TAR档案文件进行读和写操作。它允许将多个文件打包到一个TAR文件、允许列出TAR文件的内容,并且支持选择性地从TAR文件中提取文件。它也可用于GZIP压缩文件。
需要创建TAR或GZ档案文件或从档案文件中提取文件时,使用此包。
Archive::Zip
此模块用于Perl程序读写ZIP档案文件。它提供了OOP方法创建和读ZIP文件,并且支持向档案文件中添加新文件、更改档案文件的内容(包括更改名字和日期)、提取档案文件内容或查询文件信息。
需要创建ZIP文件或需要从ZIP档案文件中提取文件时,使用此模块。
Class::CSV
此模块提供基于对象的构架,对逗号分隔的数据进行有效处理。它允许CSV文件作为对象出现(反之亦然),可以将逗号间隔的值读入Perl变量。它也包含了使用自定义模式和分隔符向文件中写入新CSV记录的方法。
需要操作逗号分隔的数据字段时,使用此模块。
Unix::PasswdFile
此模块提供面向对象界面,操作不同类型的密码文件。它包含添加、编辑和删除用户和密码的方法,也包含验证密码和产生密码的方法。它也可用于密码