Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-15-2022, 07:35 AM
dyb dyb is offline Insert a picture from userform into bookmark in word document Windows 11 Insert a picture from userform into bookmark in word document Office 2019
Novice
Insert a picture from userform into bookmark in word document
 
Join Date: Feb 2022
Posts: 7
dyb is on a distinguished road
Question Insert a picture from userform into bookmark in word document

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.
Reply With Quote
  #2  
Old 02-15-2022, 10:14 PM
gmayor's Avatar
gmayor gmayor is offline Insert a picture from userform into bookmark in word document Windows 10 Insert a picture from userform into bookmark in word document Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 02-16-2022, 02:18 AM
dyb dyb is offline Insert a picture from userform into bookmark in word document Windows 11 Insert a picture from userform into bookmark in word document Office 2019
Novice
Insert a picture from userform into bookmark in word document
 
Join Date: Feb 2022
Posts: 7
dyb is on a distinguished road
Default

That worked brilliantly Graham, thanks so much.
Reply With Quote
  #4  
Old 02-16-2022, 09:18 AM
dyb dyb is offline Insert a picture from userform into bookmark in word document Windows 11 Insert a picture from userform into bookmark in word document Office 2019
Novice
Insert a picture from userform into bookmark in word document
 
Join Date: Feb 2022
Posts: 7
dyb is on a distinguished road
Default

Hi Graham

Is it possible specify the height of the image and also lock the aspect ratio within that code?
Reply With Quote
  #5  
Old 02-17-2022, 01:41 AM
gmayor's Avatar
gmayor gmayor is offline Insert a picture from userform into bookmark in word document Windows 10 Insert a picture from userform into bookmark in word document Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,137
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

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
Reply With Quote
  #6  
Old 02-18-2022, 07:22 AM
dyb dyb is offline Insert a picture from userform into bookmark in word document Windows 11 Insert a picture from userform into bookmark in word document Office 2019
Novice
Insert a picture from userform into bookmark in word document
 
Join Date: Feb 2022
Posts: 7
dyb is on a distinguished road
Default

Thanks Graham, that worked great.
Reply With Quote
Reply



Similar Threads
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
Insert a picture from userform into bookmark in word document Is there anyway I can insert an easy bookmark in word? BayhDole Word 2 07-21-2015 08:43 PM
Insert a picture from userform into bookmark in word document Moving Selected Items from a Multiselect Listbox on a userform to a bookmark in Word 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

Other Forums: Access Forums

All times are GMT -7. The time now is 08:57 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft