Visual Basic Concepts

The Microsoft Binder as an ActiveX Container

The Microsoft Office Binder is an electronic "paper clip" — you can add several disparate kinds of documents to the Binder that have some relation to each other. "Documents" can include:

This topic covers the basics of using the Binder as a container of ActiveX documents.

Adding an ActiveX Document

There are two ways to add an ActiveX document to a Binder:

Adding the ActiveX Document as a Section

When you add an ActiveX document as a section, the Binder adds the document using its ProgID. When an ActiveX document is added this way, it's akin to adding a blank Word document — the document is a "blank" document. To identify the "blank" document, Binder looks for the ProgID of the ActiveX document. In brief, the ProgId (or programmatic ID) is the class name and the component name separated by a period. For example, the ProgID of an ActiveX document with no changes to the default settings would be "Project1.UserDocument1".

Note   When you add a new section, you are create a new object from the ActiveX document class. Thus the InitProperties event will always occur (instead of the ReadProperties event).

For More Information   For details about Programmatic IDs, see "Adding Classes to Components" in "General Principles of Component Design."

To add an ActiveX document as a section

  1. Start Microsoft Binder.

  2. On the Section menu, click Add to open the Add Section dialog box.

  3. From the scrollable list of ProgIDs, double-click your ActiveX document.

Adding the ActiveX Document as a File

When you add an ActiveX document as a file, you can select a particular .vbd file that you may have saved earlier.

Note   If you have created an ActiveX document that persists data (through the PropertyBag), and you have saved data to the file, adding that .vbd file will cause the ReadProperties event to occur. But if you did not write code to persist data, the InitProperties event will fire instead. For details on persisting data, see "Persisting ActiveX Document Data" in this chapter.

To add an ActiveX document as a section

  1. Start Microsoft Binder

  2. On the Section menu, click Add from File to open the Add from File dialog box.

  3. Navigate to your .vbd file, and click Add.

Saving an ActiveX Document in the Binder

As with any other document in the binder, you can save the ActiveX document as one of a group of documents. When you save the ActiveX document in this way, the WriteProperties event will fire. However, to actually persist data in the document, you must have written the proper code in the WriteProperties event.

To save an ActiveX document in the Binder

  1. Add the ActiveX document, either as a section or a file.

  2. On the File menu, click Save Binder to open the Save Binder As dialog box.

  3. Type the name of the Binder and click Save.

Programming the Binder

You can create ActiveX documents that programmatically manipulate other sections. In order to do this, you must know the object model of the Binder.

The Binder's object model is built around a collection of Section objects, the Sections collection. And each Section object can contain a Microsoft Excel, Word, PowerPoint, or Visual Basic ActiveX document. Using this knowledge, it's possible to walk through the Sections and get a reference to each document. Using that reference, you can then manipulate the document's objects. This is easier to explain in a simple procedure.

Example: Manipulating an Excel Spreadsheet

The following scenario is necessarily simple: add an Excel worksheet and an ActiveX document to the Binder, and using an ActiveX document, retrieve a value from the worksheet.

To retrieve a value from an Excel worksheet

  1. In Visual Basic, on the File menu, click New Project. Then click the ActiveX Document DLL icon.

  2. In the Project Explorer window, double-click UserDocument1 to show its designer.

  3. Draw a TextBox control on the designer, and change its name to "txtCell"

  4. Draw a CommandButton control on the designer, and change it's caption to "Get Value".

  5. On the Project menu, click References to show the References dialog box.

  6. From the scrolling list of references, click the OLE Automation Binder 1.0 Type Library checkbox.

  7. Double-click the CommandButton to show its code window.

  8. Add the following code to the Command1 click event.
    Private Sub Command1_Click()
        Dim i As Integer ' Counter
        Dim objX As Sections ' Object variable
        Set objX = UserDocument.Parent.Parent.Sections
        For i = 1 to objX.Count
            If TypeName(objX.Item(i).Object) = _
                "Worksheet" Then
                txtCell = objeX.Item(i).Object.Range. _
                    ("A1").Value
            End If
        Next i
    End Sub
    
  9. Press F5 to run the project.

  10. Minimize the Visual Basic instance.

  11. Start Microsoft Binder.

  12. On the Section menu, click Add Section.

  13. Double-click Microsoft Excel Worksheet. A blank worksheet is now added to the Binder.

  14. Type a distinctive number or text into cell A1 (it's already selected for you, so you can simply begin typing).

  15. On the Section menu, click Add Section.

  16. Double-click Project1.UserDocument1.

  17. Click Get Value. The text or number you just typed into the spreadsheet will appear in the textbox.

The Code Explained

Because we want to early-bind the object variable, step 6 instructs you to add a reference to the OLE Automation Binder Type Library. Thus in the code, we declare the object variable of type Sections. The code then sets the variable to the Binder's Sections collection.

Once we have a reference to the collection, we can iterate through its members using the For statement. Within the For look, we then use the TypeName function to return the type of each object in the section. If the TypeName function returns "Worksheet," we know that it's the Excel worksheet in the collection. Having determined that, the code simply returns the contents of cell A1 to the TextBox control.

Another Example: Adding a Second ActiveX Object

Adding another ActiveX document to the Binder is also accomplished using the Sections collection. This time, however, we use the Add method of the Sections collection.

Option Explicit
Private mMyDoc2 As axdDoc2 ' Module level variable

Private Sub AddDoc_Click()
   Dim objX As Sections ' Object variable
   Set objX = UserDocument.Parent.Parent.Sections
   Set mMyDoc2 = objX.Add( , "c:\axdDoc2.vbd")
End Sub

The preceding code again uses the Sections collection, but instead uses the Add method to add a second ActiveX document to the binder. You may have noticed that the code also declares a module-level variable as type axdDoc2 (assuming that is the name of the second document). The code sets the object variable to the reference returned by the Add method. With that reference, you can then access the public properties and methods of the second document, as shown:

txtName.Text = mMyDoc2.Name   
   ' Assuming there's a public property called Name.
mMyDoc.MyMethod ' Assuming a public function exists.