PB uses Word by oleobject

xiaoxiao2021-03-06  61

1 Introduction

When programming, you often need to access the Word document. In addition to using DDE, PowerBuilder has a way to operate Word, which is through oleobjcet and Olecontrol.

OleObject is an invisible OLE object, while OleControl is a visible window control. Since oleobject is more flexible and convenient, if you don't have to display Word document on the window, use the former a better choice.

The program developer often encounters two problems when using the OLE to connect to the Word document:

First: If a document has been opened, the program run error will be generated when using the program call;

Second: Frequently found that the created Winword.exe process is not able to terminate normally.

2 analysis

Let's analyze the causes of these two errors.

For the first error, it is often due to oleObjectVar.connectToObject (""), This file has been opened. The use of ConnectToObject can cause errors.

For the second error, it is generally created a new Winword.exe process using oleObjectvar.connecttonewObject ("Word. Application"), but does not perform the operation of turning off this new application.

3 solution

After analysis, we can determine how to solve the problem of PB calling Word.

Open a document

First, we use .connectToobject ("" to open an empty Word document. After opening success, use .Application to get reference to Word.Application and set Applicatin.visible = true, and turn off the empty document you just opened;

Then, open the document you want to use using Word.Application Open method.

This way opens the document. If you currently run the Windows.exe process, you don't have to open a new process, but use the current process and you can successfully get a reference.

Close the document

First, let's turn off the documentation used by the program;

Then determine if the Application has other open documents. If there is no other open document, call the Application's Quit method, let the Application exits, end the program process of Winword.exe. If you are determined to call Word in the future, you can do not turn off the WiNword.exe process so you can open the Word document each time.

4 considerations

There are many restrictions on OleObject calling Word. Although this method can solve many problems, there are still many places that need special attention.

First, use the document connected to the ConnectToobject that cannot be locked (for example, it is open to the previous process, or the newly established document is used as a template, otherwise the unknown error will be generated (return value -9);

Second, for the various operations of Document, to ensure Word.Application.visible = true, otherwise the call may generate a program error.

转载请注明原文地址:https://www.9cbs.com/read-112414.html

New Post(0)