Results 1 to 4 of 4

Thread: Database fieldsin a Word document

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Littlehampton, W Sussex GB
    Posts
    203
    Does anyone know how I can put fields from my database in a Word document using VB code (a bit like a mail merge but just for the opened doc and only from one record on the database?)

  2. #2
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    you need special tables or anything or you just need to put the text in the doc?

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 1999
    Location
    Littlehampton, W Sussex GB
    Posts
    203
    Just address and salutation from the database - but automatically have it in the word doc when I open it from my app.

  4. #4
    Guru Clunietp's Avatar
    Join Date
    Oct 1999
    Location
    USA
    Posts
    1,844
    here ya go
    Code:
    'uses ADO 2.x and MS Word object library
        
        On Error GoTo Err_Handler
        
        Dim cn As ADODB.Connection
        Dim rs As ADODB.Recordset
        Dim objWord As Word.Application
        Dim objDoc As Word.Document
        
        Set cn = New Connection
        Set rs = New Recordset
        
        cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\nwind.mdb"
        
        rs.Open "Select * from Customers", cn, adOpenStatic, adLockReadOnly, adCmdText
        
        If rs.EOF = False Then
            'use the first record
            Set objWord = New Word.Application
            objWord.Visible = True
            
            Set objDoc = objWord.Documents.Add
            
            'document text goes here
            objDoc.Content.Text = "Dear " & rs.Fields("ContactName") & "," & _
                vbCrLf & vbCrLf & "If you don't pay your bill of " & _
                "$1, we will break your kneecaps.  Thank you"
                    
            objDoc.Activate
        End If
        
        cn.Close
        Set cn = Nothing
        Set objDoc = Nothing
        Set objWord = Nothing
        
        
        Exit Sub
    Err_Handler:
            
        On Error Resume Next
        
            
        'quit & release word object, avoid crashes if errors
        objWord.Quit False
        
        Set objWord = Nothing
        
        MsgBox Err.Description

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