Results 1 to 6 of 6

Thread: Word Document

  1. #1

    Thread Starter
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696

    Question

    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!

  2. #2
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593
    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]

  3. #3

    Thread Starter
    Fanatic Member Ianpbaker's Avatar
    Join Date
    Mar 2000
    Location
    Hastings
    Posts
    696
    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!

  4. #4
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Exclamation 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.

  5. #5
    Junior Member
    Join Date
    May 2000
    Location
    Alaska
    Posts
    21

    Thumbs up

    Impressive Haxsoft.
    Alaska

  6. #6
    Fanatic Member HaxSoft's Avatar
    Join Date
    May 2000
    Location
    Ohio
    Posts
    593

    Smile

    Well, thank you, Tyler

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width