Use DDE to operate word

Source: Internet
Author: User
Use DDE to operate word

Windows supports three basic IPC (inter-process communication) Mechanisms: Shared data segments in the dynamic link library (DLL), clipboard, and dynamic data exchange DDE (dynamic ).
Data exchange ). Many well-known Windows applications such as Microsoft
Word and so on are announced to support the DDE technology, and the DDE message processing function is embedded in the program. Such applications are mostly stored as a DDE Server in DDE technology, which allows users to connect to the application as a DDE client through some peripheral software, and by sending some specific macro commands to the DDE Server program to achieve dynamic control of the server program, this article uses the common Microsoft
Word is used as an example to describe how to compile the DDE client program in the Delphi 5.0 programming environment so that it can dynamically control Microsoft Word.

Ii. Working Principle of DDE

As its name implies, DDE is a general technology that provides dynamic data exchange for different programs during operation. Although Windows messages are the best way to transmit information between different program windows, a message can only contain two parameters (wparam and lparam) and cannot transmit more information. Memory block is an important means to store more information, but it does not support sharing of global memory handles. DDE is a protocol based on the Windows internal message system, global atoms, and shared global memory. It is used to coordinate data exchange and command calls between Windows applications.

The DDE protocol uses three levels of naming: Service, topic, and item to identify the data unit transmitted by DDE. The topic is a meaningful unit of information for the server. It is a good topic for Word documents. Many servers have default themes, but they cannot know which topics the server has, unless you have read the relevant technical documents of the application. Each conversation between the DDE client and the service program is started by the customer first. Therefore, the DDE Server must be put into operation before each customer starts, the following is the transaction composition of a typical DDE session process:

. The client program automatically session and the server program responds.
. Customers and servers exchange data using the following methods:
. The server sends data to the customer at the customer's request;
. The customer actively wants the server to send data;
. The customer requires the server to send data (hot data connection) when data is modified );
. The customer requires the server to send a notification (warm data connection) after data modification );
. At the customer's requirement, the server executes a command.
. The session is aborted by the customer or server.

Iii. Design Ideas

First, Borland Delphi 5.0 with ready-made DDE series components is selected in the selection of development tools. Since Microsoft
As the server to be connected, you must set the Connection Service and topic before establishing a connection with word. In this example, you only need to use this program to control some operation actions of word, for example, to open a new file, close a file, and insert a table, you can set these two items as "winword" and "system ", after the connection is set, you can use the openlink function of the ddeclientconv component provided by Delphi to connect to the service. The rest of the work is to send macro commands to the word server, you can use the executemacro function of the component to execute macro commands.

Iv. Program Implementation

(1) Add the DDE client component

Create an app project worddde, and then
Select the ddeclientconv component on the system attribute page of the palette component bar and drag it to the form. Modify the name attribute to ddeclient.

(2) Add code for interaction with the DDE Service

Add a process runmacro to the project to open the link with the word server, and notify the server to execute the macro command identified by macro, so that word can complete the response action according to the user's intent. The customer disconnects the connection and completes a session. The following is the implementation code of the above process:

Procedure tform1.runmacro (macro: pchar );
VaR pmacro: array [0 .. 80] of char;
Begin
Ddeclient. setlink ('winword', 'system'); {set connection}
Ddeclient. openlink; {open the connection according to settings}
Strpcopy (pmacro, macro );
If not ddeclient. executemacro (pmacro, false) Then {execute macro command}
Showmessage ('unable to execute macro ');
Ddeclient. closelink; {disconnect}
End;

(3) macro Command Execution

Macro (macro) is the operation command that the client program needs to complete on the server.
It is nothing more than some macro commands for file opening, inserting delimiters, copying and pasting characters, etc, most of the functions completed by these macro commands can be found under the word menu. For example, you can use the macro [fileclose] to complete the "Close file" menu. You can add a button or menu to the window and add the code for executing the macro to its handler as follows:

Procedure tform1.n2click (Sender: tobject );
Begin
Runmacro ('[filenew]'); {let word create a new file, macro [filenew] by function runmacro notifies word}
End;

There are many macro commands that can be transferred and executed in Word. Some commonly used macros are listed as follows for use in actual programming:

[Filenew]... Create a new file
[Fileclose]…… Close file
[Filesave]... Save files
[Fileprint]... Print files
[Fileexit]... Exit word
[File1]... Open the recently opened files, including [file2] and [file3 ].
[Editcut]…… Cut operation
[Editcopy]... Copy operation
[Editpaste]... Paste operation
[Editundo]... Restore the previous step
[Editredo]... Redo the previous step
[Editclear]…… Clear operation
[Editselectall]... Select All
[Viewnormal]…… Normal View
[Viewpage]…… Page View
[Viewoutline]... Outline View
[Insertbreak]... Insert delimiter
[Insertindex]... Insert Index
[Formatnumber]…… Format the project symbol and number
[Toolsoptions]... Tool options
[Tableinserttable]... Insert table
[Tableinsertrow]... Insert row
[Tabledeleterow]... Delete row
[Tablesplit]…… Split table
[Tableselectrow]... Select row
[Tableselectcolumn]... Select Columns
[Tableselecttable]... Select Table
[Tablesort]... Sort
[Windownewwindow]... New window
[Window1]…… Recently opened windows and responses include [window2] and [window3 ].
[Helpindex]... Index for help
[Helpabout]... Help

Summary: The DDE technology provides users with a more integrated working environment, which is most suitable for Dynamic Data Exchange without user participation.
Through the above example, after having a certain understanding of the working principle and programming idea of DDE, you can use a similar method to implement the same as other programs such as Microsoft
Dynamic Interaction of DDE in execl. This program is compiled by Borland Delphi 5.0 in Windows 98.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.