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.
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.
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:
This code will open a new instance of Word.
Open a new document and insert the text Hello world! !
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.
Both these command buttons should open an instance of word with the document we specified loaded