+ Reply to Thread
Results 1 to 9 of 9

VBA for Image Insert with Command Button

  1. #1
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    VBA for Image Insert with Command Button

    Hello

    I am trying to create a Command Button with a sheet that once it is pressed

    Will take me to my Documents folder

    Where I can chose a Passport size picture

    and it will place it into the Sheet in a specific merged cell

    Any assistance would be grateful

    i.e. I did find a youtube video of something similar , but it was in a foreign language to me

    So could understand it all

    But showed it was possible

    Any assistance would be Grateful

    Bod213

  2. #2
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    Capture.PNG

    Hopefully here is a picture of my spread sheet with the Command Button

    And the Merged Cell where I want the Picture to be placed

  3. #3
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    Pic.PNG

    A pic of a similar VBA for Image Insert with Command Button

  4. #4
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    Sub InsertPictureInActiveCell()
    Dim strFile As String
    Dim rng As Range
    Dim sh As Range
    Const cFile As String = "Image Files(*.bmp; *.jpg; *.jepg; *.png),"
    strFile = Application.GetOpenFilename(filefilter:=cFile, Title:=ES)
    If strFile = "False" Then
    Else
    Set rng = ActiveCell
    Set rng = mg.MergeArea
    With rng
    Set sh = ActiveSheet.Shapes.AddPicture(Filename:=strfFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
    sh.LockAspectRation = msoFalse
    End With
    Set sh = Nothing
    Set rng = Nothing
    End If
    End Sub


    I tried this code , but it didn't quite work

  5. #5
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    Sub SheetA.1_Button1_Click()

    Dim sPicture As String, pic As Picture
    sPicture = Application.GetOpenFilename
    ("Pictures (*.gif; *.jpg; *.bmp; *.tif), *.gif; *.jpg; *.bmp; *.tif",_
    "Select Picture to Import")

    If sPicture = "False" Then Exit Sub
    'Innsert
    Set pic = ActiveSheet.Pictures.Insert(sPicture)
    With pic
    .ShapeRange.LockAspectRatio = msoFalse
    .Height = ActiveCell.Height
    .Width = ActiveCell.Width
    .Top = ActiveCell.Width
    .Left = ActiveCell.Left
    .Placement = x1MoveAndSize
    End With

    Set pic = Nothing

    and I also tried this code , which didn't work either

  6. #6
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    Sub InsertPictureInActiveCell()

    Dim strFile As String
    Dim rng As Range
    Dim sh As Range

    Const cFile As String = "Image Files(*.bmp; *.jpg; *.jepg; *.png),"
    strFile = Application.GetOpenFilename(filefilter:=cFile)


    End Sub

    this part of the Code works - In that it opens up the document file

    its the other part of the code that does place the pic in the active cell

  7. #7
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    Sub InsertPictureInActiveCell()
    Dim strFile As String
    Dim rng As Range
    Dim sh As Range
    Const cFile As String = "Image Files(*.BMP; *.JPG; *.JEPG; *.PNG),"
    strFile = Application.GetOpenFilename(filefilter:=cFile, Title:=Ts)
    If strFile = "False" Then
    Else
    Set rng = ActiveCell
    Set rng = rng.MergeArea
    With rng
    Set sh = ActiveSheet.Shapes.AddPicture(Filename:=strFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
    sh.LockAspectRation = msoFalse
    End With
    Set sh = Nothing
    Set rng = Nothing
    End If
    End Sub

    I have played about with the code and managed to get it to work slightly

    It will run with the command button
    It will take me to document file
    It will allow a pic to be chosen

    But I get this error message

    pIC 3.PNG

    On pressing OK

    I does insert the pic

    But cant work out why I am getting the error message first

  8. #8
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    I also want to use this on everyone of the 26 sheets in the work book

  9. #9
    Registered User
    Join Date
    11-24-2016
    Location
    London
    MS-Off Ver
    Excel 2016 Windows
    Posts
    22

    Re: VBA for Image Insert with Command Button

    Public Sub InsertPictureInActiveCell()
    Dim strFile As String
    Dim rng As Range
    Dim sh
    Const cFile As String = "Image Files(*.bmp; *.jpg; *.jepg; *.png; *.tif),"

    strFile = Application.GetOpenFilename(filefilter:=cFile, Title:=Ts)

    If strFile = "False" Then
    Else
    Set rng = ActiveSheet.Range("B2").MergeArea
    With rng
    Set sh = ActiveSheet.Shapes.AddPicture(Filename:=strFile, LinkToFile:=msoFalse, SaveWithDocument:=msoTrue, Left:=.Left, Top:=.Top, Width:=.Width, Height:=.Height)
    sh.LockAspectRatio = msoFalse
    End With
    Set sh = Nothing
    Set rng = Nothing
    End If
    End Sub

    This is the Solved Solution for me anyway

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 2
    Last Post: 02-02-2016, 10:27 PM
  2. Insert Command Button that will Activate Command Button on a Different Sheet
    By realvirtuality1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-06-2016, 02:11 PM
  3. [SOLVED] Insert image by clicking button
    By brainzlp in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-03-2015, 05:19 AM
  4. capture the name of an image in a command button
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-17-2013, 07:32 AM
  5. Insert an image button
    By marreco in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-25-2012, 07:22 AM
  6. insert a command button
    By lee_har in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 10-31-2009, 04:25 AM
  7. Command Button - Insert Lines
    By TjBoiler in forum Excel General
    Replies: 6
    Last Post: 01-29-2007, 10:11 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1