Results 1 to 6 of 6

Thread: Access To Word_Document From VB

  1. #1

    Thread Starter
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249

    Thumbs up

    Hi,

    How can i access a word document and "put" some data in that document ? print the doc?

    thanks

  2. #2
    New Member
    Join Date
    Jan 1999
    Posts
    7
    Hi,
    Have you try thru VBA?

  3. #3
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    Opening a word document in Visual Basic :

    Code:
    Dim oWord as Object
    Dim sWordFile as String
    
    sWordFile = "C:\Folder\Document.doc"
    
    Set oWord = GetObject(sWordFile, "Word.Document")
    
    '------- Its now open ---------------
    Msgbox oWord.Application.Documents(1).Name
    
    .
    .
    .
    
    '------- Now close it and automatically save it
    oWord.Application Quit 0
    Set oWord = Nothing
    Once you have opened the document you can perform ANY VBA function on the document using the oWord object.

    I would suggest learning how do to everything you wanted from VBA first (Load Word, Press Alt-F11 to get to the VB Editor and start learning) and then migrate it to word.

    There are heaps of examples in the VB Word Help files.

  4. #4

    Thread Starter
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    GenX

    Thanks for the quick reply, but i need to read data and put the data in the document (the header is already there).

    is it good to inplement by VBA or VB?

    Thanks

  5. #5
    Hyperactive Member
    Join Date
    Mar 2000
    Posts
    461
    This might get a bit confusing but I will try and explain.


    VBA is the language used to run Macros in Word.

    That means that if you use VBA you will be writing Macros that exist in either the Normal.dot or another template that you have as an add-in.

    If you want to do this from VB you actually USE the VBA calls to objects and attributes.

    So like I said in my previous post.... FIRST learn how to use VBA to actually do what you want and then when you get to VB you replace the reference to objects with the object model I gave you instead of just using VBA.

    Code:
    ' In VBA
    
    ActiveDocument.Select
    Selection.TypeText "Fred Was Here"
    
    ' In VB
    
    oWord.Application.Documents(1).Select
    oWord.Application.Selection.TypeTExt "Fred Was Here"
    Now don't quote me Word for Word on the code... I only pulled that off the top of my head to give you an idea of how things work... you will need to do the proper syntax yourself.

    But the bottom line is that using Word through Visual Basic is a matter of using the Word objects which is exactly what you do in VBA but in slightly different ways.

  6. #6

    Thread Starter
    Addicted Member Tiovital's Avatar
    Join Date
    Apr 2000
    Posts
    249
    GenX,
    THANX

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