hunter21188
hunter21188

Reputation: 415

VBA Code to Insert Image in Placeholder

I am working with VBA for the first time and have created a User Form that includes two drop down menus along with a Command Button. Once the user selects an item from each drop down menu ("Image1" and "Image2" for example) they will then click the command button, which would insert those selected items (which are images that would be pulled from a file on the local computer) inside specific image placeholders that are on the slide.

Any ideas on how I would make this happen? I currently have the form designed, but it has zero functionality. I hope this is clear. Thanks!

--UPDATE--

Here is the VBA code I have so far. The next question is where do I link the files, such as "Ash Fork", to local images on the computer:

Private Sub Combo1_DropButtonClick()
With Combo1

    .AddItem "Ash Fork"
    .AddItem "Flagstaff"
    .AddItem "Winslow"
    .AddItem "Clints Well"
    .AddItem "Bellemont"

End With
End Sub

Private Sub Combo2_DropButtonClick()
With Combo2

    .AddItem "Ash Fork"
    .AddItem "Flagstaff"
    .AddItem "Winslow"
    .AddItem "Clints Well"
    .AddItem "Bellemont"

End With
End Sub

Private Sub CommandButton1_Click()

Dim image1 As String
Dim image2 As String

image1 = Combo1.Text
image2 = Combo2.Text

ActiveSheet.Shapes.AddPicture FileName:=image1, _
                              linktofile:=msoFalse, _
                              savewithdocument:=msoCTrue, _
                              Left:=0, Top:=0, Width:=100, Height:=100

ActiveSheet.Shapes.AddPicture FileName:=image2, _
                              linktofile:=msoFalse, _
                              savewithdocument:=msoCTrue, _
                              Left:=0, Top:=100, Width:=100, Height:=100

End Sub

Upvotes: 1

Views: 1815

Answers (2)

hunter21188
hunter21188

Reputation: 415

After doing more research, I finally came upon this, which does the job!

  Private Sub Combo1_DropButtonClick()

    If Combo1.ListCount = 0 Then
        With Combo1
            .AddItem "Ash Fork"
            .AddItem "Flagstaff"
            .AddItem "Winslow"
            .AddItem "Clints Well"
            .AddItem "Bellemont"

        End With
    End If

End Sub

Private Sub Combo2_DropButtonClick()

    If Combo2.ListCount = 0 Then
        With Combo2
            .AddItem "Ash Fork"
            .AddItem "Flagstaff"
            .AddItem "Winslow"
            .AddItem "Clints Well"
            .AddItem "Bellemont"

        End With
    End If

End Sub

Private Sub CommandButton1_Click()

  'Insert webcam for Combo1.
  If Combo1 = "Flagstaff" Then
  ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
   FileName:="K:\ALL PICTURES\Keeper_Photos\General Pictures\Aspens.jpg", _
   LinkToFile:=msoFalse, _
   SaveWithDocument:=msoTrue, Left:=60, Top:=0, _
   Width:=98, Height:=48).Select

  ElseIf Combo1 = "Ash Fork" Then
  ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
   FileName:="K:\ALL PICTURES\Keeper_Photos\General Pictures\gcanyon3.jpg", _
   LinkToFile:=msoFalse, _
   SaveWithDocument:=msoTrue, Left:=60, Top:=0, _
   Width:=98, Height:=48).Select

  End If

  'Insert webcam for Combo2.
  If Combo2 = "Flagstaff" Then
  ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
   FileName:="K:\ALL PICTURES\Keeper_Photos\General Pictures\Aspens.jpg", _
   LinkToFile:=msoFalse, _
   SaveWithDocument:=msoTrue, Left:=60, Top:=0, _
   Width:=98, Height:=48).Select

  ElseIf Combo2 = "Ash Fork" Then
  ActiveWindow.Selection.SlideRange.Shapes.AddPicture( _
   FileName:="K:\ALL PICTURES\Keeper_Photos\General Pictures\gcanyon3.jpg", _
   LinkToFile:=msoFalse, _
   SaveWithDocument:=msoTrue, Left:=60, Top:=0, _
   Width:=98, Height:=48).Select

  End If

End Sub

Upvotes: 0

Estevam Garcia
Estevam Garcia

Reputation: 445

You can use something similar to this in your command button event:

Dim image1 As String
Dim image2 As String

image1 = ComboBox1.Text
image2 = ComboBox2.Text

ActiveWindow.Selection.SlideRange.Shapes.AddPicture Filename:=image1, _
                                   linktofile:=msoFalse, _
                                   savewithdocument:=msoCTrue, _
                                   Left:=0, Top:=0, Width:=100, Height:=100

ActiveWindow.Selection.SlideRange.Shapes.AddPicture Filename:=image2, _
                                   linktofile:=msoFalse, _
                                   savewithdocument:=msoCTrue, _
                                   Left:=0, Top:=100, Width:=100, Height:=100

Upvotes: 1

Related Questions