
Excel VBA代碼:批量插入指定目錄下指定類型所有圖片
批量插入指定目錄下指定類型所有圖片Excel VBA代碼:
Sub insertimg()””””””””””””””””””””””'‘ 批量插入指定目錄下所有指定類型圖片 ‘‘ ‘””””””””””””””””””””””'Dim mypath As String, nm As StringDim theSh As ObjectDim theFolder As ObjectDim i As IntegerApplication.ScreenUpdating = False ‘關閉屏幕更新On Error Resume Next‘設置搜索路徑Set theSh = CreateObject("shell.application")Set theFolder = theSh.BrowseForFolder(0, "", 0, "")If Not theFolder Is Nothing Thenmypath = theFolder.Items.Item.PathEnd If‘//////////////搜索并插入圖片開始////////////////nm = Dir(mypath & "*.jpg")‘第一次使用dir,必須指定pathname參數,返回符合條件的第1個文件名,修改擴展名可以查其它文件類型。Do While nm <> ""With Range("a" & i + 1)ActiveSheet.Shapes.AddShape(msoShapeRectangle, .Left, .Top, .Width, .Height).SelectSelection.ShapeRange.Fill.UserPicture mypath & "" & nm ‘插入圖片End Withi = i + 1nm = Dir ‘再次調用不需要pathname參數LoopSet sh = Nothing ‘釋放內存Set theFolder = Nothing ‘釋放內存Application.ScreenUpdating = True ‘開啟屏幕更新End Sub

