返回列表 上一主題 發帖

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

本帖最後由 GBKEE 於 2012-12-13 11:15 編輯

回復 20# c_c_lai
程序中用 On Error Resume Next  有時會找不出錯誤點的
看一下  19#
  1.    
  2.            ' MyFile = Dir(e & "\*" & Cells(j, "C") & "*.*")  改用了   
  3.           If UCase(f) Like "*.JPG" Or UCase(f) Like "*.GIF" Or UCase(f) Like "*.BMP" Then
  4.                     '預防不是圖片檔
複製代碼

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

回復 22# c_c_lai
2003版 沒有錯誤!!
錯誤點 前  Debug.Print f   看看: 是哪個圖檔,將他刪掉試試

TOP

回復 19# GBKEE
這次可以顯示出圖片了
感謝GBKEE大大的幫助

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

回復 25# c_c_lai
謝謝:上了一課

TOP

回復 26# GBKEE
再問一個問題....
時間點的問題已解決
但是又遇到一個問題了
目前的巨集只能抓到指定時間點下面的圖片
如果裡面還有子資料夾
那怎麼辦呢

TOP

回復 25# c_c_lai
Dim  f As Variant   這樣試試, 2007還有錯誤嗎?     

回復 27# whirlwind963
  1. Option Explicit
  2. Dim i As Integer, xCol As Integer
  3. Sub Ex()
  4.     Dim fs, f As Object, e As Object
  5.     Sheets(1).Activate
  6.     ActiveSheet.Pictures.Delete
  7.     Set fs = CreateObject("Scripting.FileSystemObject").GetFolder("D:\2012-12-12")
  8.     xCol = 3    '欄數
  9.     For Each e In fs.subfolders     '資料夾集合物件
  10.         i = 1                       '列數
  11.         子資料夾 e
  12.         xCol = xCol + 1             '欄數
  13.     Next
  14. End Sub
  15. Private Sub 子資料夾(資料夾 As Variant)
  16.     Dim fs  As Object, f As Variant
  17.     Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(資料夾)
  18.     For Each f In fs.Files    '檔案:集合物件
  19.         If UCase(f) Like "*.JPG" Or UCase(f) Like "*.GIF" Or UCase(f) Like "*.BMP" Then
  20.             i = i + 1
  21.             With ActiveSheet.Pictures.Insert(f)
  22.                 .Top = Cells(i, xCol).Top
  23.                 .Left = Cells(i, xCol).Left
  24.                 .Height = 49.5
  25.                 .Width = 49.5
  26.                 Cells(i, xCol).RowHeight = .Height
  27.                 Cells(i, xCol).ColumnWidth = .Width / 5.5
  28.             End With
  29.         End If
  30.     Next
  31.     For Each f In fs.subfolders     '資料夾:集合物件
  32.         i = i + 1
  33.         子資料夾 f                  '再度呼叫 (本程序)
  34.     Next
  35. End Sub
複製代碼

TOP

回復 28# GBKEE
我看懂了~
感謝~
跟4#的程式碼差了一點
Private Sub 子資料夾(TheFolder)
    Dim fs As Object, f As Object
    Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(TheFolder)
=========================================================
    Private Sub 子資料夾(資料夾 As Variant)
        Dim fs  As Object, f As Variant
        Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(資料夾)
可以請問上面跟下面的差別嗎

TOP

回復 29# whirlwind963
是一樣的: 這引數 TheFolder 沒宣告型態 就是  As Variant
  1. Variant 資料型態
  2. Variant 資料型態是所有沒被明確宣告為其他型
複製代碼

TOP

        靜思自在 : 為自己找藉口的人永遠不會進步。
返回列表 上一主題