如何循环遍历工作表上的 VBA ActiveX (OLE) 图像控件并通过索引加载图片
How to loop through VBA ActiveX (OLE) image controls on worksheet and load picture through the index
我在工作表上有一堆 ActiveX 控件。我想知道是否可以循环遍历所有这些并根据特定条件加载图片。
的确,我可以遍历它们并使用以下方法打印它们的名字:
For each obj in SheetName.OLEObjects
Debug.Print obj.Name
Next obj
我也可以简单地加载图片
SheetName.Image1.picture = LoadPicture(路径)
我不能做的是通过循环加载图片:
For each obj in SheetName.OLEObjects
SheetName.OLEObjects("Image" & counter).Picture = LoadPicture(Path)
Next obj
使用控件名称加载它们会很乏味:SheetName.Image1.Picture=...., SheetName.Image2.Picture...
希望得到您的帮助!
请尝试下一种方式:
Sub testAddPictureByIteration()
Dim obj As OLEObject, Path As String
Path = ThisWorkbook.Path & "\yourPicture.bmp"
For Each obj In ActiveSheet.OLEObjects
If TypeOf obj.Object Is MSForms.Image Then 'to avoid buttons, text boxes etc.
obj.Object.Picture = LoadPicture(Path)
End If
End Sub
我在工作表上有一堆 ActiveX 控件。我想知道是否可以循环遍历所有这些并根据特定条件加载图片。
的确,我可以遍历它们并使用以下方法打印它们的名字:
For each obj in SheetName.OLEObjects
Debug.Print obj.Name
Next obj
我也可以简单地加载图片 SheetName.Image1.picture = LoadPicture(路径)
我不能做的是通过循环加载图片:
For each obj in SheetName.OLEObjects
SheetName.OLEObjects("Image" & counter).Picture = LoadPicture(Path)
Next obj
使用控件名称加载它们会很乏味:SheetName.Image1.Picture=...., SheetName.Image2.Picture...
希望得到您的帮助!
请尝试下一种方式:
Sub testAddPictureByIteration()
Dim obj As OLEObject, Path As String
Path = ThisWorkbook.Path & "\yourPicture.bmp"
For Each obj In ActiveSheet.OLEObjects
If TypeOf obj.Object Is MSForms.Image Then 'to avoid buttons, text boxes etc.
obj.Object.Picture = LoadPicture(Path)
End If
End Sub