![]() |
|
#2
|
||||
|
||||
|
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 |
| Tags |
| word count macro |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
FORMTEXT field contained in INCLUDETEXT bookmark
|
emwinschuh | Word | 1 | 08-17-2015 08:55 AM |
A recurring word count macro?
|
bpanda | Word VBA | 1 | 06-11-2013 07:17 AM |
Word Count Macro
|
bpanda | Word VBA | 1 | 01-11-2013 06:51 PM |
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 |