|
原帖由 赵晓鹃 于 2010-8-6 09:56 发表
一个文件夹下有多个WORD文档,有两个问题一直困扰着我,恳请各位大虾帮助:
1.获取该文件夹下所有WORD文档的总页数,并在文件夹名称后标明该文件夹“共X页”
2.分别获取该文件夹下每一个WORD文档的页数,并在相应 ...
主要过程如下,如需遍历文件夹,可自行修改。
Option Explicit
Sub FolderPagesCount()
Dim myDialog As FileDialog
Dim myDoc As Document
Dim FSO As Object
Dim F As Object
Dim FS As Object
Dim FC As Object
Dim F1 As Variant
Dim astrCurArray() As String
Dim L As Long
Dim K As Long
Dim lngPage As Long
Dim lngPages As Long
Dim strFileName As String
Dim strFolderSpec As String
Dim strName As String
Set myDialog = Application.FileDialog(msoFileDialogFolderPicker)
With myDialog
If .Show <> -1 Then Exit Sub
strFolderSpec = .SelectedItems(1)
End With
Set FSO = CreateObject("Scripting.FileSystemObject")
Set F = FSO.GetFolder(strFolderSpec)
Set FC = F.Files
For Each F1 In FC
strName = F1.Name
If IsWordDocument(strName) Then
ReDim Preserve astrCurArray(L)
astrCurArray(L) = strName
L = L + 1
End If
Next
Set FC = Nothing
Set F = Nothing
Set FSO = Nothing
L = L - 1
ReDim astrNewArray(L)
Application.ScreenUpdating = False
For K = 0 To L
strFileName = strFolderSpec & "\" & astrCurArray(K)
Set myDoc = Documents.Open(FileName:=strFileName, Visible:=False, AddToRecentFiles:=False)
With myDoc
lngPage = .Content.Information(wdNumberOfPagesInDocument)
lngPages = lngPages + lngPage
.Close False
End With
strName = Replace$(astrCurArray(K), ".doc", "_共" & lngPage & "页.doc", , , vbTextCompare) '''如果Doc文档有多种类型,请修改本行代码
strName = strFolderSpec & "\" & strName
Name strFileName As strName
Next
Application.ScreenUpdating = True
If lngPages > 0 Then
Name strFolderSpec As strFolderSpec & "_共" & lngPages & "页"
MsgBox "已完成文档页数统计和重命名工作!", vbInformation, "Rousoft Office技术服务中心"
End If
End Sub
Private Function IsWordDocument(FileName As String) As Boolean
'''此处可根据情况添加Word文档文件类型
Dim strType As String
strType = Right$(FileName, 5)
IsWordDocument = (InStr(1, strType, ".doc", vbTextCompare) > 0)
End Function |
|