PDA

View Full Version : Merging images and resizing



chardy89
07-19-2010, 03:17 AM
Hi Guys,

I work for a software company and our software is an SQL database based program and a major feature of our program is the ability to mere information from the database into Word. One option we have is the ability to merge images. When creating a template we have an option to insert a merge image which puts in a generic image which is merged into with images from the database. The problem is that no matter what size you make the generic image when the image is merged out of the database word would resize the image to the size of that merged image. This is where is have created a macro to fix this.

**********************************************

Private Sub Document_New()
MsgBox "Press F9 on your keyboard to merge property images"
End Sub

Sub UpdateFields()

Dim height As String
Dim width As String

For Each image In ActiveDocument.InlineShapes
image.Select
With Selection

height = .InlineShapes(1).height
width = .InlineShapes(1).width

Selection.Fields.Update

.InlineShapes(1).height = height
.InlineShapes(1).width = width

End With
Next image
End Sub

***********************************************

Basically what the macro does is with pressing the F9 button on your keyboard it will start from the first inline shape, note the height and width of the generic image. Then update the merge fields with the image which has been exported out of the database and then resizes the merged image to the size that the generic image was previous to updating and loops this command for each image in the document.

This macro has been working fine in Word 2003 and 2007 but does not seem to be working in Word 2010.

I have been trying to invetigate the exact cause of the issue and what I have found is.

If you first create the template and insert the macro and save as a macro enabled template, merge to the template it will work fine without any problems. If you open the template and then save or save as a different file and merge to any of these the macro will not work.

The error message you get when you run the macro is "Requested Member of the Collection Does Not Exisit"

What i have narrowed it down to is after it runs the selection.fields.update it seems that word then loses the selection and cannot run the resize macro below.

It seems strange to me that the macro works fine if you merge to a template which has just been created but as soon as you open and resave the template it will no longer work.

Currently the only way to get it to work is to get rid of the automated selection and have the users click on each image and press F9, but this is on the borderline of the macro being less time consuming which is the purpose of the macros.

Can anybody shed light on what might be happening and any ideas on how this can be fixed.

Thanks