刚开完盘车,然后看了下这题,写了代码如下:- Dim fso,fc,f,strName
- Set fso = CreateObject("scripting.filesystemobject")
- Set fc=fso.GetFolder(".").Files
- For Each f In fc
- If LCase(fso.GetExtensionName(f))="doc" Then
- strName=GetFileName(f.Path)
- End If
- f.Name=strName & ".doc"
- Exit For
- Next
- Set fso=Nothing
- Function GetFileName(FilePath)
- Dim i,objWord,Doc
- Set objWord=CreateObject("word.application")
- Set Doc=objWord.Documents.Open(FilePath)
- For i=1 To Doc.Paragraphs.Count
- If Replace(Doc.Paragraphs(i).Range," ","")<>vbCr Then
- GetFileName=Replace(Doc.Paragraphs(i).Range,vbCr,"")
- Exit For
- End If
- Next
- objWord.Quit
- Set objWord=Nothing
- End Function
复制代码
这个代码有点还不是很到位的,就是,如果标题含有特殊字符 \ / * | 之类的,那必须要过滤。其实用replace语句可以解决的,这里我偷下懒就不写了,睡觉,晚安~ |