天天看點

VBA學習筆記(9)--生成點撥(1)

VBA學習筆記(9)--生成點撥(1)

說明(2017.3.26):

1. 還沒寫完,寫到建立檔案夾了,下一步建立word,重命名,查找點撥,把點撥複制進去,因為要給點撥編号,應該會很麻煩

VBA學習筆記(9)--生成點撥(1)

1 Public Sub test1()
  2     Dim path
  3     Dim filename
  4     Dim folders(1 To 100)
  5     Dim i%, j%
  6     i = 1
  7     j = 1
  8 '    1. 先擷取所有的檔案夾
  9     path = ThisWorkbook.path & "\oriFolder\"
 10     folders(1) = path
 11 '    這裡的folders數組和下面的classes數組隻設定了100個長度,是為了調試友善,不然有時會出現大量空行,實際中可以增大。
 12 '    dir第二次無參數調用,傳回的是同一個檔案夾下的第二個檔案!!
 13 '    filename = Dir(folders(i), vbDirectory)這裡filename擷取的首先是folders(1)路徑下的檔案夾"."
 14 '    dir找到第一個檔案夾".",這時i=1,進入do循環,把oriFolder這一層的檔案夾都dir出來(101和102),
 15 '    找到一個檔案夾就把j加1(最後j=3),把folders(i)修改為"."路徑,101路徑和102路徑,裡面的do until循環就做了這麼個事
 16 '    do until做完之後,i要加1了,變成2,這時的filename = Dir(folders(i), vbDirectory),folders(2)就是do until循環裡已經修改的101路徑了,
 17 '    繼續do until循環,j目前=3,然後開始增加,目的是讓folders(j)數組繼續往後增加元素,等把101路徑裡所有檔案夾路徑添加進去之後,
 18 '    i變成3,再開始周遊102檔案夾
 19 '    如果101裡面還有檔案夾,就等把101和102都周遊完後,因為i每次隻加1,而j是隻要有一個檔案夾就加1,
 20 '    是以隻要i沒有到j的數量,就會一直周遊下去,把所有的子檔案周遊出來
 21     Do While i <= j
 22         filename = Dir(folders(i), vbDirectory) ' filename="."
 23         Do Until filename = ""
 24             If InStr(filename, ".") = 0 Then
 25                  j = j + 1
 26 '                當i=1的時候,folders(j)中的1,2,3分别是",",101,102目錄
 27                 folders(j) = folders(i) & filename & "\"
 28             End If
 29             filename = Dir
 30         Loop
 31         i = i + 1
 32     Loop
 33 '    For p = 1 To UBound(folders)
 34 '        If folders(p) <> "" Then
 35 '            Debug.Print (folders(p))
 36 '        End If
 37 '    Next
 38 '    2. 從每個檔案夾裡擷取所有課,存入一個數組
 39 Dim classes(1 To 100)
 40 Dim class
 41 Dim p
 42 Dim q
 43 p = 1
 44 q = 1
 45 
 46 For p = 1 To UBound(folders)
 47     If folders(p) <> "" Then
 48         class = Dir(folders(p) & "*.*")
 49         Do Until class = ""
 50             classes(q) = folders(p) & class
 51             q = q + 1
 52             class = Dir
 53         Loop
 54     End If
 55 Next
 56 
 57 
 58 '3. 在desFolder裡建立檔案夾,生成點撥rtf
 59 Dim path2
 60 '先來一套正則相關的dim as
 61 Dim reg As RegExp
 62 Dim myMatches As MatchCollection
 63 Dim myMatch As match
 64 Dim books(1 To 10)
 65 Dim bNum
 66 Dim m
 67 Dim n
 68 n = 1
 69 m = 1
 70 bNum = 1
 71 '再來一套操作word的dim as
 72 Dim wordApp As Word.Application
 73 Set wordApp = New Word.Application
 74 path2 = ThisWorkbook.path & "\desFolder\"
 75 Set reg = New RegExp
 76 '擷取所有版本檔案夾名
 77 filename2 = Dir(path, vbDirectory)
 78 Do Until filename2 = ""
 79     If InStr(filename2, ".") = 0 Then
 80         books(bNum) = filename2
 81         bNum = bNum + 1
 82     End If
 83     filename2 = Dir
 84 Loop
 85 '在desFolder裡面生成版本檔案夾
 86 For m = 1 To UBound(books)
 87 '    books(m)不為空,并且檔案夾不存在,就建立檔案夾
 88     If books(m) <> "" And Dir(path2 & books(m), vbDirectory) = "" Then
 89         MkDir (path2 & books(m))
 90 '        建立word,命名為“01_《繁星》_DianBo.doc”
 91 '        打開每課,查找點撥,複制到word中,格式為1-1-2-1-1【點撥】,第1單元-第1課-2複習-1課堂回顧-第1個點撥
 92 
 93         For n = 1 To UBound(classes)
 94             If classes(n) <> "" Then
 95                 wordApp.Documents.Open (classes(n))
 96                 
 97             End If
 98         Next
 99     End If
100 Next
101 'For x = 1 To UBound(classes)
102 '    If classes(x) <> "" Then
103 '        reg.Global = True '全局比對
104 '        reg.IgnoreCase = True '忽略大小寫
105 '        reg.Pattern = "(,*)?101_.*" '正規表達式
106 '        Set myMatches = reg.Execute(classes(x)) '比對到的結果傳回到myMatches集合
107 '        For Each myMatch In myMatches '周遊myMatches集合
108 '            If myMatch <> "" Then
109 '                Debug.Print (classes(x))
110 '            End If
111 '        Next
112 '
113 '    End If
114 'Next
115 
116 End Sub