Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

how to control word from vb6

Posted on 2006-03-22
3
Medium Priority
?
1,143 Views
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
0
Comment
Question by:kamielvdb
  • 2
3 Comments
 
LVL 5

Accepted Solution

by:
IThema earned 1000 total points
ID: 16258732
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
   Me.SetFocus
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_objDoc.Bookmarks("CustomerName").Select
   m_objWord.Selection.TypeText "Google corp."
   
   m_objDoc.Bookmarks("CustomerAddress").Select
   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....")


Cheers,

Luc Derckx
0
 

Author Comment

by:kamielvdb
ID: 16267402
thanks this working perfect
0
 
LVL 5

Expert Comment

by:IThema
ID: 16267436
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.


Cheers
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
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 Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Suggested Courses
Course of the Month13 days, 3 hours left to enroll

578 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