PDA

View Full Version : Word 2010 - Inserting Images based on certain criteria



Superdonk
04-19-2016, 04:09 AM
Hi guys

I've been racking my brain trying to do this and I cannot think of a way - can you help?

I have a Word template which needs to have a signature inserted once its been authorised (these are scanned copies of the signature saved as an image file)

There are five different signatures that could be used based on who has signed it off - I want a macro that will insert the image of the signature into the correct place based on the result, maybe from a drop down box in a userform.

Has anyone got any ideas of how I can do this?!??

Thanks in advance

SD

gmayor
04-19-2016, 04:26 AM
There are several ways you could do this. Much depends on who is completing the document created from the template. You could, for example, compare the user name with the graphics names and insert the matching graphic. Or, you could do as you suggested and list the names in a userform list box or combo box. If you make the box two columns and display only the first column (with the names) you can put the full paths of the matching graphics in the hidden second column and then insert the image from column 2. In that case the images would have to be in a location accessible by all users.

Add a simple userform with a combo box and two command buttons beneath. Insert a bookmark 'bmSign' where you want the signature.

For the userform code

Option Explicit

Private Sub CommandButton1_Click()
Me.Tag = 1
Me.Hide
lbl_Exit:
Exit Sub
End Sub

Private Sub CommandButton2_Click()
Me.Tag = 0
Me.Hide
lbl_Exit:
Exit Sub
End Sub



For the calling macro

Option Explicit

Sub InsertSigGraphic()
Dim oFrm As New UserForm1
Dim oRng As Range
Dim oBM As Bookmark
With oFrm
.Caption = "Insert signature"
With .ComboBox1
.ColumnCount = 2
.ColumnWidths = .Width - 4 & ", 0"
.AddItem "[Select Signatory]"
.AddItem
.List(.ListCount - 1, 0) = "Name 1"
.List(.ListCount - 1, 1) = "C:\Path\Image for Name1.png"
.AddItem
.List(.ListCount - 1, 0) = "Name 2"
.List(.ListCount - 1, 1) = "C:\Path\Image for Name2.png"
.AddItem
.List(.ListCount - 1, 0) = "Name 3"
.List(.ListCount - 1, 1) = "C:\Path\Image for Name3.png"
.AddItem
.List(.ListCount - 1, 0) = "Name 4"
.List(.ListCount - 1, 1) = "C:\Path\Image for Name4.png"
.AddItem
.List(.ListCount - 1, 0) = "Name 5"
.List(.ListCount - 1, 1) = "C:\Path\Image for Name5.png"
.ListIndex = 0
End With
.CommandButton1.Caption = "Continue"
.CommandButton2.Caption = "Cancel"
.Show
If .Tag = 0 Then GoTo lbl_Exit
Set oRng = ActiveDocument.Bookmarks("bmSign").Range
ActiveDocument.Shapes.AddPicture(Anchor:=oRng, Filename:= _
.ComboBox1.Column(1), LinkToFile:=False, _
SaveWithDocument:=True).WrapFormat.Type = wdWrapSquare
End With
lbl_Exit:
Unload oFrm
Set oFrm = Nothing
Exit Sub
End Sub

Superdonk
04-19-2016, 04:45 AM
Thanks Gmayor

Do you have any example code as its this that I am struggling with - I'm good with Excel VBA but Word is new to me!!

Thanks

gmayor
04-19-2016, 04:48 AM
I have added it to my earlier reply.

Superdonk
04-19-2016, 06:30 AM
Thanks Gmayor - thats working brilliantly, just one thing.

When it inserts the image into the document, it isn't inserting it right next the to Bookmark.

The bookmark is not on the far left of the document, its about halfway across the page, is there anyway of making it in line with the bookmark?

other than that, its brilliant, thank you.

SD

gmaxey
04-19-2016, 01:14 PM
Change Dim oShp as Shape to Dim oILS as InlineShape:

Then replace:
If .Tag = 0 Then GoTo lbl_Exit
Set oRng = ActiveDocument.Bookmarks("bmSign").Range
ActiveDocument.Shapes.AddPicture(Anchor:=oRng, Filename:= _
.ComboBox1.Column(1), LinkToFile:=False, _
SaveWithDocument:=True).WrapFormat.Type = wdWrapSquare

with:

If .Tag = 0 Then GoTo lbl_Exit
Set oRng = ActiveDocument.Bookmarks("bmSign").Range
Set oILS = ActiveDocument.InlineShapes.AddPicture(.ListBox1.Column(1), False, True, oRng)
Set oRng = oILS.Range
ActiveDocument.Bookmarks.Add "bmSign", oRng