OLE Embedding & Linking Word for Windows Objects into VB Apps

ID: Q97618


The information in this article applies to:


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:

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

   Property   Value
   ----------------------------
   Class      "WordDocument"
   Protocol   "StdFileEditing" 

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.


  4. 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

  1. 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.


  2. From the File menu, choose Add File and add OLECLIEN.VBX to the project.


  3. 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
     


  4. Change the Value property on OptionEmbed to True.


  5. 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
     


  6. Add the following code to the click event of Command1:
    
       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 = "WordDocument"
          OleClient1.Protocol = "StdFileEditing"
          If OptionEmbed Then
             ' Data is managed by Visual Basic
             OleClient1.ServerType = OLE_EMBEDDED
             OleClient1.Action = OLE_CREATE
          Else
             OleClient1.SourceDoc = "C:\OLETEST.DOC"
             OleClient1.SourceItem = "OLE_Link"
             OleClient1.ServerType = OLE_LINKED
             OleClient1.Action = OLE_CREATE_FROM_FILE
          End If
          OleClient1.Action = OLE_UPDATE
          fshowing = True
    
       End Sub
     


  7. Add the following code to the DblClick event of OleClient1:
    
       Sub OleClient1_DblClick ()
          OleClient1.Action = OLE_ACTIVATE
       End Sub
     


  8. Add the following code to the Click event of OptionEmbed:
    
       Sub OptionEmbed_Click ()
          Command1.Caption = "Embed WinWord Object"
       End Sub
     


  9. Add the following code to the Click event of OptionLink:
    
       Sub OptionLink_Click ()
          Command1.Caption = "Link WinWord Object"
       End Sub
     


  10. From the Run menu, choose Start (ALT+R, S) to run the program.


  11. Click the Embed WinWord Object button to activate Word for Windows.


  12. Type some text into the active Word document.


  13. 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.


  14. Double-click the OLE client control to reactivate Word and redisplay the text you entered.


  15. Click OptionLink. The caption of button changes to Link WinWord Object.


  16. 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.


  17. Double-click the OLE client control to activate Word for Windows and redisplay the text you entered in the first document.


Additional query words: noupd


Keywords          : 
Version           : 
Platform          : 
Issue type        : 

Last Reviewed: June 1, 1999