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,
- 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.
- 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.
- First, a couple of assumptions:
- I created a Table named tblTest that consists of only two Fields, namely: [PK] - {AutoNumber/Primary Key}, and [MyJPGs] - {Attachment/holds *.jpgs}.
- 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.
- 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.
- Code Definition:
-
Dim dbs As DAO.Database
-
Dim rst As DAO.Recordset2
-
Dim rsA As DAO.Recordset2
-
Dim fld As DAO.Field2
-
Dim strSQL As String
-
-
strSQL = "SELECT * FROM tblTest WHERE [PK]=1"
-
-
'Get the Database, Recordset, and Attachment Field
-
Set dbs = CurrentDb
-
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
-
Set fld = rst("MyJPGs")
-
-
'Navigate through the Table
-
Do While Not rst.EOF
-
'Get the Recordset for the Attachments field
-
Set rsA = fld.Value
-
With rsA
-
'Save all Attachments in the Field to Disk
-
Do While Not rsA.EOF
-
![FileData].SaveToFile CurrentProject.Path & "\" & ![Filename]
-
.MoveNext
-
Loop
-
-
.Close
-
End With
-
-
rst.MoveNext 'Next Record
-
Loop
-
-
rst.Close
-
dbs.Close
-
-
Set fld = Nothing
-
Set rsA = Nothing
-
Set rst = Nothing
-
Set dbs = Nothing
-
- 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.
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
I'll check it out. Thanks for the replies NeoPa and PhilOfWalton.
- 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.
- 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.
- First, a couple of assumptions:
- I created a Table named tblTest that consists of only two Fields, namely: [PK] - {AutoNumber/Primary Key}, and [MyJPGs] - {Attachment/holds *.jpgs}.
- 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.
- 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.
- Code Definition:
-
Dim dbs As DAO.Database
-
Dim rst As DAO.Recordset2
-
Dim rsA As DAO.Recordset2
-
Dim fld As DAO.Field2
-
Dim strSQL As String
-
-
strSQL = "SELECT * FROM tblTest WHERE [PK]=1"
-
-
'Get the Database, Recordset, and Attachment Field
-
Set dbs = CurrentDb
-
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
-
Set fld = rst("MyJPGs")
-
-
'Navigate through the Table
-
Do While Not rst.EOF
-
'Get the Recordset for the Attachments field
-
Set rsA = fld.Value
-
With rsA
-
'Save all Attachments in the Field to Disk
-
Do While Not rsA.EOF
-
![FileData].SaveToFile CurrentProject.Path & "\" & ![Filename]
-
.MoveNext
-
Loop
-
-
.Close
-
End With
-
-
rst.MoveNext 'Next Record
-
Loop
-
-
rst.Close
-
dbs.Close
-
-
Set fld = Nothing
-
Set rsA = Nothing
-
Set rst = Nothing
-
Set dbs = Nothing
-
- Any questions, feel free to ask.
ADezii, I was considering something like this. Your code is cleaner than what I was working. :)
Thanks!
- 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:
- A Word Template was created as such:
- Two *.jpgs exist in the C:\Test Folder, namely:
- Two Bookmarks were also created and strategically placed - they are named Bookmark1 and Bookmark2.
- 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.
- Dim wrdApp As New Word.Application
-
Dim wrdDoc As New Word.Document
-
Dim objWdRange As Word.Range
-
Dim strJPG As String
-
Dim strJPG2 As String
-
Dim wrdPic As Word.InlineShape
-
-
strJPG = "C:\Test\JPG1.jpg"
-
strJPG2 = "C:\Test\JPG2.jpg"
-
-
wrdApp.Visible = True
-
-
Set wrdDoc = wrdApp.Documents.Open("C:\Test\Test.docx")
-
-
With wrdDoc
-
Set wrdPic = .Bookmarks("Bookmark1").Range.InlineShapes.AddPicture(Filename:=strJPG, _
-
LinkToFile:=False, SaveWithDocument:=True)
-
wrdPic.ScaleHeight = 25
-
wrdPic.ScaleWidth = 25
-
Set wrdPic = .Bookmarks("Bookmark2").Range.InlineShapes.AddPicture(Filename:=strJPG2, _
-
LinkToFile:=False, SaveWithDocument:=True)
-
wrdPic.ScaleHeight = 25
-
wrdPic.ScaleWidth = 25
-
.SaveAs "C:\Test\Test.docx"
-
End With
-
-
wrdDoc.Close
-
Set wrdDoc = Nothing
-
-
wrdApp.Quit
-
Set wrdApp = Nothing
- The Code is only meant to be a guide and easily can be improved upon and/or expanded.
- It was meant to be an extension of saving the *.jpgs to Disk Code using SaveToFile. It has been tested and is fully operational.
- Good Luck with your Project.
ADezii, thanks for taking the time to post this.
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.
@NeoPa:
Thank you for your kind words. Actually, I learned a lot from you and you are still my Mentor.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
by: B Deepak |
last post by:
|
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...
|
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
|
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
|
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
|
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...
|
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...
|
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...
|
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,...
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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...
| |