PDA

View Full Version : Extracting an Attachment from Access 2010 using Word VBA



Roderick
05-25-2013, 06:35 AM
I'm using Word 2010 and Access 2010. In Access I have a database with a table comprised of two fields: one is my label name and the other is an attachment field which holds .jpg graphics (small logos).

In my word template I have a procedure which looks down the list of the Access table, finds the label name I've provided and then looks at the second field and should - hopefully - extract the logo and place it on the Word document.

Alas, no such luck!

Here is my code as it stands:

Option Explicit
Private m_oConn As Object 'New ADODB.Connection
Private m_oRecordSet As Object 'New ADODB.Recordset
Private m_lngNumRecs As Long, m_lngIndex As Long
Private m_strWidth As String
Dim m_strConnection As String
Dim myMessage As String 'MsgBox below

Sub GetDataFromDataBase()
Dim arrData As Variant
Dim lngIndex As Long
Dim strDetails As String

'Open a database
fcnFillFromAccessTable arrData, "D:\RibbonX Project\RibbonData.accdb", "False", "Template_Logos", "Logo_Name", "False"
'Go to the first record
With m_oRecordSet
.MoveFirst
End With

For lngIndex = 0 To UBound(arrData, 2)
'label names.
strDetails = arrData(1, lngIndex)
If strDetails = "Acme Large" Then
Selection.InsertAfter m_oRecordSet.Fields("Logo_Graphic")
GoTo lbl_Cleanup
End If
Next lngIndex

'Then cleanup and close the database
lbl_Cleanup:
If m_oRecordSet.State = 1 Then m_oRecordSet.Close
Set m_oRecordSet = Nothing
If m_oConn.State = 1 Then m_oConn.Close
Set m_oConn = Nothing
End Sub
Everything works fine (btw I haven't included the Function which works OK with this procedure.)

Thanks to Greg Maxey and Bill Coan I've pieced together the above code which all works as it should until it gets to the bold red text.

My intention was that it should look at the second field and extract the attachment into the selection point of the Word document.

Research now tells me that it will only work for text strings. But how can I get the code to extract the graphic attachment located within the field, please?

Thanks for any help.

Doug Robbins
05-31-2013, 10:42 PM
Based on the information in

http://msdn.microsoft.com/en-us/library/bb258184.aspx

you would need to copy the attachment (the logo) to a file. You could then use code to insert the image from that file into the document, or you could have an IncludePicture field in the document that had the path and file name that you use for the file to which you copy the attachment and then have the code update that field to show the attachment.