我想遍歷文件夾(G:/ Proj)並找到名爲「SUMMARY LOG」的任何子文件夾,然後在每個文件夾內打印Excel文件(通常只有一個)。查找具有指定名稱的Windows子文件夾
主文件夾(PROJ)的所有項目文件夾這是我想打印出的文件只是一個截圖。
每個項目都有一個SUMMARY LOG文件夾。
這裏是VBA代碼。它循環遍歷每個子文件夾並打印出這些文件夾中的每個Excel文件,而不僅僅是「彙總日誌」。
Sub LoopFolders()
Dim strFolder As String
Dim strSubFolder As String
Dim strFile As String
Dim colSubFolders As New Collection
Dim varItem As Variant
Dim wbk As Workbook
' Parent folder including trailing backslash
strFolder = "G:/Proj/"
' Loop through the subfolders and fill Collection object
strSubFolder = Dir(strFolder & "*", vbDirectory)
Do While Not strSubFolder = ""
Select Case strSubFolder
Case ".", ".."
' Current folder or parent folder - ignore
Case Else
' Add to collection
colSubFolders.Add Item:=strSubFolder, Key:=strSubFolder
End Select
' On to the next one
strSubFolder = Dir
Loop
' Loop through the collection
For Each varItem In colSubFolders
' Loop through Excel workbooks in subfolder
strFile = Dir(strFolder & varItem & "\*.xls*")
Do While strFile <> ""
' Open workbook
Set wbk = Workbooks.Open(Filename:=strFolder & _
varItem & "\" & strFile, AddToMRU:=False)
' Do something with the workbook
ActiveSheet.PrintOut
' Close it
wbk.Close SaveChanges:=False
strFile = Dir
Loop
Next varItem
End Sub
'strFile = DIR(strFolder&varItem& 「\內容LOG \ *。XLS *」)' –