Microsoft KB Archive/97618

{|
 * width="100%"|

-

The information in this article applies to:


 * Microsoft Visual Basic Professional Edition for Windows, version 2.0
 * Microsoft Word for Windows, version 2.0

-

SUMMARY
This article shows by example how to use the object linking and embedding (OLE) client custom control (OLECLIEN.VBX) with Microsoft Word for Windows. The example demonstrates both how to embed and how to link a Word for Windows document into a Visual Basic application.

NOTE: In Word for Windows, version 6.0 or 6.0a, the Bookmark menu item moved from the Insert menu to the View menu.

MORE INFORMATION
Embedding an object encapsulates the data displayed in the Visual Basic OLE client control and makes the data inaccessible to other applications, unlike the data in an linked object. In addition, embedding an object does not require that a file already exist for the object to be usable.

Linking an object, on the other hand, does require that a file already exist, and it requires a LinkItem setting. For a Word for Windows document, the LinkItem can be any bookmark within the document.

The example shown below demonstrates how to use:


 * Embedded Word for Windows objects
 * Linked Word for Windows objects

The following OLE client control property settings are required to create a Word for Windows OLE object:

  Property   Value Class     &quot;WordDocument&quot; Protocol  &quot;StdFileEditing&quot;

In addition, linked objects require the following OLE client control property settings:

  Property     Value -  SourceDoc    The full path of the document to use (such as                 C:\OLETEST.DOC)

SourceItem  A bookmark (OLE_Link is used in this example)

Here are the steps you need to follow to create the example:

Step One: Create the Word for Windows Document You Want to Link Or Embed

 * 1) Start Word for Windows. Document1 is created by default.
 * 2) Press CTRL+SHIFT+END to select to the end of the document.
 * 3) From the Insert menu, choose Bookmark. Under Bookmark Name, type:

OLE_Link

and press ENTER to set a bookmark for the entire document. This bookmark functions as the LinkItem.
 * 1) From the File menu, choose Save As, and save the document with the name C:\OLETEST.DOC. (If the path is different, change the ServerDoc property on OleClient1 to reflect the correct path.)

Step Two: Create the Visual Basic Application That Will Hold the Document
 Start Visual Basic or from the File menu, choose New Project (ALT, F, N) if Visual Basic is already running. Form1 is created by default. From the File menu, choose Add File and add OLECLIEN.VBX to the project.  Add the following controls to Form1, and give them the properties shown:   Default Name   Caption                Name OleClient1    N/A                    OleClient1 Option1       &Embed Object          OptionEmbed Option2       &Link Object           OptionLink Command1      Embed WinWord Object   Command1  Change the Value property on OptionEmbed to True.  Add the following code to the general declarations section of Form1:   Dim fshowing As Integer

Const OLE_LINKED = 0 Const OLE_EMBEDDED = 1 Const OLE_STATIC = 2

Const OLE_CREATE = 0 Const OLE_CREATE_FROM_FILE = 1 Const OLE_UPDATE = 6 Const OLE_ACTIVATE = 7 Const OLE_DELETE = 10   Add the following code to the click event of Command1: <pre class="CODESAMP">  Sub Command1_Click

' Unload the current object so a new object can be loaded If fshowing Then OleClient1.Action = OLE_DELETE End If

OleClient1.Class = &quot;WordDocument&quot; OleClient1.Protocol = &quot;StdFileEditing&quot; If OptionEmbed Then ' Data is managed by Visual Basic OleClient1.ServerType = OLE_EMBEDDED OleClient1.Action = OLE_CREATE Else OleClient1.SourceDoc = &quot;C:\OLETEST.DOC&quot; OleClient1.SourceItem = &quot;OLE_Link&quot; OleClient1.ServerType = OLE_LINKED OleClient1.Action = OLE_CREATE_FROM_FILE End If     OleClient1.Action = OLE_UPDATE fshowing = True

End Sub </li>  Add the following code to the DblClick event of OleClient1: <pre class="CODESAMP">  Sub OleClient1_DblClick OleClient1.Action = OLE_ACTIVATE End Sub </li>  Add the following code to the Click event of OptionEmbed: <pre class="CODESAMP">  Sub OptionEmbed_Click Command1.Caption = &quot;Embed WinWord Object&quot; End Sub </li>  Add the following code to the Click event of OptionLink: <pre class="CODESAMP">  Sub OptionLink_Click Command1.Caption = &quot;Link WinWord Object&quot; End Sub </li> From the Run menu, choose Start (ALT+R, S) to run the program.</li> Click the Embed WinWord Object button to activate Word for Windows.</li> Type some text into the active Word document.</li> Close Word and click the Yes button when asked if you want to update the Object in OleClient1. The Word for Windows icon is painted in the OleClient1 control.</li> Double-click the OLE client control to reactivate Word and redisplay the text you entered.</li> Click OptionLink. The caption of button changes to Link WinWord Object.</li> Click the Link WinWord Object button. The Word icon remains in the OLE client control, however it is now linked to the document created in the first part of this example, not the embedded object.</li> Double-click the OLE client control to activate Word for Windows and redisplay the text you entered in the first document.</li></ol>

Additional query words: noupd

Keywords :

Version :

Platform :

Issue type :
 * }