Late Binding

CreateObject

Syntax

CreateObject(class)

The class argument uses the syntax appname.objecttype and has these parts:

Part Description
appname Required; Variant (String). The name of the application providing the object.
Example: Word or Excel or Access
objecttype Required; Variant (String). The type or class of object to create.
Example: Application or Document or Worksheet
The Application being used the most because all Office Apps have an application object

Remarks

Every application that supports Automation provides at least one type of object. For example, a word processing application may provide an Application object, a Document object, and a Toolbar object.

Example:
Dim ObjWord As ObjectSet ObjWord = CreateObject("word.application")

ObjWord.Visible = True

ObjWord.Documents.Open ("C:/test.doc")* Put a path to a word doc here

Note: Use CreateObject when there is no current instance of the object. If an instance of the objectalready running, a new instance is started, and an object of the specified type is created.To use the current instance, or to start the application and have it load a file, use the GetObject function.

If an object has registered itself as a single-instance object, only one instance of the object is created matter how many times CreateObject is executed.


GetObject

Syntax

GetObject([pathname] [, class])

The GetObject function syntax has these named arguments:

Part Description
pathname Optional; Variant (String). The full path and name of the file containing the object to retrieve. If pathname is omitted, class is required.
class Optional; Variant (String). A string representing the class of the object.
The class argument uses the syntax appname.objecttype and has these parts:

Part Description
appname Required; Variant (String). The name of the application providing the object.
objecttype Required; Variant (String). The type or class of object to create.

Remarks

Use the GetObject fUnction to access an ActiveX object from a file and assign the object to an object variable. Use the Set statement to assign the object returned by GetObject to the object variable. For example:

Dim ObjWord As Object

Set ObjWord = GetObj ect("d:/classsamples/test.doc") * Put a path to a word doc here

ObjWord.Application.Visible = True

ObjWord.Application.Documents.Open( "d:/test.doc")* Put a path to a word doc here

When this code is executed, the application associated with the specified pathname is started and the object in the specified file is activated.

If pathname is a zero-length string (""), GetObject returns a new object instance of the specified type. If the pathname argument is omitted, GetObject returns a currently active object of the specified type. If no object of the specified type exists, an error occurs.

Note Use the GetObject function when there is a current instance of the object or if you want to create the object with a file already loaded. If there is no current instance, and you don't want the object started with a file loaded, use the CreateObject function.

With a single-instance obiect, GetObject always returns the same instance when called with the zero-length String ("") syntax, and it causes an error if the pathname argument is omitted. You can't use GetObject to obtain a reference to a class created with Visual Basic.


In-line Binding & Early Binding

For Both In-line Binding and Early Binding you have to make sure that the object library for the particular object model is selected in Project References. In this case it would be Word 8.0 (Office 97).

The Office 97 object model is different from the Office 95 object model. So some of the code that worked in Office 95 may not still work in Office 97.

There is no actual Office 95 object model for example there is no Word 7.0 object model like there is a Word 8.0. You can use the previous version of product object model or use late binding. The best way to find out what properties and methods are in an object model is to use the Object Browser. The object browser shows you what properties and method are exposed for any object. The best way to learn this is to just play around with the different object models and see what works

In-line Binding Example:Dim ObjWord As Word.Application

Set ObjWord = New Word.Application

ObjWord.Visible = True

ObjWor.Documents.Open ("d:/textfiles/other/call.doc")* Put a path to a word doc here

Early Binding Example:Dim ObjWord As New Word.Application

ObjWord.Visible = True

ObjWord.Documents.Open ("d:/textfiles/other/call.doc")* Put a path to a word doc here


Class Example:

  1. Start a new Project
  2. Add 2 command buttons to your form.
  3. Change the caption of the 1st command button to CreateObject.
  4. Change the caption of the 2nd command button to GetObject.
  5. Put this code under the command button name CreateObject's click event. Dim objword As Object
    Set objword = CreateObject("word.application")
    objword.Visible = True
    objword.Documents.Add
    objword.Selection.Text = "Hello World!!!"
  6. This code will open a new instance of Word.
    Open a new document and insert the text Hello world! !

  7. Put this code under the command button name GetObject's click event.
    Dim ObjWord As Object
    Set ObjWord = GetObject("d:/classsamples/test.doc")* Put a path to a word doc here
    ObjWord.Application.Visible = True
    ObjWord.Application.Documents.Open ("d :/classsamples/test.doc ")* Put a path to a word doc here
  8. This code will open the Word document specified in the pathname argument. If Word is already running it will not create another instance of Word. If Word is not running it will start up a new instance of Word.

  9. Now run the project and click on CreateObject command button. You should see word open with a new document and the text Hello World!!!
  10. Go back to your project and click on the GetObject command button. It should open the instance of word that we already opened with the document we specified.
  11. Close Word.
  12. Stop the program.
  13. Go to Project/References and add the Microsoft Word 8.0 Object Library.
  14. Add 2 more command buttons to your form.
  15. Change the caption of the one of command button to Early Binding.
  16. Change the caption of the other command button to Inline Binding.
  17. Put this code under the command button named In-Line Binding's click event.
    Set ObjWord = New Word.Application
    ObjWord.Visible = True
  18. Put this code under the command button named Early Binding's click event.
    Dim ObjWord As New Word.Application
    ObjWord.Visible = True
    ObjWord.Documents.Open ("d:/classsamples/test.doc") * Put a path to a word doc here
  19. Both these command buttons should open an instance of word with the document we specified loaded


Portions of the above information taken from the VB5 Help files. Copyright Microsoft.

[Home]   [Back to the top]


Hosted by www.Geocities.ws

1