Building a Word Document using VB code

Posted on 2007-10-08
Last Modified: 2010-04-30
I need to read a database, and take the contents of the database and build a word document with the information using VB 6. Can someone point me in the right direction on how to do this?

Question by:samic400
    LVL 15

    Expert Comment

    This sounds like a job for mailmerge

    The best information on mailmerge I have found is here:
    LVL 17

    Accepted Solution

    Dim wordapp As New Word.Application
    Dim cn As New ADODB.connection
    Dim rs As New ADODB.Recordset

    Private Sub Form_Load()
    wordapp.Visible = True
    cn.Open "connection string"
    Set rs = New ADODB.Recordset
    rs.Open "select * from tablename", cn, adOpenKeyset, adLockOptimistic
    Do Until rs.EOF
    wordapp.Selection.TypeText Text:="Field 1" & rs.Fields(0) & vbCrLf
    wordapp.Selection.TypeText Text:="Field 2" & rs.Fields(1) & vbCrLf
    wordapp.Selection.TypeText Text:="Field 3" & rs.Fields(2) & vbCrLf
    wordapp.Selection.TypeText Text:="Field 4" & rs.Fields(3) & vbCrLf
    Set wordapp = Nothing

    End Sub

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    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…

    732 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

    17 Experts available now in Live!

    Get 1:1 Help Now