PDA

View Full Version : Insert image of signature in to bookmark using word userform



nigel9097
04-15-2017, 08:11 AM
Hi,

I have a user form created in word. Amongst other options there is one important option offering Option A or option B. What I want to do is if option A is selected using radio buttons on the userform , then display Image 1 of a signature when the command click button is pressed and the information inserted in to the bookmark in the word document. thus dsisplaying the signature. If option B is selected then display image 2 as the signature.

Any ideas? Ideally I could do with the signature held somewhere within the userform, or word document, which becomes visible or not visible. I don't really want to load it from a file location, as I have to send this form out when completed and I don't want to rely on the user putting the file in the correct location.

Kind Regards,

Nigel

gmayor
04-15-2017, 09:37 PM
As this is presumably a macro enabled document and not a template, then the obvious path of using building blocks to hold the signatures doesn't apply. However, if you put the images on the userform itself (lets call them with their default names of Image1 and Image2), and associate each with an option button (though you could use the images themselves as buttons) then the following userform code will insert the image associated with the selected option at the named bookmark - here "bkSig" when CommandButton1 is clicked. Note that the image will be inserted at its original size and not at the size you may have shrunk it to on the userform. The image is stored as a temporary file which is removed after use and the image is written to the bookmark so repeated use will replace the image. If userform space is at a premium, the images there do not have to be visible if selected by option buttons.


Option Explicit


Private Sub CommandButton1_Click()
Select Case True
Case OptionButton1.Value
UserFormImageToBM "bkSig", Image1.Picture
Case OptionButton2.Value
UserFormImageToBM "bkSig", Image2.Picture
End Select
Unload Me
lbl_Exit:
Exit Sub
End Sub




Private Sub UserFormImageToBM(strBMName As String, oImage As Object)
'Graham Mayor - http://www.gmayor.com
Dim oRng As Range
Dim TempFile As String
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")
TempFile = Replace(fso.GetTempName, "tmp", "bmp")
SavePicture oImage, TempFile
With ActiveDocument
On Error GoTo lbl_Exit
Set oRng = .Bookmarks(strBMName).Range
oRng.Text = ""
oRng.InlineShapes.AddPicture _
FileName:=TempFile, LinkToFile:=False, _
SaveWithDocument:=True
oRng.End = oRng.End + 1
oRng.Bookmarks.Add strBMName
End With
Kill TempFile
lbl_Exit:
Set fso = Nothing
Set oRng = Nothing
Exit Sub
End Sub