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
Printable View
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
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:
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).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
I hope this gets you started.
[Edited by HaxSoft on 07-19-2000 at 06:16 PM]
Thanks A lot. That was a great help
Ian
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.
Impressive Haxsoft.
Well, thank you, Tyler :)