ACC97: How to Merge Pictures and Text from an Access Table with a Word Document Without Having to Store the Pictures (219054)



The information in this article applies to:

  • Microsoft Access 97

This article was previously published under Q219054
Moderate: Requires basic macro, coding, and interoperability skills.

SUMMARY

When you follow the usual procedures for merging a Microsoft Access table with a Microsoft Word document, the pictures that are stored in the Access table are not successfully merged into the Word document.

MORE INFORMATION

The following code sample demonstrates how to create a new Word document and how to merge the text from one field next to a picture taken from a text field in the Access table. The text field will contain only the path to the picture on the hard disk.
  1. Open an Access database and create a new table named Sample.
  2. Add two text fields to the table as follows:
    Field nameData typeField size
    DescriptionText50
    PictureText100
  3. Search your hard disk for files with the extension .jpg and populate the Samples table with several new records. Include a description that describes the picture, and the full path to the picture on the hard disk, for example:

    Description = "One Hundred"
    Picture = "C:\Program Files\Microsoft Office\Templates\Access\100.jpg"

  4. Type the following function into a new module:
    Option Compare Database
    Option Explicit
    
    Public Function Merg()
        Dim obj As Object
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
    
        Set db = CurrentDb
        Set rs = db.OpenRecordset("Sample")
        Set obj = CreateObject("Word.Application")
    
        obj.Application.Visible = True
    
        rs.MoveFirst
    
        'The following line assumes Word's Normal template is in the default
        'location.
        obj.Documents.Add Template:="C:\Program Files\Microsoft _
           Office\Templates\Normal.dot", NewTemplate:=False
    
        Do While Not rs.EOF
            With obj
              .Selection.TypeText Text:="" & rs![Description]
              .Selection.InlineShapes.AddPicture FileName:="" & rs![Picture], _
                  LinkToFile:=False, SaveWithDocument:=True
              .Selection.TypeText Text:=Chr(13)    'New Line.
              .Selection.InsertBreak Type:=3       'wdSectionBreakContinuous.
            End With
            rs.MoveNext
        Loop
    End Function
  5. Type the following line in the Immediate window, and then press ENTER:
    ?Merg()

REFERENCES

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:

131583 Sending the Current Record to Word 97 with Automation

114306 Inserting Database in Word Document Does Not Retain Pictures


Modification Type:MajorLast Reviewed:10/22/2003
Keywords:kbhowto KB219054