|
Option Explicit
Const g_strRootPath = "C:\word2" '指定存放所有文件的目录,可以有子目录
Const g_nTitleMaxLen = 28 '指定获取文档里面第一段中的前多少个字符来作为文件名
'Call Main
Sub Main批量文档处理() '主函数入口
Dim fso, oFolder, oWordApp
Set oWordApp = CreateObject("Word.Application")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFolder = fso.GetFolder(g_strRootPath)
RenameDocFilesUnderFolder oWordApp, fso, oFolder
oWordApp.Quit
Set oWordApp = Nothing
MsgBox "完成!"
End Sub
Sub RenameDocFilesUnderFolder(oWordApp, fso, oFolder) '重命名指定文件夹(递归)下面的所有Word文件,按照文件里面的第一句可见的文字命名
Dim oSubFolder, oFile, oDoc
Dim strTitle, strFileName
For Each oSubFolder In oFolder.SubFolders
RenameDocFilesUnderFolder oWordApp, fso, oSubFolder
Next
For Each oFile In oFolder.Files
Set oDoc = oWordApp.Documents.Open(oFile.Path) '打开word
oWordApp.Visible = True 'word可见
' 激活WORD
Documents(oDoc).Activate '这里的激活不对,所以下面的代码就没有达到想要的结果 不知道哪里出了问题
' strTitle = GetFirstVisibleTextContent(oDoc) '获取文档第一行字 //要改为,进入排版要求
MsgBox ActiveDocument.Name
'===================================把页面格式改一下!录制而成~!
Selection.WholeStory
With ActiveDocument.Styles(wdStyleNormal).Font
If .NameFarEast = .NameAscii Then
.NameAscii = ""
End If
.NameFarEast = ""
End With
With ActiveDocument.PageSetup
.LineNumbering.Active = False
.Orientation = wdOrientPortrait
.TopMargin = CentimetersToPoints(2)
.BottomMargin = CentimetersToPoints(2)
.LeftMargin = CentimetersToPoints(2)
.RightMargin = CentimetersToPoints(2)
.Gutter = CentimetersToPoints(0)
.HeaderDistance = CentimetersToPoints(1.5)
.FooterDistance = CentimetersToPoints(1.75)
.PageWidth = CentimetersToPoints(21)
.PageHeight = CentimetersToPoints(29.7)
.FirstPageTray = wdPrinterDefaultBin
.OtherPagesTray = wdPrinterDefaultBin
.SectionStart = wdSectionNewPage
.OddAndEvenPagesHeaderFooter = False
.DifferentFirstPageHeaderFooter = False
.VerticalAlignment = wdAlignVerticalTop
.SuppressEndnotes = False
.MirrorMargins = False
.TwoPagesOnOne = False
.BookFoldPrinting = False
.BookFoldRevPrinting = False
.BookFoldPrintingSheets = 1
.GutterPos = wdGutterPosLeft
.LayoutMode = wdLayoutModeLineGrid
End With
'===================================
' 保存WORD
' Documents(oDoc).Save
oDoc.Close SaveChanges:=wdSaveChanges '文档关闭 //改为保存再关闭
Set oDoc = Nothing
'下面代码为改名代码 // 不要
' If Len(strTitle) <> 0 Then
' strFileName = fso.BuildPath(fso.GetParentFolderName(oFile.Path), strTitle & "." & fso.GetExtensionName(oFile.Path))
' strFileName = GetUniqueFileName(fso, strFileName)
' fso.MoveFile oFile.Path, strFileName
' End If
Next
End Sub
|
|