|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
我想用word文档的标题重新命名文件,在网上找了这段代码,可以运行,但不出现重新命名的结果,请高手帮忙检查一下,这段代码有什么问题?
代码如下:
Function GetTitle(wrd, fileName)
on error resume next
Dim strTitle, strWord
Dim i
dim ThisDocument
wrd.Documents.Open(fileName)
strTitle = ""
if Err.Number <> 0 then
Err.clear
else
strTitle = ""
set ThisDocument = wrd.ActiveDocument
if (ThisDocument.Words.Count > 0) then
For i = 1 To ThisDocument.Words.Count
strWord = ThisDocument.Words.Item(i)
If Asc(strWord) = 13 and strTitle <> "" Then
Exit For
End If
if Asc(strWord) <> 13 then
strTitle = strTitle + strWord
if(len(strTitle) > 200) then exit for
end if
Next
else
GetTitle = "del"
end if
wrd.ActiveDocument.Close
end if
GetTitle = replace(strTitle,vbCrLf,"")
End Function
sub main
dim wrd
dim strDir,strFileName, strFileExt, strFilePath, strFileTitle,strNewTitle, strCmdLine
dim objFSO , objFolder, objFile, objLog
dim nPos, nIndex
set wrd = CreateObject("Word.Application")
wrd.visible=true
wrd.application.activate
strDir = "."
set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDir)
Set objLog = objFSO.CreateTextFile("rename.bat", True, True)
'´´½¨Ä¿Â¼
if not objFSO.FolderExists("deleted") then
objFSO.CreateFolder("deleted")
end if
if not objFSO.FolderExists("emptyFiles") then
objFSO.CreateFolder("emptyFiles")
end if
nIndex = 1
for each objFile in objFolder.Files
nPos = InStrRev(objFile, "\")
strFilePath = mid(objFile, 1, nPos)
strFileName = mid(objFile, nPos + 1)
nPos = InStrRev(strFileName , ".")
strFileTitle = mid(strFileName , 1, nPos)
strFilePathExt = mid(strFileName , nPos + 1)
if (ucase(strFilePathExt) = "DOC") and (asc(mid(strFileTitle ,1,1)) <> 126) then
strNewTitle = GetTitle(wrd, objFile & "")
if (not isnull(strNewTitle)) and (trim(strNewTitle) <> "") then
if(strNewTitle = "del") then
strCmdLine = "move " + strFileName + " deleted\"
else
nIndex = nIndex + 1
strCmdLine = "ren """ + strFileName + """ """ + strNewTitle & "_" & nIndex & ".doc"""
end if
else
strCmdLine = "move " + strFileName + " emptyFiles\"
end if
end if
objLog.WriteLine(strCmdLine )
strCmdLine = ""
next
objLog.Close
end sub
main
|
|