![]() |
#1
|
|||
|
|||
![]()
I am a novice at vba but have created a userform to be used to insert customer data into bookmarks in a word template. All of the code inserting text from text boxes on the userform works fine but it doesn't work for the image. Instead of the image it uploads a string of numbers. The relevant part of the code is below:
Dim Proposal As Range Set Proposal = ActiveDocument.Bookmarks("Proposal").Range Proposal = Me.Image1.Picture The bookmark name is Proposal which is where I want a JPG image (Image1) to be placed. The image has been uploaded to the userform via a command button and is visible on the userform. Any help to correct the code would be much appreciated. |
#2
|
||||
|
||||
![]()
You need to save the image from the userform as a temporary file then insert that temporary file at the bookmark.
Call the image using the following sub as follows: Code:
UserformImageToBM "Proposal", Image1.Picture Code:
Private Sub UserFormImageToBM(strbmName As String, oImage As Object) 'Graham Mayor - http://www.gmayor.com 'UserformImageToBM "bookmarkName", Image1.Picture 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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#3
|
|||
|
|||
![]()
That worked brilliantly Graham, thanks so much.
|
#4
|
|||
|
|||
![]()
Hi Graham
Is it possible specify the height of the image and also lock the aspect ratio within that code? |
#5
|
||||
|
||||
![]()
You can apply any of the available formatting parameters to the image - here the height is set to 0.5":
Code:
Private Sub UserFormImageToBM(strbmName As String, oImage As Object) 'Graham Mayor - https://www.gmayor.com 'UserformImageToBM "bookmarkName", Image1.Picture Dim oRng As Range Dim TempFile As String Dim oShape As InlineShape 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 = "" Set oShape = oRng.InlineShapes.AddPicture _ (Filename:=TempFile, LinkToFile:=False, _ SaveWithDocument:=True) With oShape .LockAspectRatio = msoTrue .Height = InchesToPoints(0.5) End With 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
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#6
|
|||
|
|||
![]()
Thanks Graham, that worked great.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
how to select a default value for a combo in a userform from value of bookmark in the word document | caracoder | Word VBA | 1 | 08-29-2021 04:19 AM |
![]() |
BayhDole | Word | 2 | 07-21-2015 08:43 PM |
![]() |
marksm33 | Word VBA | 3 | 01-15-2015 07:55 PM |
Insert an image to a document created by a userform? | jgoodrich | Word VBA | 0 | 10-22-2014 11:03 AM |
How Can you insert a live hosted picture- in a document? -How can it be done?-Help!! | nationsheet | Word | 0 | 05-15-2009 06:53 PM |