|
楼主 |
发表于 2018-7-18 16:36
|
显示全部楼层
Attribute VB_Name = "Word模块导入"
Sub ModuleUpdating()
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim oVBProj As Object, oVBComp As Object, oDoc As Object
Dim strFilePath As String
Set fso = CreateObject("scripting.filesystemobject")
Set oDoc = ThisDocument
strFilePath = oDoc.Path & "\" & oDoc.Name
For i = 1 To Application.VBE.VBProjects.Count
If Application.VBE.VBProjects(i).fileName = strFilePath Then
Set oVBProj = Application.VBE.VBProjects(i)
End If
Next i
For Each oVBComp In oVBProj.VBComponents
Select Case oVBComp.Type
Case 1, 2, 3
With oVBProj.VBComponents
.Remove .Item(oVBComp.Name) '删除模块、类模块、窗体
End With
Case Else
oVBComp.CodeModule.DeleteLines 1, oVBComp.CodeModule.CountOfLines '删除工作表或Thisworkbook代码区代码
End Select
Next
' 选择模块所在文件夹
strFolderPath = oDoc.Path & "\" & "Word所有模块"
Set reg = CreateObject("vbscript.regexp")
Set oFolder = fso.getfolder(strFolderPath)
For Each file In oFolder.Files
strFileName = fso.getbasename(file)
sText = LCase(strFileName)
With reg
.Global = True '设置是否匹配所有的符合项,True表示匹配所有, False表示仅匹配第一个符合项
.IgnoreCase = True ' 设置是否区分大小写,True表示不区分大小写, False表示区分大小写
.Pattern = "^(thisdocument)" ' 判断是否可以找到匹配的字符,若可以则返回True
If .test(sText) Then '是document模块
Set objMatches = .Execute(sText)
strMatches = objMatches(0).Value '编号从0开始
oVBProj.VBComponents("ThisDocument").CodeModule.AddFromFile file
Else '非document模块
oVBProj.VBComponents.Import file
End If
End With
Next file
End Sub
|
|