• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Upgrade Word Count Macro - Further|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Upgrade Word Count Macro - Further|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosUpgrade Word Count Macro - Further
sp_PrintTopic sp_TopicIcon
Upgrade Word Count Macro - Further
Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
1
January 31, 2023 - 4:29 am
sp_Permalink sp_Print

I posted a macro, and @Velouria had a great solution of using a Function to browse for the folder, rather than hardcoding a location or using GetFolder. (Brilliant). I had already closed that one as my specific request had been answered. Anyways, My Boss now wants The Software Name (Word, Excel, Adobe, PowerPoint, etc....) to display in Column A, and the file extension to appear in Column B. This would then shift the current info of Document Name from (currently) column A to Column C. And No of Pages from Column B to Column C. Here is the current VBA code below. This code works on PDF files if I change the extension. However I have to do PowerPoint (count # of slides) and Excel (# of sheets) manually. Can a version of this code be "converted" to use for those softwares too? I would run it seperately, as all PP files are in a seperate folder, same is true for Excel.

Option Explicit
Sub CountPagesInDocs()

Const wdStatisticPages = 2
Dim wsStats As Worksheet
Dim objWrd As Object
Dim objDoc As Object
Dim strFileName As String
Dim strPath As String
Dim arrStats()
Dim cnt As Long
strPath = GetFolder & "\" ' This code uses Function code below to BROWSE

strFileName = Dir(strPath & "*.doc*")

Set objWrd = CreateObject("Word.Application")

objWrd.Visible = False

Do While Len(strFileName) 0
ReDim Preserve arrStats(1 To 2, cnt)
Set objDoc = objWrd.Documents.Open(strPath & strFileName)

arrStats(1, cnt) = strFileName

arrStats(2, cnt) = objDoc.ComputeStatistics(wdStatisticPages)

objDoc.Close
cnt = cnt + 1
strFileName = Dir
Loop

objWrd.Quit

Set objWrd = Nothing

Set wsStats = Sheets.Add

With wsStats
.Range("A1:B1").Value = Array("Document Name", "No of Pages")
.Range("A2:B2").Resize(UBound(arrStats, 2) + 1).Value = Application.Transpose(arrStats)
.Range("A1:B1").EntireColumn.AutoFit
End With

End Sub
Function GetFolder() As String
Dim dlg As fileDialog
Set dlg = Application.fileDialog(msoFileDialogFolderPicker)
dlg.InitialFileName = "C:\"
If dlg.Show = -1 Then
GetFolder = dlg.SelectedItems(1)
End If
End Function

Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 613
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
January 31, 2023 - 8:13 pm
sp_Permalink sp_Print

You could do something like this (it could probably do with a bit of refactoring and some error handling, but should get you started):

Option Explicit
Sub GetWordPageCounts()
CountPagesInFiles "Word"
End Sub

Sub GetAdobePageCounts()
CountPagesInFiles "Adobe"
End Sub

Sub GetExcelSheetCounts()
CountPagesInFiles "Excel"
End Sub

Sub GetPowerPointSlideCounts()
CountPagesInFiles "Powerpoint"
End Sub

Sub CountPagesInFiles(appName As String)
Dim arrStats()
Dim cnt As Long

Dim FilePath As String
FilePath = GetFolder & "\" ' This code uses Function code below to BROWSE

Dim startAppName As String
startAppName = appName

Dim FileExt As String
Dim countType As String
Select Case LCase$(appName)
Case "word"
FileExt = "doc*"
countType = "Pages"
Case "adobe"
startAppName = "Word" ' use Word to handle pdfs
FileExt = "pdf"
countType = "Pages"
Case "excel"
FileExt = "xl*"
countType = "Sheets"
Case "powerpoint"
FileExt = "ppt*"
countType = "Slides"
Case Else
MsgBox "Invalid application name!"
Exit Sub
End Select

Dim fileName As String
fileName = Dir(FilePath & "*." & FileExt)

If Len(fileName) 0 Then

Dim someApp As Object
Set someApp = CreateObject(startAppName & ".Application")

If LCase$(appName) "powerpoint" Then someApp.Visible = False

Do
ReDim Preserve arrStats(1 To 4, cnt)

arrStats(1, cnt) = appName
arrStats(2, cnt) = FileExt
arrStats(3, cnt) = fileName
arrStats(4, cnt) = GetPageCount(someApp, appName, FilePath & fileName)
cnt = cnt + 1
fileName = Dir

Loop While Len(fileName) 0

someApp.Quit

Set someApp = Nothing

Dim StatsSheet As Worksheet
Set StatsSheet = Sheets.Add

With StatsSheet
Dim colCount As Long
colCount = UBound(arrStats, 1)
.Range("A1").Resize(, colCount).Value = Array("Application", "File extension", "Document Name", "No of " & countType)
.Range("A2").Resize(UBound(arrStats, 2) + 1, colCount).Value = Application.Transpose(arrStats)
.Range("A1").Resize(, colCount).EntireColumn.AutoFit
End With
End If
End Sub
Function GetPageCount(app As Object, appName As String, fileName As String) As Long
Const wdStatisticPages = 2
Dim someFile As Object
Select Case LCase$(appName)
Case "word", "adobe"
Set someFile = app.Documents.Open(fileName)
GetPageCount = someFile.ComputeStatistics(wdStatisticPages)
someFile.Close savechanges:=False
Case "excel"
Set someFile = app.Workbooks.Open(fileName)
GetPageCount = someFile.Sheets.Count
someFile.Close savechanges:=False
Case "powerpoint"
Set someFile = app.presentations.Open(fileName, , , msoFalse)
GetPageCount = someFile.slides.Count
someFile.Close
End Select

End Function
Function GetFolder() As String
Dim dlg As FileDialog
Set dlg = Application.FileDialog(msoFileDialogFolderPicker)
dlg.InitialFileName = "C:\"
If dlg.Show = -1 Then
GetFolder = dlg.SelectedItems(1)
End If
End Function

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
3
February 2, 2023 - 1:17 am
sp_Permalink sp_Print

@Velouria,

I copied your solution into my VBA editor, and was about to try it when I noticed this. Normally code in red is some type of error, am I correct?

Annotation-2023-02-01-101337.pngImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage Annotation-2023-02-01-101337.png (34 KB)
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 613
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
February 2, 2023 - 4:45 pm
sp_Permalink sp_Print

Yes, it looks like the forum software stripped out any occurrences of a 'less than' symbol followed by a 'more than' symbol (presumably treated it as a HTML tag)

It should read:

If Len(fileName) <> 0 Then

Dim someApp As Object
Set someApp = CreateObject(startAppName & ".Application")

If LCase$(appName) <> "powerpoint" Then someApp.Visible = False

Do
ReDim Preserve arrStats(1 To 4, cnt)

arrStats(1, cnt) = appName
arrStats(2, cnt) = FileExt
arrStats(3, cnt) = fileName
arrStats(4, cnt) = GetPageCount(someApp, appName, FilePath & fileName)
cnt = cnt + 1
fileName = Dir

Loop While Len(fileName) <> 0

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
5
February 3, 2023 - 5:08 am
sp_Permalink sp_Print sp_EditHistory

Velouria,

Thanks. I made the corrections. I divided up my files (so the macro test would take less time). I started with Adobe, there were 2 files. I initially saw it freeze up, and eventually give me an error. I was forced to close Excel, and then I tried again. Then I got a run-time error. Screenshots of everything is attached. Not sure why this is happening.

.2023-02-02_13-42-16.pngImage Enlarger

2023-02-02_13-45-46.pngImage Enlarger

2023-02-02_13-46-33.pngImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage 2023-02-02_13-42-16.png (31 KB)
  • sp_PlupImage 2023-02-02_13-45-46.png (6 KB)
  • sp_PlupImage 2023-02-02_13-46-33.png (32 KB)
Avatar
Velouria
London or thereabouts
Moderator
Members


Trusted Members

Moderators
Level 4
Forum Posts: 613
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
6
February 6, 2023 - 7:20 am
sp_Permalink sp_Print

The first message is quite normal - you just need to say Yes and check the box to not ask again. (the code is not saving the files so there will be no permanent changes)

I suspect the other two messages are related to the first one, so you probablhy need to restart and try the code again.

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online:
Guest(s) 9
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
Ivica Cvetkovski
Blaine Cox
Shankar Srinivasan
riyepa fdgf
Hannah Cave
Len Matthews
Kristine Arthy
Michelle Neven
Andrew Kuhn
Angela Paul
Forum Stats:
Groups: 3
Forums: 24
Topics: 6206
Posts: 27202

 

Member Stats:
Guest Posters: 49
Members: 31875
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.