how to control word from vb6

Posted on 2006-03-22
Last Modified: 2012-06-27
i want to open a word document from vb6, sed some text to the document en the user can complete the document.
When the document close i most have the control back to my vb6 program
Question by:kamielvdb
    LVL 5

    Accepted Solution

    Hi Kamiel,

    In order to control word, you'd best use the Word object model. To do this, reference the "Microsoft Word X.X object model" where X.X may be 9.0 or 8.0 or 10.0, depending on your version of Word.

    For what I understand, you have some word template that you want to complete with some data that's known within your VB program. (A customer name and address for example). Then the user will edit the document/letter in order to save/send it.

    Here's an example of a VB program that uses word to open an existing document and sends text to predefined bookmarks within that document. I'm not 100% if this is your way to go. If not, please let me know and I may be able to provide a different example.

    Option Explicit

    Private Const m_cTemplate = "c:\documents and settings\Luc\My Documents\Calculation.doc"
    Private WithEvents m_objWord As Word.Application
    Private WithEvents m_objDoc As Word.Document

    Private Sub m_objDoc_Close()
       Set m_objDoc = Nothing
       'Enable this form and activate it; the document has been closed
       Me.Enabled = True
    End Sub

    Private Sub m_objWord_Quit()
       Set m_objWord = Nothing
    End Sub

    Private Sub ToWord_Click()
       'This event will open Microsoft word, opens an existing document and edits it.
       'Get the word application if it already exists.
       On Error Resume Next
       Set m_objWord = GetObject(, "Word.Application")
       On Error GoTo 0
       'If it does not yet exist, start a new word instance
       If m_objWord Is Nothing Then Set m_objWord = New Word.Application
       'Open the (template) word document and show word
       Set m_objDoc = m_objWord.Documents.Open(m_cTemplate, , False, True, , , , , , , , True)
       m_objWord.Visible = True
       'Disable this form (until the document is closed)
       Me.Enabled = False
       'Set a bookmark
       m_objWord.Selection.TypeText "Google corp."
       m_objWord.Selection.TypeText "Somewhere"
    End Sub

    I hope this example helps.

    One more advice: If you completed building the application, try to use late binding, so you don't rely on one specific version of word. Replace the declarations from Word.Document and Word.Application etc to Object, drop the reference to "Microsoft Word X.X Object Model" and replace the Set ... = New Word.... to Set ... = CreateObject("Word....")


    Luc Derckx

    Author Comment

    thanks this working perfect
    LVL 5

    Expert Comment

    Hi Kamiel,

    Thanks for the grade. I'm glad I could help.

    I have another advice for you which helped me perfectly while I was programming Word Automation: If you don't know how to use the Word object model to do something, simply open word and do the same as you want to do from code while recording a macro. Take a look at the macro once you're done and see how the macro uses the object model to do what you want to do from code. It always pointed me in the right direction.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now