Excel精英培训网

 找回密码
 注册
数据透视表40+个常用小技巧,让你一次学会!
查看: 3096|回复: 6

[已解决]为选定目录下的所有WORD文档名称加上总页数

[复制链接]
发表于 2014-3-30 19:44 | 显示全部楼层 |阅读模式
为选定目录下的所有WORD文档名称加上总页数

先谢谢各位大侠了!

原文件名样式.JPG


统计.rar (77.79 KB, 下载次数: 9)
excel精英培训的微信平台,每天都会发送excel学习教程和资料。扫一扫明天就可以收到新教程
发表于 2014-3-30 20:57 | 显示全部楼层
你上次的那个代码单页变多页的代码稍微一改就成了。
回复

使用道具 举报

发表于 2014-3-30 21:12 | 显示全部楼层
  1. Sub batchWordRename()
  2.     Dim strPath As String, strFile As String, strNew$
  3.     Dim wd As Document
  4.     Dim lPage%
  5.     On Error Resume Next
  6.     With Application.FileDialog(msoFileDialogFolderPicker)
  7.         .AllowMultiSelect = False
  8.         .InitialFileName = ThisDocument.Path
  9.         If .Show Then
  10.             strPath = .SelectedItems(1) & Application.PathSeparator
  11.         Else
  12.             MsgBox "没有选择要修改的文件夹"
  13.             Exit Sub
  14.         End If
  15.     End With
  16.     Application.DisplayAlerts = False
  17.     Application.ScreenUpdating = False

  18.     strFile = Dir(strPath & "*.doc")
  19.     Do While Len(strFile)
  20.         If strFile <> ThisDocument.Name And (Not strFile Like "*页*") Then
  21.             Set wd = GetObject(strPath & strFile)
  22.             If Not wd Is Nothing Then
  23.                 With wd
  24.                     Application.StatusBar = "正在处理 " & strPath & strFile
  25.                     .ActiveWindow.Selection.EndKey Unit:=6
  26.                     lPage = .BuiltInDocumentProperties(14)
  27.                     .Close False
  28.                     strNew = Left(strFile, InStrRev(strFile, ".") - 1) & "_共" & lPage & "页" & Mid(strFile, InStrRev(strFile, "."))
  29.                     Name strPath & strFile As strPath & strNew
  30.                 End With
  31.             Else
  32.                 MsgBox strPath & strFile & "打开失败"
  33.             End If
  34.         End If
  35.         strFile = Dir
  36.     Loop
  37.     Application.StatusBar = False
  38.     Application.DisplayAlerts = True
  39.     Application.ScreenUpdating = True
  40.     MsgBox "ok"
  41. End Sub
复制代码
回复

使用道具 举报

发表于 2014-3-30 21:13 | 显示全部楼层    本楼为最佳答案   
  1. Sub batchWordRename()
  2.     Dim strPath As String, strFile As String, strNew$
  3.     Dim wd As Document
  4.     Dim lPage%
  5.     On Error Resume Next
  6.     With Application.FileDialog(msoFileDialogFolderPicker)
  7.         .AllowMultiSelect = False
  8.         .InitialFileName = ThisDocument.Path
  9.         If .Show Then
  10.             strPath = .SelectedItems(1) & Application.PathSeparator
  11.         Else
  12.             MsgBox "没有选择要修改的文件夹"
  13.             Exit Sub
  14.         End If
  15.     End With
  16.     Application.DisplayAlerts = False
  17.     Application.ScreenUpdating = False

  18.     strFile = Dir(strPath & "*.doc")
  19.     Do While Len(strFile)
  20.         If strFile <> ThisDocument.Name And (Not strFile Like "*共*页*") Then
  21.             Set wd = GetObject(strPath & strFile)
  22.             If Not wd Is Nothing Then
  23.                 With wd
  24.                     Application.StatusBar = "正在处理 " & strPath & strFile
  25.                     .ActiveWindow.Selection.EndKey Unit:=6
  26.                     lPage = .BuiltInDocumentProperties(14)
  27.                     .Close False
  28.                     strNew = Left(strFile, InStrRev(strFile, ".") - 1) & "_共" & lPage & "页" & Mid(strFile, InStrRev(strFile, "."))
  29.                     Name strPath & strFile As strPath & strNew
  30.                 End With
  31.             Else
  32.                 MsgBox strPath & strFile & "打开失败"
  33.             End If
  34.         End If
  35.         strFile = Dir
  36.     Loop
  37.     Application.StatusBar = False
  38.     Application.DisplayAlerts = True
  39.     Application.ScreenUpdating = True
  40.     MsgBox "ok"
  41. End Sub
复制代码

评分

参与人数 1 +9 收起 理由
yjwdjfqb + 9 太经典,一开始居然没有发现有避免二次重命.

查看全部评分

回复

使用道具 举报

 楼主| 发表于 2014-3-30 21:17 | 显示全部楼层
hwc2ycy 发表于 2014-3-30 20:57
你上次的那个代码单页变多页的代码稍微一改就成了。

在那儿呢老师!
回复

使用道具 举报

 楼主| 发表于 2014-3-30 21:17 | 显示全部楼层
hwc2ycy 发表于 2014-3-30 21:13

老师你好,我看三楼、四楼代码差不多,那个更好点呀!
回复

使用道具 举报

发表于 2014-3-30 21:32 | 显示全部楼层
4楼的避免二次重命名。随你便吧。

评分

参与人数 1 +9 收起 理由
yjwdjfqb + 9

查看全部评分

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|Excel精英培训 ( 豫ICP备11015029号 )

GMT+8, 2024-4-17 05:15 , Processed in 0.250580 second(s), 15 queries , Gzip On, Yac On.

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表