|
-
Jul 18th, 2000, 10:38 AM
#1
Thread Starter
Fanatic Member
Has any one got any source code for creating a simple word document, as I have been fiddling around and I can't seem to get it to work.
Cheers All
Ian
Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!
-
Jul 18th, 2000, 09:45 PM
#2
Fanatic Member
I used MS Word 2000 for this example, but the code is the same for Word 97. Under Project\References, add a reference to the C:\Program Files\Microsoft Office\Office\MSWORD9.OLB (Word 2000) ... For Word 97, I think the name was MSWORD8.OLB, but I am not sure.
The example below assumes that you have a form with three text boxes; txtFirstName, txtLastName, and txtMessage. Also, the form should have a check box that reads something like "Make Name Bold" and it should be called chkBoldName. Finally, add a command button to the form and enter the following code in the button's Click event, e.g. Command1_Click:
Code:
'*** Object Variable for MS Word.
Dim wrd As New Word.Application
With wrd
.Visible = True ' make us see it work
'.Visible = False ' uncomment to hide
.Documents.Add ' create new document
' The Selection object is more or less equal to the
' insertion point (cursor), or the selected area.
With .Selection
' Make the font bold if necessary (it is normal by
' default).
If chkBoldName = vbChecked Then .Font.Bold = True
' Now, we just type away with the TypeText method.
.TypeText txtFirstName & " "
.TypeText txtLastName
.Font.Bold = False ' set to normal
.TypeParagraph ' hit <ENTER>
.TypeText txtMessage
End With
.Quit ' QUIT the application
End With
Suggestion: Use the Word Macro Recorder to record macros. Then open the generated code and copy it to your VB project. You may have to re-write it a bit, but it is a pretty fast way to learn the Word objects and their methods (and there are MANY).
I hope this gets you started.
[Edited by HaxSoft on 07-19-2000 at 06:16 PM]
-
Jul 19th, 2000, 03:04 AM
#3
Thread Starter
Fanatic Member
Thanks A lot. That was a great help
Ian
Yeah, well I'm gonna build my own lunar space lander! With blackjack aaaaannd Hookers! Actually, forget the space lander, and the blackjack. Ahhhh forget the whole thing!
-
Jul 19th, 2000, 12:11 PM
#4
Fanatic Member
By the way
I forgot to say this It is important to call the Quit method rather than just Set wrd = Nothing.
If Word crashes or you don't call the Quit method, Word will still be running "silently" or hidden. This eats some memory. There are some details on that in the Visual Basic documentation ... either the Books Online/MSDN Lib or in the Word help.
Just thought you might wanna know that.
-
Jul 19th, 2000, 12:17 PM
#5
Junior Member
-
Jul 19th, 2000, 01:44 PM
#6
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|