• 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
    • 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
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Alterations to Word Count macro|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Alterations to Word Count macro|VBA & Macros|Excel Forum|My Online Training Hub

Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search
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 & MacrosAlterations to Word Count macro
sp_PrintTopic sp_TopicIcon
Alterations to Word Count macro
Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 55
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
1
December 22, 2022 - 1:28 am
sp_Permalink sp_Print sp_EditHistory

I found this code, it works great, however, my need requires 1 change:

1)  Change the static path to a dynamic "browse to location"

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 = "C:\Test\" ' change this to the folder/path where you are storing your Word documents

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

sp_AnswersTopicSeeAnswer See Answer
Avatar
Velouria
London or thereabouts
Member
Members


Trusted Members
Level 4
Forum Posts: 574
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
2
December 22, 2022 - 7:56 pm
sp_Permalink sp_Print

Add this function below your code:

 

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

 

then amend your line to:

strPath = GetFolder & "\"

sp_AnswersTopicAnswer
Answers Post
Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 55
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
3
January 10, 2023 - 1:03 am
sp_Permalink sp_Print

@Velouria,

That was an AMAZING answer!!! I am new to functions, and most of the time, I have seen others provide a "browse code" to replace the path with. However your answer was so completely simple! I can EASILY use that code line and function code in other codes of mine to provide the browse for folder (I hate hard-coded folders). I am so excited!!! Thanks so very much!!!!!

Avatar
Velouria
London or thereabouts
Member
Members


Trusted Members
Level 4
Forum Posts: 574
Member Since:
November 1, 2018
sp_UserOfflineSmall Offline
4
January 10, 2023 - 2:13 am
sp_Permalink sp_Print

You're welcome. 🙂

That's exactly why I have it as a function - reusable anywhere!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 170
Currently Online: Riny van Eekelen, Christian Huapaya, Laxmi Praveen
Guest(s) 44
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1431
Anders Sehlstedt: 845
Velouria: 574
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 213
A.Maurizio: 202
Aye Mu: 201
Jessica Stewart: 185
Newest Members:
Vicky Otosnika
Abhishek Singh
Kevin Sojourner
Kara Weiss
And Woox
Armani Quenga
moshood bello
annelies b
James1989
lucy gilmour
Forum Stats:
Groups: 3
Forums: 24
Topics: 6045
Posts: 26524

 

Member Stats:
Guest Posters: 49
Members: 31492
Moderators: 2
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Riny van Eekelen
© Simple:Press —sp_Information
  • 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
 
  • About My Online Training Hub
  • Contact
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

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.

Download A Free Copy of 100 Excel Tips & Tricks

excel tips and tricks ebook

We respect your privacy. We won’t spam you.

x