|
Sub word根据文件指定位置内容修改文件名()
On Error Resume Next
Dim p$, f$, WDApp As Object, mydoc As Object, txt$, sgdw$, xmmc$, xmfzr$
Dim reg, mh
Set reg = CreateObject("vbscript.regexp")
reg.Global = True
reg.MultiLine = True
p = ThisWorkbook.Path & "\"
Set WDApp = CreateObject("word.application")
WDApp.Visible = False
f = Dir(p & "*.doc*")
Do While f <> ""
sgdw = ""
xmmc = ""
xmfzr = ""
Set mydoc = WDApp.Documents.Open(p & f)
txt = mydoc.Content
mydoc.Close
reg.Pattern = "项目名称[::]([\S+]+).+?实施单位[::]([\S+]+).+?项目负责人[::]([\S+]+)"
If reg.test(txt) Then
Set mh = reg.Execute(txt)
xmmc = mh(0).submatches(0)
sgdw = mh(0).submatches(1)
xmfzr = mh(0).submatches(2)
End If
Name p & f As p & sgdw & "-" & xmmc & "-" & xmfzr & ".docx"
f = Dir
Loop
WDApp.Quit
Set WDApp = Nothing
End Sub |
|