| Add thousands Separator in VB Message Box |
| Jignesh Shah replied to alfred van cleef on Sunday, November 02, 2008 12:46 AM |
|
Hi Alfred,
select all of the doc and try either of below:
MsgBox Selection.Words.Count MsgBox Selection.Range.ComputeStatistics(wdStatisticWords)
The second comes closer to what you want, though it is regrettable that Word uses different methods for word count.
Alternativelly you can use below macro:
Sub countWords() Dim nWords As Long Dim rDcm As Range Dim oWrd As Range Set rDcm = ActiveDocument.Range For Each oWrd In rDcm.Words If Len(oWrd) > 0 Then nWords = nWords + 1 End If Next MsgBox "Total word count= " & nWords End Sub
You can also see http://web.ticino.com/multilingual/word_character_count_tips.htm for getting various macros for word count with explaination how to run it and use it.
-Jack
|
| Reply Reply Using Power Editor |
| "I am electric engg and professor in college. Working on all the softwar languages are my passion." - Jignesh |
| |
Rank |
Winnings |
Points |
| February |
0 |
$0.00 |
0 |
| January |
0 |
$0.00 |
0 |
|
|
|
|
|
|
| counting words and pages with thousands separator |
| alfred van cleef replied to Jignesh Shah on Sunday, November 02, 2008 11:53 AM |
|
Hallo Jack,
Thanks very much for your fast answer. In my macro I did use already the ComputeStatistics (wdStatisticWords) method and it does count the words correctly. It does not use a thousands separator, however, as the regular words counting option in the Word 2003 Tools menu does. So the word count that is displayed in my Message Box reads 123456 instead of 123.456 which is how I want the numbers to be displayed. Your CountWords macro does not display a thousands separator either.
Alfred
|
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| February |
0 |
$0.00 |
0 |
| January |
0 |
$0.00 |
0 |
|
|
|
|
|
|
| reply |
Jignesh Shah provided a rated reply to alfred van cleef on Sunday, November 02, 2008 10:48 PM |
|
Ohhh...Ok use below code it does it: You will need to user Format function thats it :-)
Sub countWords() Dim nWords As Long Dim rDcm As Range Dim oWrd As Range Set rDcm = ActiveDocument.Range For Each oWrd In rDcm.Words If Len(oWrd) > 0 Then nWords = nWords + 1 End If Next MsgBox "Total word count= " & Format(nWords,"#,##0.0")
End Sub Details of functions that you can use:
For example: a = 12345000
Format(a,"#,##0.0") --> 12,345,000.0
Format(a,"#,###") --->12,345,000
value = 1234567890
value.ToString("#,#", CultureInfo.InvariantCulture)) --> 1,234,567,890
value.ToString("#,#",CultureInfo.CreateSpecificCulture("el-GR"))) --> 1.234.567.890Jack
|
| Reply Reply Using Power Editor |
| "I am electric engg and professor in college. Working on all the softwar languages are my passion." - Jignesh |
| |
Rank |
Winnings |
Points |
| February |
0 |
$0.00 |
0 |
| January |
0 |
$0.00 |
0 |
|
|
|
|
|
|
| try this example |
| C_A P replied to alfred van cleef on Monday, November 03, 2008 9:47 AM |
|
Example
This example shows various uses of the Format function to format
values using both String formats and user-defined formats. For
the date separator (/), time separator (:), and the AM/PM
indicators (t and tt), the actual formatted output displayed
by your system depends on the locale settings the code is using. When
times and dates are displayed in the development environment, the short
time format and short date format of the code locale are used.
Dim MyDateTime As Date = #1/27/2001 5:04:23 PM# Dim MyStr As String ' Returns current system time in the system-defined long time format. MyStr = Format(Now(), "Long Time") ' Returns current system date in the system-defined long date format. MyStr = Format(Now(), "Long Date") ' Also returns current system date in the system-defined long date ' format, using the single letter code for the format. MyStr = Format(Now(), "D") ' Returns the value of MyDateTime in user-defined date/time formats. MyStr = Format(MyDateTime, "h:m:s") ' Returns "5:4:23". MyStr = Format(MyDateTime, "hh:mm:ss tt") ' Returns "05:04:23 PM". MyStr = Format(MyDateTime, "dddd, MMM d yyyy") ' Returns "Saturday, ' Jan 27 2001". MyStr = Format(MyDateTime, "HH:mm:ss") ' Returns "17:04:23"
MyStr = Format(23) ' Returns "23". ' User-defined numeric formats. MyStr = Format(5459.4, "##,##0.00") ' Returns "5,459.40". MyStr = Format(334.9, "###0.00") ' Returns "334.90". MyStr = Format(5, "0.00%") ' Returns "500.00%".
MyStr = Format(23.99, "$##,##0.00") ' Returns "$23.00".
more info available on http://www.seattlecentral.edu/~ymoh/mic110vb/function_list.htm
|
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| February |
0 |
$0.00 |
0 |
| January |
67 |
$0.00 |
1 |
|
|
|
|
|
|
| Thanks + Save document that does not have focus |
| alfred van cleef replied to Jignesh Shah on Monday, November 03, 2008 10:24 AM |
|
Hi Jack,
Thanks for your answer. Since the CountWords macro also counts paragraphs and punctuation as words, I kept on using the WordStatistic method, because it only counts real words. Using the principle of your format code, I managed to have it work exactly as desired. Thank you so much!!
I feel free to ask you one more – very last! – question. After having lost a day’s work when shutting down Word in a regular way, but without saving my work, I installed – to prevent this ever happening again - the following macro that I found on the internet:
Sub AutoOpen()
WordSaver
End Sub
Sub WordSaver()
Application.OnTime When:=Now + _
TimeValue("00:10:00"), _
Name:="Saver"
End Sub
Sub Saver()
ActiveDocument.Save
WordSaver
End Sub
I attached this macro to the specific document that I am working on, which is called Boek (Book). This macro gives an error, however, when the active document does not have focus. Is there a way to make this macro work, whenever the document called ‘Boek’ is open, even when it does not have focus?
Thanks!
Alfred
|
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| February |
0 |
$0.00 |
0 |
| January |
0 |
$0.00 |
0 |
|
|
|
|
|
|
| Problem solved but thanks! |
| alfred van cleef replied to C_A P on Monday, November 03, 2008 10:28 AM |
|
Hallo Adarsh! I just managed to solve my problem, but I will keep your information for future use. Thanks! |
| Reply Reply Using Power Editor |
| |
| |
Rank |
Winnings |
Points |
| February |
0 |
$0.00 |
0 |
| January |
0 |
$0.00 |
0 |
|
|
|
|
|
|
|