how to control word from vb6

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
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
kamielvdbAuthor Commented:
thanks this working perfect
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.

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.