批量插入指定目錄下指定類型所有圖片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 ‘關(guān)閉屏幕更新<br />
On Error Resume Next<br />
‘設(shè)置搜索路徑<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參數(shù),返回符合條件的第1個(gè)文件名,修改擴(kuò)展名可以查其它文件類型。<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 ‘再次調(diào)用不需要pathname參數(shù)<br />
Loop<br />
Set sh = Nothing ‘釋放內(nèi)存<br />
Set theFolder = Nothing ‘釋放內(nèi)存<br />
Application.ScreenUpdating = True ‘開啟屏幕更新<br />
End Sub<br />