473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to export attached jpeg files from Access to Word

I have working VBA that creates Word documents from Access data and can even insert jpeg's into the headers, but the jpegs are in a folder on the C: drive.

What is the VBA to insert the same jpegs that are now attachments in the Access database? The customer wants the jpegs in the database so it can move from machine to machine without reassigned the C: folder path for the jpegs.

Thanks,
May 30 '17 #1

✓ answered by ADezii

  1. I am not sure how you could 'directly' insert Attachments consisting of *.jpgs into a Word Document. As the others have suggested, it would more than likely involve a Binary Transfer of the Images using some form of BLOB approach.
  2. I do, however, have an indirect approach using the SaveToFile Method which will transfer the Attached *.jpgs as Binary Streams to a Folder of your choosing. Once they have been saved to Disk it should be a simple matter to Insert them into a Word Document then subsequently Delete them.
  3. First, a couple of assumptions:
    1. I created a Table named tblTest that consists of only two Fields, namely: [PK] - {AutoNumber/Primary Key}, and [MyJPGs] - {Attachment/holds *.jpgs}.
    2. There are only two Records in this Table: Record #1 has a Primary Key of 1 with four *.jpgs Attached to the [MyJPGs] Field and Record #2 has a Primary Key of 2 with three *.jpgs Attached to the [MyJPGs] Field.
  4. The following Code will extract the four *.jpgs in Record #1 to the CurrentProject.Path Folder. From this point, they can easily be inserted into a Word Document, either manually or programmatically.
  5. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim dbs As DAO.Database
    2. Dim rst As DAO.Recordset2
    3. Dim rsA As DAO.Recordset2
    4. Dim fld As DAO.Field2
    5. Dim strSQL As String
    6.  
    7. strSQL = "SELECT * FROM tblTest WHERE [PK]=1"
    8.  
    9. 'Get the Database, Recordset, and Attachment Field
    10. Set dbs = CurrentDb
    11. Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
    12. Set fld = rst("MyJPGs")
    13.  
    14. 'Navigate through the Table
    15. Do While Not rst.EOF
    16.   'Get the Recordset for the Attachments field
    17.   Set rsA = fld.Value
    18.     With rsA
    19.       'Save all Attachments in the Field to Disk
    20.       Do While Not rsA.EOF
    21.         ![FileData].SaveToFile CurrentProject.Path & "\" & ![Filename]
    22.           .MoveNext
    23.       Loop
    24.  
    25.       .Close
    26.      End With
    27.  
    28.     rst.MoveNext    'Next Record
    29. Loop
    30.  
    31. rst.Close
    32. dbs.Close
    33.  
    34. Set fld = Nothing
    35. Set rsA = Nothing
    36. Set rst = Nothing
    37. Set dbs = Nothing
    38.  
  6. Any questions, feel free to ask.

10 1111
NeoPa
32,556 Expert Mod 16PB
I've heard of BLOBs (Binary Large OBjects) that can be handled in Access, but don't know much about them. It may be the direction to start looking in though.
May 30 '17 #2
PhilOfWalton
1,430 Expert 1GB
I Know Stephen Lebans certainly http://www.lebans.com/
has done works on Blobs, I can't remember which was the relevant title, but I have and old Mdb version of one of his programs.

Stephen sadly retired about 8 years ago, so I think most of his work was in Mdb format.

Phil
May 30 '17 #3
I'll check it out. Thanks for the replies NeoPa and PhilOfWalton.
May 30 '17 #4
ADezii
8,834 Expert 8TB
  1. I am not sure how you could 'directly' insert Attachments consisting of *.jpgs into a Word Document. As the others have suggested, it would more than likely involve a Binary Transfer of the Images using some form of BLOB approach.
  2. I do, however, have an indirect approach using the SaveToFile Method which will transfer the Attached *.jpgs as Binary Streams to a Folder of your choosing. Once they have been saved to Disk it should be a simple matter to Insert them into a Word Document then subsequently Delete them.
  3. First, a couple of assumptions:
    1. I created a Table named tblTest that consists of only two Fields, namely: [PK] - {AutoNumber/Primary Key}, and [MyJPGs] - {Attachment/holds *.jpgs}.
    2. There are only two Records in this Table: Record #1 has a Primary Key of 1 with four *.jpgs Attached to the [MyJPGs] Field and Record #2 has a Primary Key of 2 with three *.jpgs Attached to the [MyJPGs] Field.
  4. The following Code will extract the four *.jpgs in Record #1 to the CurrentProject.Path Folder. From this point, they can easily be inserted into a Word Document, either manually or programmatically.
  5. Code Definition:
    Expand|Select|Wrap|Line Numbers
    1. Dim dbs As DAO.Database
    2. Dim rst As DAO.Recordset2
    3. Dim rsA As DAO.Recordset2
    4. Dim fld As DAO.Field2
    5. Dim strSQL As String
    6.  
    7. strSQL = "SELECT * FROM tblTest WHERE [PK]=1"
    8.  
    9. 'Get the Database, Recordset, and Attachment Field
    10. Set dbs = CurrentDb
    11. Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
    12. Set fld = rst("MyJPGs")
    13.  
    14. 'Navigate through the Table
    15. Do While Not rst.EOF
    16.   'Get the Recordset for the Attachments field
    17.   Set rsA = fld.Value
    18.     With rsA
    19.       'Save all Attachments in the Field to Disk
    20.       Do While Not rsA.EOF
    21.         ![FileData].SaveToFile CurrentProject.Path & "\" & ![Filename]
    22.           .MoveNext
    23.       Loop
    24.  
    25.       .Close
    26.      End With
    27.  
    28.     rst.MoveNext    'Next Record
    29. Loop
    30.  
    31. rst.Close
    32. dbs.Close
    33.  
    34. Set fld = Nothing
    35. Set rsA = Nothing
    36. Set rst = Nothing
    37. Set dbs = Nothing
    38.  
  6. Any questions, feel free to ask.
May 30 '17 #5
ADezii, I was considering something like this. Your code is cleaner than what I was working. :)

Thanks!
May 30 '17 #6
ADezii
8,834 Expert 8TB
  1. I had some spare time, so I decided to write some follow-up Code should you decide to use the prior approach. I found the easiest method to Insert Graphics (*.jpg) once the *.jpgs are written to Disk is to have a Word Template with pre-defined Bookmarks. In my simple Demo:
    1. A Word Template was created as such:
      Expand|Select|Wrap|Line Numbers
      1. C:\Test\Test.docx
    2. Two *.jpgs exist in the C:\Test Folder, namely:
      Expand|Select|Wrap|Line Numbers
      1. JPG1.jpg and JPG2.jpg
    3. Two Bookmarks were also created and strategically placed - they are named Bookmark1 and Bookmark2.
  2. The following Code will insert JPG1.jpg @ Bookmark1 and JPG2.jpg @ Bookmark2. Both Images were scaled to 25% both Horizontally and Vertically to ensure they fit on the Page.
    Expand|Select|Wrap|Line Numbers
    1. Dim wrdApp As New Word.Application
    2. Dim wrdDoc As New Word.Document
    3. Dim objWdRange As Word.Range
    4. Dim strJPG As String
    5. Dim strJPG2 As String
    6. Dim wrdPic As Word.InlineShape
    7.  
    8. strJPG = "C:\Test\JPG1.jpg"
    9. strJPG2 = "C:\Test\JPG2.jpg"
    10.  
    11. wrdApp.Visible = True
    12.  
    13. Set wrdDoc = wrdApp.Documents.Open("C:\Test\Test.docx")
    14.  
    15. With wrdDoc
    16.   Set wrdPic = .Bookmarks("Bookmark1").Range.InlineShapes.AddPicture(Filename:=strJPG, _
    17.                 LinkToFile:=False, SaveWithDocument:=True)
    18.       wrdPic.ScaleHeight = 25
    19.       wrdPic.ScaleWidth = 25
    20.   Set wrdPic = .Bookmarks("Bookmark2").Range.InlineShapes.AddPicture(Filename:=strJPG2, _
    21.                 LinkToFile:=False, SaveWithDocument:=True)
    22.       wrdPic.ScaleHeight = 25
    23.       wrdPic.ScaleWidth = 25
    24.        .SaveAs "C:\Test\Test.docx"
    25. End With
    26.  
    27. wrdDoc.Close
    28. Set wrdDoc = Nothing
    29.  
    30. wrdApp.Quit
    31. Set wrdApp = Nothing
  3. The Code is only meant to be a guide and easily can be improved upon and/or expanded.
  4. It was meant to be an extension of saving the *.jpgs to Disk Code using SaveToFile. It has been tested and is fully operational.
  5. Good Luck with your Project.
May 30 '17 #7
ADezii, thanks for taking the time to post this.
May 30 '17 #8
ADezii
8,834 Expert 8TB
You are quite welcome!
May 30 '17 #9
NeoPa
32,556 Expert Mod 16PB
Nice one again Armund.

@CapsFan.
You won't go too far wrong once Armund has got involved. An expert of long standing.
May 30 '17 #10
ADezii
8,834 Expert 8TB
@NeoPa:
Thank you for your kind words. Actually, I learned a lot from you and you are still my Mentor.
May 31 '17 #11

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Markus Bulmer | last post by:
Hi, I'm trying to do a photo album with php. The admins should be able to write comments, descriptions etc. into the jpeg files via a web form. I tried to implement this with the (still...
3
by: Sonu | last post by:
Greetings - I am trying to export a tbl from Access 2000 that has 26,544 records. When I export the tbl to Excel, it only exports out 16,384 records. It also creates a tbl that shows the data...
3
by: marcw43 | last post by:
Hello, I have downloaded the lebans OLEtoDisk, which is 99% exactly what I needed for a database with over 800 records/Pictures. It is a great program ;-) What I need is say for example: I...
1
by: new | last post by:
I have data for each week in a single table. I need to export this data to a separate flat file for each week. Any ideas? DB2 SQL Query export to flat files as a function of data on each record
2
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
Hi, How do I export data from an Access query or table to a csv file? Thanks in advance
1
by: panku007 | last post by:
hi All, I wanted to export excel data into ms word document through java coding.So please send me reply,according to my question. thanks
0
by: Steve C. Orr [MCSD, MVP, CSM, ASP Insider] | last post by:
I've already assembled details about generating Word (and Excel) documents: http://SteveOrr.net/articles/ExcelExport.aspx http://SteveOrr.net/articles/ExportPanel.aspx Here are some decent 3rd...
0
by: prashantdixit | last post by:
Hi, I have beent trying importing Excel data with one column containing PDF/JPEG file name to access 2007. I have a Excel file with few columns One of the columns named as "Reference". The...
35
by: TravelingCat | last post by:
Hello, Say there is a word document saved on my computer. I need to display text from this document in a report, without opening it (not that i know how to do it while it's opened, but anyway it...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.