返回列表 上一主題 發帖

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

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

本帖最後由 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
複製代碼

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

TOP

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

TOP

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

TOP

回復 4# GBKEE
再請教一個問題
有辦法在C欄顯示圖片D欄顯示圖片的名稱嗎

TOP

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

TOP

回復 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
會出錯
請問有辦法自己在儲存格輸入位址嗎

TOP

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

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

7# 問 : 檢查  NN= cells(2,1),MM=cells(3,1)  的路徑對嗎?
有辦法自己在儲存格輸入位址嗎? 不懂你的意思.

TOP

回復 4# GBKEE
請問一下
如果我有24個子資料夾
我想要
C1顯示子資料夾1的名稱C2顯示圖片
D1顯示子資料夾2的名稱D2顯示圖片
這樣的話該如何改呢

TOP

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

TOP

        靜思自在 : 愛不是要求對方,而是要由自身的付出。
返回列表 上一主題