#1
|
|||
|
|||
Displaying pictures from a field with a file path
This is long but I am working with 3 different programs and I will be adding Excel to the mix soon, so here it goes. I have created a frontend Access 2010 database with an SQL backend (MySQL 2005) in an office environment stored on a server (Server 2003). My knowledge level isn't high on visual basic but I can do a lot of basic stuff and calculations. I have a hyperlink form field in Access where someone can go get a picture and it will display a thumbnail and in VB it renames the picture and saves it in a directory folder I created, then it stores the new file path in a text field (Photo1). All of the form data goes into a table that I use as my data source in Word. I need to have this picture go into a Word doc, all the merge fields which work great, except for this picture. It comes in and states "The linked image cannot be displayed. The file may have been moved, renamed, or deleted. Verify that the link points to the correct file and location." I have created various VB for this field and tweaked the field itself and nothing seems to work. Right now I have the field in a table and structured like this:
{INCLUDEPICTURE "{MERGEFIELD\"Photo1\"}.jpg" \d \*MERGEFORMAT } I also tried adding just {MERGEFIELD Photo1 } and then putting VB in to run when the doc opened but it didn't work (See below). Code:
Private Sub AssemblyMeeting_Open() Dim Table As Table Dim Cell As Cell FileName = Cell.Range.Text Dim FileName As String Dim strCell As String Set Table = ActiveDocument.Tables(1) Set PicCell = ActiveDocument.Range(Start:=Table.Cell(1, 1).Range _ .Start, End:=Table.Cell(1, 2).Range.End) For Each Cell In Table.Range.Cells strCell = FileName If FileName = "" Then Cell.Value = "" Else With Cell.Range .Delete .InlineShapes.AddPicture FileName:=FileName, LinkToFile:=False, SaveWithDocument:=True .InlineShapes.AddPicture FileName:=PicCell, LinkToFile:=False, SaveWithDocument:=True End With End If Next End Sub Last edited by macropod; 07-28-2012 at 01:52 AM. Reason: Added Code Tags & Formatting |
#2
|
|||
|
|||
OMG! Yea, I figured out how to get the picture in, the only problem now is to get the picture to insert in the location I want. Here is how I fixed the issue:
I recorded a macro of me inserting the picture and then I took the recorded macro and copied and pasted it into the event behind the command button in Access that performs the mail merge where it would run right after it opens the Word document. Worked great! Now I just need to figure out how to make the picture come in sized 3.5 x 5.7 and on the 3rd line with a border. Does anyone know how I can modify the below VB to do this type of formatting? Here is my modified code that brings the picture in: Code:
Private Sub Command127_Click() 'generates the individual comp template for the current record LOCAL_TEMPLATE = Me.txtCompTemplate 'SEE IF THE TEMPLATE IS OPEN DoCmd.SetWarnings False 'THIS QUERY CREATES A TEMP TABLE FOR THE MERGE DoCmd.OpenQuery "qryCompData", acViewNormal Call OPEN_WORD_MERGE_DOC(LOCAL_TEMPLATE) 'This is a copy of the Word macro to bring in the picture 'Will need to change Filename to a field from Access, this was to see if it worked Selection.InlineShapes.AddPicture Filename:="K:\AC_CompPics\210019544.jpg" _ , LinkToFile:=False, SaveWithDocument:=True End Sub Last edited by macropod; 07-28-2012 at 01:32 AM. Reason: Added Code Tags |
#3
|
|||
|
|||
I finally found what works. I inserted the following into Access 2010 that will run when a command button is clicked in an Access form. The code will open the word document that I already have set up and insert the picture, resize it, and add a border around the picture.
Code:
Private Sub Command127_Click() 'generates the individual comp template for the current record LOCAL_TEMPLATE = Me.txtCompTemplate 'SEE IF THE TEMPLATE IS OPEN DoCmd.SetWarnings False 'THIS QUERY CREATES A TEMP TABLE FOR THE MERGE DoCmd.OpenQuery "qryCompData", acViewNormal Call OPEN_WORD_MERGE_DOC(LOCAL_TEMPLATE) 'This is a copy of the Word macro to bring in the picture Dim newPicture As InlineShape Selection.GoTo what:=wdGoToBookmark, Name:="Picture" Set newPicture = Selection.InlineShapes.AddPicture(FileName:=Me.Photo1, _ LinkToFile:=False, SaveWithDocument:=True) With newPicture .LockAspectRatio = msoTrue .Height = 252 .Width = 410.25 With .Borders(wdBorderBottom) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth100pt .Color = wdColorBlack End With With .Borders(wdBorderTop) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth100pt .Color = wdColorBlack End With With .Borders(wdBorderLeft) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth100pt .Color = wdColorBlack End With With .Borders(wdBorderRight) .LineStyle = wdLineStyleSingle .LineWidth = wdLineWidth100pt .Color = wdColorBlack End With End With End Sub Last edited by macropod; 07-28-2012 at 01:47 AM. Reason: Added Code Tags & Formatting |
#4
|
|||
|
|||
Oh yea, in the above code "Picture" is a bookmark I inserted in the Word document where I wanted the picture to go in. I hope this helps someone!
|
#5
|
||||
|
||||
Hi okiegirl,
Some comments: 1. Mergefields are really only for use in a mailmerge, where they get data directly from a mailmerge datasource. 2. The syntax for a mergefield embedded in an INCLUDEPICTURE field would be either: {INCLUDEPICTURE "{MERGEFIELD Photo1}.jpg" \d} if the mergefield includes the path (which needs to includes double backslashes as the separators, or: {INCLUDEPICTURE "C:\\Users\\FilePath\\{MERGEFIELD\"Photo1\"}.j pg" \d} if the mergefield does not include the path. 3. If you're trying to insert the picture with a link via vba, you would need to have 'LinkToFile:=True'. Fields aren't required for this. 4. I note you're now using a Selection as the destination for the inserted picture. If you use a range object (eg pointing to a bookmark or a cell in a specified table), you don't need to select it beforehand. Furthermore, if the table cell has fixed dimensions, the inserted picture will automatically be constrained to fit the cell's size, maintaining the correct aspect ratio. In that case, you wouldn't need to code for sizing the shape. 5. The way you're using the 'Picture' bookmark doesn't allow for the picture to be changed. Any attempt to update it will result in multiple pictures. PS: When posting code, please use code tags (on the 'Go Advanced' tab).
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
Tags |
mail merge problem, pictures |
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
File Path for Outlook 2007 pst | eliz.bell | Outlook | 2 | 10-29-2011 07:04 PM |
Displaying contact pictures in e-mail header | christer | Office | 0 | 10-03-2011 09:40 AM |
Modify default template to include file path | jacl | Excel | 1 | 03-26-2011 10:24 AM |
Show Full File Path in Title Bar | paulj | Excel | 3 | 02-10-2010 07:18 AM |
get file name without Path | Ziggy1 | Word VBA | 1 | 09-29-2006 07:55 PM |