Sub NewNameWithDocuments() Dim MyDialog As FileDialog, oDoc As Variant, myDoc As Document Dim myRange As Range, oldName As String, ErrArray() As Variant Dim oArray As Variant, newName As String On Error Resume Next '忽略错误 '定义一个导致文件名出错的数组 ErrArray = Array("\", "/", "*", "?", "<", ">", "|", """", Chr(9), Chr(11), Chr(13)) '定义一个文件夹选取对话框 Set MyDialog = Application.FileDialog(msoFileDialogFilePicker) With MyDialog .Filters.Clear '清除所有文件筛选器中的项目 .Filters.Add "所有 WORD 文件", "*.doc", 1 '增加筛选器的项目为所有WORD文件 .AllowMultiSelect = True '允许多项选择 If .Show <> -1 Then Exit Sub For Each oDoc In .SelectedItems '在所有选取项目中循环 newName = "" '以隐藏方式打开Word文档 Set myDoc = Word.Documents.Open(FileName:=oDoc, Visible:=False) Set myRange = myDoc.Paragraphs.First.Range '取得第1个段落区域 oldName = Application.CleanString(myRange.Text) '清除非打印字符 For Each oArray In ErrArray '遍历替换 oldName = Replace(oldName, oArray, "") Next myDoc.Close False '关闭文档 If Len(oldName) < 1 Then '如果长度小于1不处理 Else '进行重命名操作 newName = .InitialFileName & oldName & ".doc" '如果当前文件夹中没有此文件名 If Dir(newName, vbDirectory) = "" Then Name oDoc As newName '直接命名 Else '如果存在,则按时间序列数加命名 newName = Replace(newName, ".doc", "") newName = newName & Timer newName = Replace(newName, ".", "") newName = newName & ".doc" Name oDoc As newName End If End If Next End With End Sub 这是版主的程序,我不知怎么改才能有我要的效果,就是用文件名来替换word第一行文字 |