首页 > Excel专区 > Excel函数 >

Excel VBA代码:批量插入指定目录下指定类型所有图片

Excel函数 2023-01-13

批量插入指定目录下指定类型所有图片Excel VBA代码:

<br />
Sub insertimg()<br />
””””””””””””””””””””””'<br />
‘ 批量插入指定目录下所有指定类型图片 ‘<br />
‘ ‘<br />
””””””””””””””””””””””'<br />
Dim mypath As String, nm As String<br />
Dim theSh As Object<br />
Dim theFolder As Object<br />
Dim i As Integer<br />
Application.ScreenUpdating = False ‘关闭屏幕更新<br />
On Error Resume Next<br />
‘设置搜索路径<br />
Set theSh = CreateObject("shell.application")<br />
Set theFolder = theSh.BrowseForFolder(0, "", 0, "")<br />
If Not theFolder Is Nothing Then<br />
mypath = theFolder.Items.Item.Path<br />
End If<br />
‘//////////////搜索并插入图片开始////////////////<br />
nm = Dir(mypath & "\*.jpg")<br />
‘第一次使用dir,必须指定pathname参数,返回符合条件的第1个文件名,修改扩展名可以查其它文件类型。<br />
Do While nm <> ""<br />
With Range("a" & i + 1)<br />
ActiveSheet.Shapes.AddShape(msoShapeRectangle, .Left, .Top, .Width, .Height).Select<br />
Selection.ShapeRange.Fill.UserPicture mypath & "\" & nm ‘插入图片<br />
End With<br />
i = i + 1<br />
nm = Dir ‘再次调用不需要pathname参数<br />
Loop<br />
Set sh = Nothing ‘释放内存<br />
Set theFolder = Nothing ‘释放内存<br />
Application.ScreenUpdating = True ‘开启屏幕更新<br />
End Sub<br />


Copyright © 2016-2023 office学习教程网 office.tqzw.net.cn. All Rights Reserved.