Visual Basic Concepts
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.
There are two ways to add an ActiveX document to a Binder:
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
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
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
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.
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
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
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.
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.