Public Sub ModifyFileNames()
Dim FolderPath As String
Dim FileNames As Variant Dim dotPos As Long
Dim ExtName As String
Dim RealName As String
Dim NewFile() As String
ReDim NewFile(1 To 1) As String
Dim Index As Long Dim StartTime As Variant
Dim UsedTime As Variant
StartTime = VBA.Timer 'Set ppApp = CreateObject("Powerpoint.Application") With Application.FileDialog(msoFileDialogFolderPicker)
.InitialFileName = ThisWorkbook.Path & "\"
.AllowMultiSelect = False
.Title = "请选取Excel工作簿所在文件夹"
If .Show = -1 Then
FolderPath = .SelectedItems(1)
Else
MsgBox "您没有选中任何文件夹,本次汇总中断!"
Exit Sub
End If
End With If Right(FolderPath, 1) <> Application.PathSeparator Then FolderPath = FolderPath & Application.PathSeparator FileNames = FsoGetFiles(FolderPath, "*PDF*|*DOC*|*PPT*")
Index = 0
For n = LBound(FileNames) To UBound(FileNames) Step 1
Debug.Print FileNames(n)
Index = Index + 1
ReDim Preserve NewFile(1 To Index)
FilePath = FileNames(n)
If UCase(FileNames(n)) Like "*.PDF" Then
'Debug.Print PdfPageCount(FilePath)
dotPos = InStrRev(FilePath, ".")
ExtName = Mid(FilePath, dotPos)
Debug.Print ExtName
RealName = Left(FilePath, dotPos - 1)
NewPath = RealName & "(" & PdfPageCount(FilePath) & ")页" & ExtName
On Error Resume Next
Kill NewPath
On Error GoTo 0
VBA.FileCopy FilePath, NewPath
NewFile(Index) = NewPath
On Error Resume Next
Kill FilePath
On Error GoTo 0
ElseIf UCase(FileNames(n)) Like "*.DOC*" Then
'Debug.Print WordPageCount(FilePath)
dotPos = InStrRev(FilePath, ".")
ExtName = Mid(FilePath, dotPos)
Debug.Print ExtName
RealName = Left(FilePath, dotPos - 1)
NewPath = RealName & "(" & GetFilePages(FilePath) & "页)" & ExtName
On Error Resume Next
Kill NewPath
On Error GoTo 0
VBA.FileCopy FilePath, NewPath
NewFile(Index) = NewPath
On Error Resume Next
Kill FilePath
On Error GoTo 0
ElseIf UCase(FileNames(n)) Like "*.PPT*" Then
'Debug.Print SlidePageCount(FilePath)
dotPos = InStrRev(FilePath, ".")
ExtName = Mid(FilePath, dotPos)
Debug.Print ExtName
RealName = Left(FilePath, dotPos - 1)
NewPath = RealName & "(" & GetFilePages(FilePath) & "页)" & ExtName
On Error Resume Next
Kill NewPath
On Error GoTo 0
VBA.FileCopy FilePath, NewPath
NewFile(Index) = NewPath
On Error Resume Next
Kill FilePath
On Error GoTo 0
End If
Next n UsedTime = VBA.Timer - StartTime
' Debug.Print "UsedTime :" & Format(UsedTime, "#0.0000 Seconds")
MsgBox "UsedTime :" & Format(UsedTime, "#0.0000 Seconds") End Sub
Private Function FsoGetFiles(ByVal FolderPath As String, ByVal Pattern As String, Optional ComplementPattern As String = "") As String()
Dim Arr() As String
Dim FSO As Object
Dim ThisFolder As Object
Dim OneFile As Object
Dim Pats As Variant ReDim Arr(1 To 1)
Arr(1) = "None"
Dim Index As Long
Dim p As Long
Index = 0
Set FSO = CreateObject("Scripting.FileSystemObject")
On Error GoTo ErrorExit
Set ThisFolder = FSO.getfolder(FolderPath)
If Err.Number <> 0 Then Exit Function If InStr(Pattern, "|") > 0 Then
Pats = Split(Pattern, "|")
Else
ReDim Pats(1 To 1) As String
Pats(1) = Pattern
End If For Each OneFile In ThisFolder.Files
For p = LBound(Pats) To UBound(Pats) If UCase(OneFile.Name) Like Pats(p) Then
If Len(ComplementPattern) > 0 Then
If Not UCase(OneFile.Name) Like ComplementPattern Then
Index = Index + 1
ReDim Preserve Arr(1 To Index)
Arr(Index) = OneFile.Path '& OneFile.Name
End If
Else
Index = Index + 1
ReDim Preserve Arr(1 To Index)
Arr(Index) = OneFile.Path '& OneFile.Name
End If Exit For
End If Next p
Next OneFile
ErrorExit:
FsoGetFiles = Arr
Erase Arr
Set FSO = Nothing
Set ThisFolder = Nothing
Set OneFile = Nothing
End Function
Private Function PdfPageCount(ByVal FilePath As String) As Long
Debug.Print FilePath
Dim OneMatch, mStr$
PdfPageCount = 0
With CreateObject("Scripting.FileSystemObject").OpenTextFile(FilePath)
mStr = .readall
.Close
End With
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
.Pattern = "\/Count ([\d]+)"
If .TEST(mStr) Then
For Each OneMatch In .Execute(mStr)
If Val(OneMatch.submatches(0)) > PdfPageCount Then
PdfPageCount = Val(OneMatch.submatches(0))
End If
Next OneMatch
End If
End With
End Function
Function GetFilePages(ByVal FilePath As String) As Variant
Dim AttrNo As Long
Select Case True
Case UCase(FilePath) Like "*.DOC*"
AttrNo = 148
Case UCase(FilePath) Like "*.PPT*"
AttrNo = 149
End Select
'工程-引用 “microsoft shell controls and automation”
Dim myShell As Shell32.Shell
Dim myShellFolder As Shell32.Folder
Dim FileName As String, Pos As Long, ExtName As String
Set myShell = New Shell
Pos = InStrRev(FilePath, "\")
FileName = Left(FilePath, Pos - 1)
ExtName = Mid(FilePath, Pos + 1)
Set myShellFolder = myShell.Namespace(FileName)
If myShellFolder.GetDetailsOf(myShellFolder.Items.Item(ExtName), AttrNo) <> "" Then
GetFilePages = myShellFolder.GetDetailsOf(myShellFolder.Items.Item(ExtName), AttrNo)
Else
GetFilePages = 0
End If
Set myShell = Nothing
Set myShellFolder = Nothing
End Function

  

20171024xlVBA批量获取PPT\WORD\PDF页数的更多相关文章

  1. PPT文档页数显示的增加和更新

    在PPT的右下角增加页数的显示能够帮助演讲者把握进度,所以会经常遇到需要把页数显示在右下角的情况,这次在制作ppt的时候也遇到了.因此在这里总结一下设置方法. 一.在右下角显示当前页数和总页数 1)获 ...

  2. c#获取word文件页数、字数

    引用命名空间:using Microsoft.Office.Interop.Word; //启动Word程序 Application myWordApp = new ApplicationClass( ...

  3. [Python Study Notes]批量将ppt转换为pdf v1.0

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  4. 获取PDF页数

    下载pdfbox这个包,这俩个方法都可以: PDDocument doc = PDDocument.load("e://aa.pdf"); System.out.println(d ...

  5. iTextSharp之pdfRead(两个文件文本内容的比较,指定页数的pdf截取,水印的添加)

    using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.pdf.parser; using System; us ...

  6. dotnet获取PDF文件的页数

    #region 获取PDF文件的页数 private int BytesLastIndexOf(Byte[] buffer, int length, string Search) { if (buff ...

  7. Atitit 计算word ppt文档的页数

    Atitit 计算word ppt文档的页数 http://localhost:8888/ http://git.oschina.net/attilax/ati_wordutil private vo ...

  8. [开发笔记]-C#获取pdf文档的页数

    [操作pdf文档]之C#判断pdf文档的页数: /// <summary> /// 获取pdf文档的页数 /// </summary> /// <param name=& ...

  9. 真正免费,不限页数的PDF转Word工具

    真正免费,不限页数的PDF转Word工具 我们知道PDF转Word工具非常多,但大部分都有各种限制,限大小,限页数,加水印等等. 这其中绝大部分其实并不能做到格式完全一样,遇到图片更是直接傻了. 我们 ...

随机推荐

  1. Jordan 块的几何

    设 $V$ 是复数域 $\mathbb{C}$ 上的 $n$ 维线性空间, $\varphi$ 是 $V$ 上的线性变换, $A\in M_n(\mathbb{C})$ 是 $\varphi$ 在某组 ...

  2. Flask学习【第6篇】:Flask中的信号

    实例化补充 instance_path和instance_relative_config是配合来用的.这两个参数是用来找配置文件的,当用app.config.from_pyfile('settings ...

  3. Mysql优化知识点总结(转自CS-Notes)

    转载地址:https://github.com/CyC2018/CS-Notes/blob/master/notes/MySQL.md 一.索引 B+ Tree 原理 MySQL 索引 索引优化 索引 ...

  4. 18 Issues in Current Deep Reinforcement Learning from ZhiHu

    深度强化学习的18个关键问题 from: https://zhuanlan.zhihu.com/p/32153603 85 人赞了该文章 深度强化学习的问题在哪里?未来怎么走?哪些方面可以突破? 这两 ...

  5. iframe初始化属性

    <iframe id="user" src="xxx.html" frameborder="0" width="" ...

  6. 用Qemu模拟vexpress-a9 --- 配置 qemu 的网络功能

    转载:http://wiki.sylixos.com/index.php/Linux%E7%8E%AF%E5%A2%83%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97 环境介 ...

  7. 【转载】C++宏定义详解

    摘自:http://blog.chinaunix.net/uid-21372424-id-119797.html   C++宏定义详解 2011-02-14 23:33:24   分类: C/C++ ...

  8. Javascript 高级程序设计(第3版) - 第01章

    2017-05-10 js简介 一个叫“不难登”的人发明的.js的流行是因为 ajax 的关系. js分为三个部分: 核心: ECMAScript 文档对象模型: DOM 浏览器对象模型: BOM 核 ...

  9. 【Java】【THINK】

    1. 新建类,应优先考虑“组织”对象,而不是继承.这样可以保持清爽. 2. Java对象&对象句柄: 声明了一个类型的变量也就是声明了一个该类型的对象.但是这个对象只是个抽象的概念,并不会在内 ...

  10. VR外包团队:魔幻的三星GearVR

    如果问大大菌,移动端什么产品最好,大大菌一定会和你说,是gearvr,对于已经购买了三星2015旗舰手机.又对VR技术感到好奇的用户,建议花99美元买一台Gear VR,绝对会让你物有所值. 在短短的 ...