将图片加载到Image Active X excel VBA

时间:2017-02-07 22:07:24

标签: excel vba

我需要一些帮助,情况是我有一些代码放在工作表上各种图像活动X我需要从文件夹路径中搜索图像我这个但是说当文件doest退出时我dloble检查和该文件位于该文件夹中。

conter = Range("A" & Rows.Count).End(xlUp).Row
inica = "C:\Mydocuments\folderpath\images\"
For i = 2 To conter
tope = Cells(i, "K").Top
izq = Cells(i, "K").Left
valor = Cells(i, "K").Value
ActiveSheet.OLEObjects.Add(ClassType:="Forms.Image.1", Link:=False, _
    DisplayAsIcon:=False, Left:=izq, Top:=tope, Width:=160, Height _
    :=55).Select
fullpath = inica & valor & ".jpg"
Image1.Picture = LoadPicture(fullpath)`enter code here`
Next

我有很多图像活动X,我不知道如何从每个人添加图像我想要chheck的文件的名称是在K列 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

试试这个。我添加了一个显示路径的消息框。此外,它将新的activex控件放入变量img中,这样您就可以使用多个控件

dim img as variant

conter = Range("A" & Rows.Count).End(xlUp).Row
inica = "C:\Mydocuments\folderpath\images\"
For i = 2 To conter
    tope = Cells(i, "K").Top
    izq = Cells(i, "K").Left
    valor = Cells(i, "K").Value

    set img = ActiveSheet.OLEObjects.Add(ClassType:="Forms.Image.1", Link:=False, _
        DisplayAsIcon:=False, Left:=izq, Top:=tope, Width:=160, Height _
        :=55)
    fullpath = inica & valor & ".jpg"

    'This is for debugging
    msgbox fullpath

    img.Object.Picture = LoadPicture(fullpath)`enter code here`
Next