Consulting

Results 1 to 4 of 4

Thread: Inserting image to table from userform

  1. #1

    Inserting image to table from userform

    Hey team,

    So I'm working on a document that is created from a userform.

    In creating the letter head portion, I currently do this...

    GRAPHIMAGE_LOGO = "C:\Users\OEM\Documents\Form_Set\letterhead.png"
    .Cell(1, 1).Range.InlineShapes.AddPicture FileName:=GRAPHIMAGE_LOGO, LinkToFile:=False, SaveWithDocument:=True

    What I would really like is to have a hidden userform with picture controls in it that hold my images, so I can then grab said image from the userform and fling it into the cell in the document table.

    GRAPHIMAGE_LOGO = UserForm999.letterhead.Picture
    .Cell(1, 1). [insert amazing code here please]

    Thanks in advance.


    Regards
    Kes

  2. #2
    Let us assume that you have a userform with four picture controls displaying your images (you can have more or fewer as required) named Image1 to Image4 then the code for the userform would be

    Option Explicit
    
    
    Private Sub Image1_Click()
        CopyToCell Image1
        Hide
    End Sub
    
    
    Private Sub Image2_Click()
        CopyToCell Image2
        Hide
    End Sub
    
    
    Private Sub Image3_Click()
        CopyToCell Image3
        Hide
    End Sub
    
    
    Private Sub Image4_Click()
        CopyToCell Image4
        Hide
    End Sub
    
    
    Sub CopyToCell(oCtrl As Control)
    Const TemporaryFolder = 2
    Dim FSO As Object
    Dim strImage As String
    Dim oRng As Range
        Set oRng = ActiveDocument.Tables(1).Cell(1, 1).Range
        oRng.End = oRng.End - 1
        oRng.Text = ""
        Set FSO = CreateObject("scripting.filesystemobject")
        strImage = FSO.GetSpecialFolder(TemporaryFolder).Path & "\" & FSO.gettempname
        SavePicture oCtrl.Picture, strImage
        oRng.InlineShapes.AddPicture strImage
        FSO.deletefile strImage
    lbl_Exit:
        Set oRng = Nothing
        Set FSO = Nothing
        Exit Sub
    End Sub
    You can call the userform with a simple macro e.g.
    Option Explicit
    
    
    Sub AutoOpen()
        ShowForm
    End Sub
    
    
    Sub ShowForm()
        UserForm1.Show
        Unload UserForm1
    End Sub
    Click one of the images to insert it in cell 1 - see attached.
    Attached Files Attached Files
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Thank you for the quick reply, I'll get to work on integrating it.

    Thanks
    Kes

  4. #4
    Worked perfectly, shame I can't upvote you!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •