- 帖子
- 2035
- 主題
- 24
- 精華
- 0
- 積分
- 2031
- 點名
- 0
- 作業系統
- Win7
- 軟體版本
- Office2010
- 閱讀權限
- 100
- 性別
- 男
- 註冊時間
- 2012-3-22
- 最後登錄
- 2024-2-1
|
回復 19# GBKEE
GBKEE 版大, 早安!
我實際測了一陣子,發現 一執行到 With ActiveSheet.Pictures.Insert(f)
這一行便出現 104 的錯誤訊息, 經檢查語法也沒錯,就是會有錯誤訊息。
也令我百思不解。 我另外將其中 GetFolder 的處裡模組放入到另一支程式
裏測試都非常正常,我將程式碼 (在ThisWorkbook執行) 附上讓您參考。- Option Explicit
- Sub Ex()
- Dim fs As Object, f As Variant, e As Variant
- Dim j As Integer, MyPath As String, MyFile As String
-
- j = 2
- MyPath = ActiveWorkbook.Path & "\My Pictures\"
-
- Application.ScreenUpdating = False
-
- Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(MyPath)
- For Each e In fs.subfolders ' 資料夾集合物件 e = "D:\Workspaces\DATA\Excel 範例集錦\TXT\2012-12-12\14"
- ' For Each f In e.Files ' 檔案集合物件 f = "D:\Workspaces\DATA\Excel 範例集錦\TXT\2012-12-12\14\Winter.jpg"
- With Sheets("工作表1")
- .Pictures.Delete
- While Cells(j, "C") <> ""
- If UCase(.Cells(j, "C")) Like "*ABCD*" Then '字串中有"ABCD"
- 'UCase 函數 傳回一個 Variant (String),所含為轉成大寫之字串。
- .Cells(j, "D").Select
-
- Selection.RowHeight = 100
- Selection.ColumnWidth = 25
-
- On Error Resume Next
- ' MyFile = Dir(e & "\*" & Cells(j, "C") & "*.*", vbDirectory)
- MyFile = Dir(e & "\*" & Cells(j, "C") & "*.*")
- If MyFile <> "" Then
- ' With .Pictures.Insert(MyPath & MyFile)
- With .Pictures.Insert(e & "\" & MyFile)
- .Top = Cells(j, "D").Top
- .Left = Cells(j, "D").Left
- .Height = 90
- .Width = 120
- .Cells(j, "D").RowHeight = .Height
- .Cells(j, "D").ColumnWidth = .Width / 5.5
- ' .ShapeRange.LockAspectRatio = msoTrue
- ' 在調整圖案大小時,可以分別地調整圖案的長度和寬度
- ' .ShapeRange.LockAspectRatio = msoFalse
- ' .ShapeRange.Height = IIf(.ShapeRange.Height > 98, 98, .ShapeRange.Height)
- ' .ShapeRange.Width = IIf(.ShapeRange.Width > 150, 150, .ShapeRange.Width)
- ' .ShapeRange.Rotation = 0#
- ' .ShapeRange.IncrementLeft 2#
- ' .ShapeRange.IncrementTop 1#
- ' .Placement = xlMoveAndSize
- ' .PrintObject = True
- End With
- .Cells(j, "E") = MyFile
- End If
- End If
- j = j + 1
- Wend
- .Range("C2").Select
- End With
- ' Next ' For Each f In e.Files
- Next ' For Each e In fs.subfolders
- Application.ScreenUpdating = True
- End Sub
複製代碼 |
|