Microsoft Office Forums

Go Back   Microsoft Office Forums > >

 
 
Thread Tools Display Modes
Prev Previous Post   Next Post Next
  #2  
Old 08-21-2016, 05:43 AM
gmayor's Avatar
gmayor gmayor is offline Macro to return a word count of text contained within a bookmark Windows 10 Macro to return a word count of text contained within a bookmark Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

In theory that is straightforward, though your idea of what constitues a 'word' to be counted my not coincide with Word's

Code:
Option Explicit

Sub Macro1()
Dim oTable As Table
Dim oRng As Range
Dim oBM As Range
    Set oTable = ActiveDocument.Tables(ActiveDocument.Tables.Count)
    If InStr(oTable.Cell(1, 1).Range.Text, "Bookmark") = 0 Then
        Set oRng = ActiveDocument.Range
        oRng.Collapse 0
        Set oTable = ActiveDocument.Tables.Add(oRng, 4, 2)
        Set oRng = oTable.Cell(1, 1).Range
        oRng.End = oRng.End - 1
        oRng.Text = "Bookmark"
        oRng.Font.Bold = True
        Set oRng = oTable.Cell(1, 2).Range
        oRng.End = oRng.End - 1
        oRng.Text = "Word Count"
        oRng.Font.Bold = True
        Set oRng = oTable.Cell(2, 1).Range
        oRng.End = oRng.End - 1
        oRng.Text = "Sustainability"
        oRng.Font.Bold = True
        Set oRng = oTable.Cell(3, 1).Range
        oRng.End = oRng.End - 1
        oRng.Text = "Team Working"
        oRng.Font.Bold = True
        Set oRng = oTable.Cell(4, 1).Range
        oRng.End = oRng.End - 1
        oRng.Text = "Total"
        oRng.Font.Bold = False
        oTable.Rows(2).Shading.BackgroundPatternColor = &HD8EFE1
        oTable.Rows(4).Shading.BackgroundPatternColor = &HD8EFE1
    End If
    Set oBM = ActiveDocument.Bookmarks("Sustainability").Range
    Set oRng = oTable.Cell(2, 2).Range
    oRng.End = oRng.End - 1
    oRng.Text = oBM.Words.Count
    oRng.Font.Bold = False
    Set oBM = ActiveDocument.Bookmarks("TeamWorking").Range
    Set oRng = oTable.Cell(3, 2).Range
    oRng.End = oRng.End - 1
    oRng.Text = oBM.Words.Count
    oRng.Font.Bold = False
    Set oRng = oTable.Cell(4, 2).Range
    oRng.End = oRng.End - 1
    oRng.Text = ""
    ActiveDocument.Fields.Add oRng, wdFieldExpression, "SUM(ABOVE)", False
lbl_Exit:
    Set oTable = Nothing
    Set oRng = Nothing
    Set oBM = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
 

Tags
word count macro



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro to return a word count of text contained within a bookmark FORMTEXT field contained in INCLUDETEXT bookmark emwinschuh Word 1 08-17-2015 08:55 AM
Macro to return a word count of text contained within a bookmark A recurring word count macro? bpanda Word VBA 1 06-11-2013 07:17 AM
Macro to return a word count of text contained within a bookmark Word Count Macro bpanda Word VBA 1 01-11-2013 06:51 PM
Macro to return a word count of text contained within a bookmark Word 2003 - IncludeText Does Not Include Bookmark Text if in a Form Text Control skarden Word 1 12-12-2011 10:39 PM
*Word 2007 Macro for Character Count gbartlet Word 0 07-21-2010 11:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:16 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft