Bond
06-10-2004, 07:34 AM
In your VB project, add a reference to "Microsoft Word x.0 Object Library" where x is the version of Word that is installed (9 = 97, 10 = 2000, 11 = 2002/XP). Add this reference by clicking the Project menu, then selecting References. Then, select the one mentioned above and click OK.
Now, you will be able to use the Word object library and the intellisense features to make life easier. For example, in your code, add something like this:
Dim w as Word.Application
Set w = New Word.Application
As you type, VB's intellisense should allow you to see the Word object library and the available choices.
Now, add this:
Dim doc as Word.Document
Set doc = w.Documents.Add
Again, the intellisense feature should guide you through this.
Finally, type:
doc.PrintOut
and you will see the available parameters (Range, From, To, Copies, Pages, etc.). You don't need to specify ALL of them -- just the ones you plan to use. For example:
doc.PrintOut Copies:=1, Pages:=1
or whatever is needed.
To see all of Word's available objects/methods/properties, press the F2 key to bring up the Object Browser. Select "Word" from the list of libraries in the upper left corner of the window, then select "Document" from the list of objects in the left pane. "PrintOut" should be a method in the right pane.
The Object Browser is convenient for seeing a library's objects, enums, etc., as well as an object's methods and properties. However, it won't tell you much about how to use them. You will have to consult the VB section in Word itself to find out more. Try opening Word, then hit Alt+F11 to bring up the VBA editor, then hit F1 to bring up help.
Also note that in the example above, we never actually add any text to the document! You're just bringing up a blank document and printing it. You will need to add your text to the doc first.
Now, you will be able to use the Word object library and the intellisense features to make life easier. For example, in your code, add something like this:
Dim w as Word.Application
Set w = New Word.Application
As you type, VB's intellisense should allow you to see the Word object library and the available choices.
Now, add this:
Dim doc as Word.Document
Set doc = w.Documents.Add
Again, the intellisense feature should guide you through this.
Finally, type:
doc.PrintOut
and you will see the available parameters (Range, From, To, Copies, Pages, etc.). You don't need to specify ALL of them -- just the ones you plan to use. For example:
doc.PrintOut Copies:=1, Pages:=1
or whatever is needed.
To see all of Word's available objects/methods/properties, press the F2 key to bring up the Object Browser. Select "Word" from the list of libraries in the upper left corner of the window, then select "Document" from the list of objects in the left pane. "PrintOut" should be a method in the right pane.
The Object Browser is convenient for seeing a library's objects, enums, etc., as well as an object's methods and properties. However, it won't tell you much about how to use them. You will have to consult the VB section in Word itself to find out more. Try opening Word, then hit Alt+F11 to bring up the VBA editor, then hit F1 to bring up help.
Also note that in the example above, we never actually add any text to the document! You're just bringing up a blank document and printing it. You will need to add your text to the doc first.