G
Greg Maxey
A friend challenged me to replicate the Word2003 Word Count toolbar where
the feature is absent in Word2007.
The toolbar has two commands (control IDs 5739 and 5740). As best I can
tell 5739 corresponds command ToolsWordRecount. I can't find a
corresponding command for 5740.
Word2007 has a control idMso WordCountRecount that functions identical to
ToolsWordRecount?????. It is not included on any ribbon tab, but there just
the same. Word2007 also has a control idMso WordCount on the Review tab
that functions identical to the Word2003 command ToolsWordCount but it is
missing the "Show list option."
Striking out on builtin controls I thought I would try to create a userform
that performs the same task. I have come close using a
WindowsSelectionChange to detect text deletions, pasting, or re-selecting
changing blocks of text. All of these events cause the built-in toolbar to
reset.
The problem is that the built in control doesn't reset if you simply move
the IP. A bigger problem is the builtin toolbar reset if you add text by
typing. The WindowsSelectionChange event doesn't detect this.
Question - There is something that the built-in control uses to actively
monitor a changing character count. Does anyone know what this event is and
how it can be captured with VBA?
Thanks.
To replicate I create a form with one combobox and one command button.
Userform code is:
Option Explicit
Private Sub ComboBox1_Click()
pSel = Me.ComboBox1.ListIndex
End Sub
Private Sub CommandButton1_Click()
Application.Run MacroName:="ToolsWordCountRecount"
Me.ComboBox1.Clear
UserForm_Initialize
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
If Selection.Range.Start = Selection.Range.End Then
.AddItem ActiveDocument.ComputeStatistics(wdStatisticCharacters) & "
characters"
.AddItem
ActiveDocument.ComputeStatistics(wdStatisticCharactersWithSpaces) & "
characters with spaces"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticWords) & " Words"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticLines) & " Lines"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticPages) & " Pages"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticParagraphs) & "
Paragraphs"
Else
.AddItem Selection.Range.ComputeStatistics(wdStatisticCharacters) & "
characters"
.AddItem
Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces) & "
characters with spaces"
.AddItem Selection.Range.ComputeStatistics(wdStatisticWords) & " Words"
.AddItem Selection.Range.ComputeStatistics(wdStatisticLines) & " Lines"
.AddItem Selection.Range.ComputeStatistics(wdStatisticPages) & " Pages"
.AddItem Selection.Range.ComputeStatistics(wdStatisticParagraphs) & "
Paragraphs"
End If
End With
Me.ComboBox1.ListIndex = pSel
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ActiveDocument.Variables("pSel").Value = Me.ComboBox1.ListIndex
Set myDocMonitor = Nothing
Unload Me
End Sub
Standard Module code:
Option Explicit
Public myDocMonitor As myClass1
Public pSel As Long
Public bSuppressChangeEvent As Boolean
Sub SetMyClass1()
Set myDocMonitor = New myClass1
End Sub
Sub ShowForm()
Dim i As Long
bSuppressChangeEvent = True
On Error GoTo Err_Handler
pSel = CLng(ActiveDocument.Variables("pSel").Value)
SetMyClass1
Dim oRng As Word.Range
Set oRng = Selection.Range
UserForm1.Show vbModeless
Application.Activate
oRng.Select
bSuppressChangeEvent = False
Exit Sub
Err_Handler:
ActiveDocument.Variables("pSel").Value = "1"
Resume
End Sub
Sub PressA()
MsgBox "a pressed"
End Sub
Class module code:
Option Explicit
Private WithEvents mWordApp As Word.Application
Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub
Private Sub mWordApp_WindowSelectionChange(ByVal Sel As Selection)
If Not bSuppressChangeEvent Then
UserForm1.ComboBox1.Value = "<Click Recount to View>"
End If
End Sub
--
Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org
McCain/Palin '08 !!!
the feature is absent in Word2007.
The toolbar has two commands (control IDs 5739 and 5740). As best I can
tell 5739 corresponds command ToolsWordRecount. I can't find a
corresponding command for 5740.
Word2007 has a control idMso WordCountRecount that functions identical to
ToolsWordRecount?????. It is not included on any ribbon tab, but there just
the same. Word2007 also has a control idMso WordCount on the Review tab
that functions identical to the Word2003 command ToolsWordCount but it is
missing the "Show list option."
Striking out on builtin controls I thought I would try to create a userform
that performs the same task. I have come close using a
WindowsSelectionChange to detect text deletions, pasting, or re-selecting
changing blocks of text. All of these events cause the built-in toolbar to
reset.
The problem is that the built in control doesn't reset if you simply move
the IP. A bigger problem is the builtin toolbar reset if you add text by
typing. The WindowsSelectionChange event doesn't detect this.
Question - There is something that the built-in control uses to actively
monitor a changing character count. Does anyone know what this event is and
how it can be captured with VBA?
Thanks.
To replicate I create a form with one combobox and one command button.
Userform code is:
Option Explicit
Private Sub ComboBox1_Click()
pSel = Me.ComboBox1.ListIndex
End Sub
Private Sub CommandButton1_Click()
Application.Run MacroName:="ToolsWordCountRecount"
Me.ComboBox1.Clear
UserForm_Initialize
End Sub
Private Sub UserForm_Initialize()
With Me.ComboBox1
If Selection.Range.Start = Selection.Range.End Then
.AddItem ActiveDocument.ComputeStatistics(wdStatisticCharacters) & "
characters"
.AddItem
ActiveDocument.ComputeStatistics(wdStatisticCharactersWithSpaces) & "
characters with spaces"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticWords) & " Words"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticLines) & " Lines"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticPages) & " Pages"
.AddItem ActiveDocument.ComputeStatistics(wdStatisticParagraphs) & "
Paragraphs"
Else
.AddItem Selection.Range.ComputeStatistics(wdStatisticCharacters) & "
characters"
.AddItem
Selection.Range.ComputeStatistics(wdStatisticCharactersWithSpaces) & "
characters with spaces"
.AddItem Selection.Range.ComputeStatistics(wdStatisticWords) & " Words"
.AddItem Selection.Range.ComputeStatistics(wdStatisticLines) & " Lines"
.AddItem Selection.Range.ComputeStatistics(wdStatisticPages) & " Pages"
.AddItem Selection.Range.ComputeStatistics(wdStatisticParagraphs) & "
Paragraphs"
End If
End With
Me.ComboBox1.ListIndex = pSel
End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
ActiveDocument.Variables("pSel").Value = Me.ComboBox1.ListIndex
Set myDocMonitor = Nothing
Unload Me
End Sub
Standard Module code:
Option Explicit
Public myDocMonitor As myClass1
Public pSel As Long
Public bSuppressChangeEvent As Boolean
Sub SetMyClass1()
Set myDocMonitor = New myClass1
End Sub
Sub ShowForm()
Dim i As Long
bSuppressChangeEvent = True
On Error GoTo Err_Handler
pSel = CLng(ActiveDocument.Variables("pSel").Value)
SetMyClass1
Dim oRng As Word.Range
Set oRng = Selection.Range
UserForm1.Show vbModeless
Application.Activate
oRng.Select
bSuppressChangeEvent = False
Exit Sub
Err_Handler:
ActiveDocument.Variables("pSel").Value = "1"
Resume
End Sub
Sub PressA()
MsgBox "a pressed"
End Sub
Class module code:
Option Explicit
Private WithEvents mWordApp As Word.Application
Private Sub Class_Initialize()
Set mWordApp = Word.Application
End Sub
Private Sub mWordApp_WindowSelectionChange(ByVal Sel As Selection)
If Not bSuppressChangeEvent Then
UserForm1.ComboBox1.Value = "<Click Recount to View>"
End If
End Sub
--
Greg Maxey - Word MVP
My web site http://gregmaxey.mvps.org
McCain/Palin '08 !!!