怎么批量把word的“标题”设置成为“文件名”?

文件名是“我的办公文档1” 标题为空! 怎么批量把word的“标题”设置成为“文件名”?注意:是批量!!

VBS处理
代码如下

Option Explicit
On Error Resume Next ' 容错语句,避免程序崩溃 '有可能重命名文件已经存在,忽略错误。

Dim fso,fs,f
Dim i
Dim strNewName
Const strCurrentPath = "."

Msgbox "根据文本文件第一行批量重命名的VBS程序" & vbcrlf & vbcrlf & "Created By Shortway",0,"QQ:380710203"

Set fso = Wscript.CreateObject("Scripting.FileSystemObject")
Set fs = fso.GetFolder(strCurrentPath).Files
i = 0
For Each f In fs '遍历当前文件夹内每个文件
If LCase(right(f.name,3))="txt" Then '判断是否是文本文件
strNewName = Trim(fso.OpenTextFile(f, 1, False).ReadLine) '得到第一行

'以下剔除不能作文件名的特殊字符
strNewName = Replace(strNewName, "\", "")
strNewName = Replace(strNewName, "/", "")
strNewName = Replace(strNewName, ":", "")
strNewName = Replace(strNewName, "*", "")
strNewName = Replace(strNewName, "?", "")
strNewName = Replace(strNewName, """", "")
strNewName = Replace(strNewName, ">", "")
strNewName = Replace(strNewName, "<", "")
strNewName = Replace(strNewName, "|", "")

strNewName = left(strNewName,50) '有时第一行文字太多了,就选50个字符了

f.name=strNewName & ".txt"
i = i + 1
End if
Next
Msgbox i & "个文件改名完成!(忽略重名)"
Set fs = Nothing
Set fso = Nothing

将以上代码粘贴到记事本中,然后另存为后缀为vbs文件,如“标题修改.vbs”,把要处理的文件与vbs文件放在同一文件夹中,然后运行vbs文件,数据无价,修改时请先实验测试。
温馨提示:答案为网友推荐,仅供参考
第1个回答  2022-11-03
亲测可用,唯一不足就是遇到错误不会跳过,希望有大佬能补充一下代码:
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
f.Name=GetFileName(f.Path) & ".docx"
End If
If LCase(fso.GetExtensionName(f))="docx" Then
f.Name=GetFileName(f.Path) & ".docx"
End If
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