返回列表 上一主題 發帖

[發問] 如何 插入 資料夾中含子資料夾的 圖片

回復 19# GBKEE
GBKEE 版大, 早安!
我實際測了一陣子,發現 一執行到 With ActiveSheet.Pictures.Insert(f)
這一行便出現 104 的錯誤訊息, 經檢查語法也沒錯,就是會有錯誤訊息。
也令我百思不解。 我另外將其中 GetFolder 的處裡模組放入到另一支程式
裏測試都非常正常,我將程式碼  (在ThisWorkbook執行) 附上讓您參考。
  1. Option Explicit

  2. Sub Ex()
  3.     Dim fs As Object, f As Variant, e As Variant
  4.     Dim j As Integer, MyPath As String, MyFile As String
  5.    
  6.     j = 2
  7.     MyPath = ActiveWorkbook.Path & "\My Pictures\"
  8.    
  9.     Application.ScreenUpdating = False
  10.    
  11.     Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(MyPath)
  12.     For Each e In fs.subfolders  ' 資料夾集合物件 e = "D:\Workspaces\DATA\Excel 範例集錦\TXT\2012-12-12\14"
  13.         '  For Each f In e.Files  ' 檔案集合物件 f = "D:\Workspaces\DATA\Excel 範例集錦\TXT\2012-12-12\14\Winter.jpg"
  14.         With Sheets("工作表1")
  15.             .Pictures.Delete
  16.             While Cells(j, "C") <> ""
  17.                 If UCase(.Cells(j, "C")) Like "*ABCD*" Then  '字串中有"ABCD"
  18.                     'UCase 函數 傳回一個 Variant (String),所含為轉成大寫之字串。
  19.                     .Cells(j, "D").Select
  20.                                        
  21.                      Selection.RowHeight = 100
  22.                      Selection.ColumnWidth = 25
  23.                                             
  24.                      On Error Resume Next
  25.                      '   MyFile = Dir(e & "\*" & Cells(j, "C") & "*.*", vbDirectory)
  26.                      MyFile = Dir(e & "\*" & Cells(j, "C") & "*.*")
  27.                      If MyFile <> "" Then
  28.                          '  With .Pictures.Insert(MyPath & MyFile)
  29.                          With .Pictures.Insert(e & "\" & MyFile)
  30.                              .Top = Cells(j, "D").Top
  31.                              .Left = Cells(j, "D").Left
  32.                              .Height = 90
  33.                              .Width = 120
  34.                              .Cells(j, "D").RowHeight = .Height
  35.                              .Cells(j, "D").ColumnWidth = .Width / 5.5
  36.                              '  .ShapeRange.LockAspectRatio = msoTrue
  37.                              '  在調整圖案大小時,可以分別地調整圖案的長度和寬度
  38.                              '  .ShapeRange.LockAspectRatio = msoFalse
  39.                              '  .ShapeRange.Height = IIf(.ShapeRange.Height > 98, 98, .ShapeRange.Height)
  40.                              '  .ShapeRange.Width = IIf(.ShapeRange.Width > 150, 150, .ShapeRange.Width)
  41.                              '  .ShapeRange.Rotation = 0#
  42.                              '  .ShapeRange.IncrementLeft 2#
  43.                              '  .ShapeRange.IncrementTop 1#
  44.                              '  .Placement = xlMoveAndSize
  45.                              '  .PrintObject = True
  46.                          End With
  47.                          .Cells(j, "E") = MyFile
  48.                      End If
  49.                 End If
  50.                 j = j + 1
  51.             Wend
  52.             .Range("C2").Select
  53.         End With
  54.         '  Next    '  For Each f In e.Files
  55.     Next           '  For Each e In fs.subfolders
  56.     Application.ScreenUpdating = True
  57. End Sub
複製代碼

TOP

回復 21# GBKEE
  1. Option Explicit

  2. Sub Ex()
  3.     Dim fs, f, e As Variant, i As Integer, xCol As Integer
  4.    
  5.     Sheets(1).Activate
  6.     ActiveSheet.Pictures.Delete
  7.     xCol = 3    '欄數
  8.     Set fs = CreateObject("Scripting.FileSystemObject").GetFolder("D:\2012-12-12")
  9.     '**檔案,資料夾的命名中: 不可有  / \ : * ? < > |  這些字元
  10.     For Each e In fs.subfolders  '資料夾集合物件
  11.         i = 2       '列數
  12.         If Val(e.Name) >= [A1] And Val(e.Name) <= Range("B1") Then '如果我在A1輸入06  B1輸入12
  13.         'If e.Name >= 5 And e.Name <= 15 Then    '5 到 10
  14.             For Each f In e.Files    '檔案集合物件
  15.                 If UCase(f) Like "*.JPG" Or UCase(f) Like "*.GIF" Or UCase(f) Like "*.BMP" Then
  16.                 '預防不是圖片檔
  17.                     i = i + 1
  18.                     With ActiveSheet.Pictures.Insert(f)
  19.                         .Top = Cells(i, xCol).Top
  20.                         .Left = Cells(i, xCol).Left
  21.                         .Height = 49.5
  22.                         .Width = 49.5
  23.                         Cells(i, xCol).RowHeight = .Height
  24.                         Cells(i, xCol).ColumnWidth = .Width / 5.5
  25.                     End With
  26.                 End If
  27.             Next
  28.             xCol = xCol + 1   '欄數
  29.         End If
  30.     Next
  31. End Sub
複製代碼

供做測試用:   插入圖片2.rar (9.79 KB)

TOP

回復  c_c_lai
2003版 沒有錯誤!!
錯誤點 前  Debug.Print f   看看: 是哪個圖檔,將他刪掉試試
GBKEE 發表於 2012-12-13 12:09

測出癥結了,問題出在 f  變數之使用上:
  1.         With ActiveSheet.Pictures.Insert(f)
複製代碼
會出現 1004 的錯誤訊息,需修正為:
  1.         With ActiveSheet.Pictures.Insert(e & "\" & f.Name)
複製代碼
如此看來,For Each f In e.Files  的 f 在 2003 它可以當成字串直接處理,
而在 2010 時,  f 則視為一個物件 (Class),此時如果直接使用它執行
With ActiveSheet.Pictures.Insert(f) 就會出現 1004 的錯誤訊息。

TOP

        靜思自在 : 有心就有福,有願就有力,自造福田,自得福緣。
返回列表 上一主題