用于在占位符中插入图像的VBA代码

时间:2015-11-13 15:15:30

标签: vba

我第一次使用VBA并创建了一个包含两个下拉菜单和一个命令按钮的用户表单。一旦用户从每个下拉菜单中选择一个项目(" Image1"和" Image2"例如),他们将点击命令按钮,这将插入那些选定的项目(即图像)将从幻灯片上的特定图像占位符内的本地计算机上的文件中提取。

关于如何实现这一目标的任何想法?我目前有设计的表单,但它没有功能。我希望这很清楚。谢谢!

- UPDATE -

这是我到目前为止的VBA代码。接下来的问题是我在哪里将文件(例如" Ash Fork")链接到计算机上的本地图像:

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

2 个答案:

答案 0 :(得分:1)

您可以在命令按钮事件中使用与此类似的内容:

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

答案 1 :(得分:0)

经过更多的研究,我终于找到了这个,这就完成了工作!

  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