Board logo

標題: [發問] 如何 插入 資料夾中含子資料夾的 圖片 [打印本頁]

作者: oxrain    時間: 2011-9-28 15:01     標題: 如何 插入 資料夾中含子資料夾的 圖片

本帖最後由 oxrain 於 2011-9-28 15:06 編輯

請問 各位大大  要如何插入 資料夾中 含子資料夾  的圖片呢
因為將東西以資料夾 做分類存放

根目錄在D碟,主資料夾在 D:\PIC 及 D:\PIC01
而 D:\PIC 中有 001~070 不等約莫 50來個資料夾
而 D:\PIC01 中也有 010~070 約40來個

加上 D:\PIC 及 D:\PIC01 下,也有不在子資料夾的jpg檔
請教各位前輩  要如何加入呢?想說一個一個列,但會弄個好幾天,而且好笨...>.<
附上我的程式檔,麻煩各位賜教,我的頭快要爆了
  1. Sub 插入圖片()
  2.         Dim modelno, modelno1, picins As String
  3.         Dim modelno2%
  4.         modelno = InputBox("A1請輸入A1、C6請輸入C6,以此類推", "輸入商品型號起始欄位", "")
  5.         picins = InputBox("插入A欄請輸入A、插入C欄請輸入C,以此類推", "輸入商品圖片插入欄位", "")
  6.         If modelno = "" Or picins = "" Then
  7.           MsgBox("末確實輸入")
  8.         Else
  9.             modelno1 = Left(modelno, 1)
  10.           modelno2 = Mid(modelno, 2, 3)
  11.           Columns("" & picins & ":" & picins & "").Select
  12.           Selection.ColumnWidth = 20
  13.           Rows("" & modelno2 & ":9999").Select
  14.           Selection.RowHeight = 50
  15.           Dim a%
  16.           Dim name As String
  17.             For a = modelno2 To 9999
  18.                 name = Range("" & modelno1 & "" & a & "")
  19.                   If name <> "" Then
  20.                         Range("" & picins & "" & a & "").Select
  21.                           If Dir("D:\PICTURE\001\" & name & ".jpg") <> "" Then
  22.                                 ActiveSheet.Pictures.Insert(P).Select
  23.                                   Selection.ShapeRange.LockAspectRatio = msoTrue
  24.                                   Selection.ShapeRange.Height = 49.5
  25.                                   Selection.ShapeRange.IncrementLeft 0.75
  26.                           Else
  27.                                   Range("" & picins & "" & a & "") = "無圖片"
  28.                           End If
  29.                 End If
  30.             Next
  31.         End If
  32. End Sub
複製代碼

作者: GBKEE    時間: 2011-9-28 16:55

回復 1# oxrain
試試看
  1. Dim i As Integer
  2. Sub Ex()
  3.     Dim fs, f, e As Variant
  4.     Sheets(1).Activate
  5.     ActiveSheet.Pictures.Delete
  6.     i = 1
  7.     For Each e In Array("D:\PIC0", "D:\PIC01")
  8.         Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(e)
  9.         子資料夾 fs
  10.     Next
  11. End Sub
  12. Private Sub 子資料夾(TheFolder)
  13.     Dim fs As Object, f As Object
  14.     Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(TheFolder)
  15.     For Each f In fs.Files
  16.         If UCase(Mid(f, InStr(f, ".") + 1)) = "JPG" Then
  17.              With ActiveSheet.Pictures.Insert(f)
  18.                 .Top = Cells(i, "A").Top
  19.                 .Height = 49.5
  20.                 .ShapeRange.LockAspectRatio = msoTrue
  21.                 .ShapeRange.IncrementLeft 0.75
  22.             End With
  23.             i = i + 5
  24.        End If
  25.     Next
  26.     For Each f In fs.SubFolders
  27.          子資料夾 f
  28.     Next
  29. End Sub
複製代碼

作者: whirlwind963    時間: 2012-11-1 20:54

回復 2# GBKEE
請問
(1)可以選擇要從哪裡開始插入圖片嗎?例如C2開始
(2)我要10個圖就換一欄要怎麼用呢?
作者: GBKEE    時間: 2012-11-2 16:11

回復 3# whirlwind963
  1. Dim i As Integer, xCol As Integer
  2. Sub Ex()
  3.     Dim fs, f, e As Variant
  4.     Sheets(1).Activate
  5.     ActiveSheet.Pictures.Delete
  6.     'C2開始
  7.     i = 2       '列數
  8.     xCol = 3    '欄數
  9.     For Each e In Array("D:\相片")
  10.         Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(e)
  11.         子資料夾 fs
  12.     Next
  13. End Sub
  14. Private Sub 子資料夾(TheFolder)
  15.     Dim fs As Object, f As Object
  16.     Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(TheFolder)
  17.     For Each f In fs.Files
  18.         If UCase(Mid(f, InStr(f, ".") + 1)) = "JPG" Then
  19.              With ActiveSheet.Pictures.Insert(f)
  20.                 '.Top = Cells(i, "A").Top
  21.                  .Top = Cells(i, xCol).Top
  22.                  .Left = Cells(i, xCol).Left
  23.                 .Height = 49.5
  24.                 .Width = 49.5
  25.                 .ShapeRange.LockAspectRatio = msoTrue
  26.                 .ShapeRange.IncrementLeft 0.75
  27.             End With
  28.             i = i + 5
  29.               If i >= 5 * 10 Then  '10個圖就換一欄
  30.                 xCol = xCol + 1
  31.                 i = 2
  32.             End If
  33.        End If
  34.     Next
  35.     For Each f In fs.SubFolders
  36.          子資料夾 f
  37.     Next
  38. End Sub
複製代碼

作者: whirlwind963    時間: 2012-11-2 19:35

回復 4# GBKEE
再請教一個問題
有辦法在C欄顯示圖片D欄顯示圖片的名稱嗎
作者: whirlwind963    時間: 2012-11-3 02:34

回復 4# GBKEE
請問如果我加入
Cells(i, xCol + 1) = f
取得檔案名稱
有辦法只取得檔案名稱
而不要完整的路徑嗎
EX:D:\圖片\ABC.JPG
     我只要顯示ABC.JPG就好
作者: whirlwind963    時間: 2012-11-3 11:52

回復 2# GBKEE

如果改成
NN= cells(2,1)
MM=cells(3,1)
        For Each e In Array(NN,MM)
        Set fs = CreateObject("Scripting.FileSystemObject").GetFolder(e)
        子資料夾 fs
    Next
會出錯
請問有辦法自己在儲存格輸入位址嗎
作者: GBKEE    時間: 2012-11-3 15:06

回復 7# whirlwind963
5# 問 :  C欄顯示圖片D欄顯示圖片的名稱  你已在6# 自行解答,

6# 問 : 取得檔案名稱(f.Name),不要完整的路徑 f

7# 問 : 檢查  NN= cells(2,1),MM=cells(3,1)  的路徑對嗎?
有辦法自己在儲存格輸入位址嗎? 不懂你的意思.
作者: whirlwind963    時間: 2012-11-18 22:53

回復 4# GBKEE
請問一下
如果我有24個子資料夾
我想要
C1顯示子資料夾1的名稱C2顯示圖片
D1顯示子資料夾2的名稱D2顯示圖片
這樣的話該如何改呢
作者: GBKEE    時間: 2012-11-19 17:22

回復 9# whirlwind963
試試看
  1. Option Explicit
  2. Sub Ex()
  3.     Dim fs, f, e As Variant, i As Integer, xCol As Integer
  4.     Sheets(1).Activate
  5.     ActiveSheet.Pictures.Delete
  6.     xCol = 3    '欄數
  7.     Set fs = CreateObject("Scripting.FileSystemObject").GetFolder("D:\相片")
  8.     For Each e In fs.subfolders  '資料夾集合物件
  9.         i = 2       '列數
  10.         Cells(i, xCol) = e.Name
  11.         For Each f In e.Files    '檔案集合物件
  12.             If UCase(Mid(f, InStr(f, ".") + 1)) = "JPG" Then
  13.                 i = i + 1
  14.                 With ActiveSheet.Pictures.Insert(f)
  15.                     .Top = Cells(i, xCol).Top
  16.                     .Left = Cells(i, xCol).Left
  17.                     .Height = 49.5
  18.                     .Width = 49.5
  19.                     Cells(i, xCol).RowHeight = .Height
  20.                     Cells(i, xCol).ColumnWidth = .Width / 5.5
  21.                     End With
  22.             End If
  23.         Next
  24.         xCol = xCol + 1   '欄數
  25.     Next
  26. End Sub
複製代碼

作者: whirlwind963    時間: 2012-12-11 22:29

回復 10# GBKEE
請問一下
我的資料夾名稱是日期格式(2012/12/10)
子資料夾的名稱是小時格式(00-23)24個資料夾
如果我在A1輸入06  B1輸入12
就可以看到06-12這之間所有的圖片
這樣程式要怎麼改呢
作者: GBKEE    時間: 2012-12-12 08:35

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

作者: whirlwind963    時間: 2012-12-12 11:01

回復 12# GBKEE

我加了 If e.Name >= [A1] And e.Name <= Range("B1") Then 這一行後
無法顯示圖片耶
請問有別的方法嗎
還是哪裡錯了呢
作者: GBKEE    時間: 2012-12-12 11:55

回復 13# whirlwind963
這裡有改嗎?
  1. Set fs = CreateObject("Scripting.FileSystemObject").GetFolder("D:\2012-12-12")
  2.                       '**檔案,資料夾的命名中: 不可有  / \ : * ? < > |  這些字元
複製代碼

作者: whirlwind963    時間: 2012-12-12 13:50

回復 14# GBKEE
有阿
我確認檔名跟資料夾都沒有那些字元
作者: whirlwind963    時間: 2012-12-12 16:38

回復 16# GBKEE
我只改了路徑而已
其他的程式碼都一樣
在A1跟B1輸入數字
還是無法顯示圖片
作者: GBKEE    時間: 2012-12-12 20:21

回復 16# whirlwind963
上傳:檔案,資料夾 看看
作者: whirlwind963    時間: 2012-12-12 21:38

回復 17# GBKEE
因為上傳檔案的限制
我刪掉一些檔案
麻煩幫我測試看看
作者: GBKEE    時間: 2012-12-13 06:14

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

作者: c_c_lai    時間: 2012-12-13 11:00

回復 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
複製代碼

作者: GBKEE    時間: 2012-12-13 11:11

本帖最後由 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.                     '預防不是圖片檔
複製代碼

作者: c_c_lai    時間: 2012-12-13 11:39

回復 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
複製代碼
[attach]13510[/attach]
供做測試用:  [attach]13511[/attach]
作者: GBKEE    時間: 2012-12-13 12:09

回復 22# c_c_lai
2003版 沒有錯誤!!
錯誤點 前  Debug.Print f   看看: 是哪個圖檔,將他刪掉試試
作者: whirlwind963    時間: 2012-12-13 14:18

回復 19# GBKEE
這次可以顯示出圖片了
感謝GBKEE大大的幫助
作者: c_c_lai    時間: 2012-12-13 14:48

回復  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 的錯誤訊息。
作者: GBKEE    時間: 2012-12-13 15:03

回復 25# c_c_lai
謝謝:上了一課
作者: whirlwind963    時間: 2012-12-15 09:19

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

回復 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
複製代碼

作者: whirlwind963    時間: 2012-12-15 11:17

回復 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(資料夾)
可以請問上面跟下面的差別嗎
作者: GBKEE    時間: 2012-12-15 11:35

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





歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)