VB 6.0 - Add thousands Separator in VB Message Box

Asked By alfred van cleef
01-Nov-08 06:59 PM

Hi all,

I am a Dutch, professional literary writer and I created a macro that helps me to count the words and pages of the book I am currently writing. For various reasons I chose not to use the regular built-in word counting tool for this goal. I use Windows XP, Word 2003 and VB 6.3. I wrote a macro, in which I would like the words and pages to be displayed with a (European) thousands separator in a Message Box. Now it will read in my Message Box: Words: 13456 or 123456 and I want it to read 13.456 or 123.456.

My regional settings are Dutch. The normal built-in word counting option in the Tools menu of Word 2003 does work well: it uses the regular European thousands separator and will read 13.456 for 13456 or 123.456 for 123456, but my message box numbers won't. Is there a way to change this behaviour? I tried the FontNumbers option, but without success.

Here is my macro (I changed Dutch into English or 'My Text'):

Sub CountWords()

MsgBox "Pages:  " & Selection.Information(wdNumberOfPagesInDocument) & vbCrLf _

& "Words: " & ActiveDocument.Range.ComputeStatistics(wdStatisticWords) _

& vbCrLf & vbCrLf & "My Text", vbOKOnly, "My Text"

End Sub

 

Thanks in advance,

Alfred

PS I am not an expert in programming at all, so I would prefer to just get some lines of code I could add to the above macro that will force the counted pages and words to use a thousands separator

Add thousands Separator in VB Message Box  Add thousands Separator in VB Message Box

02-Nov-08 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

counting words and pages with thousands separator  counting words and pages with thousands separator

02-Nov-08 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

02-Nov-08 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.890
Jack
try this example  try this example
03-Nov-08 09: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
Thanks + Save document that does not have focus  Thanks + Save document that does not have focus
03-Nov-08 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

 

Problem solved but thanks!  Problem solved but thanks!
03-Nov-08 10:28 AM
Hallo Adarsh! I just managed to solve my problem, but I will keep your information for future use. Thanks!
Create New Account
help
up Office I'm not at all sure what to do here. When I run windows update, the junk mail filter KB936677 for Outlook 2003 keeps on coming up time after bytes) OUTLFLTR.DLL 1.4.3504.0 300 KB (307, 440 bytes) I'm running Windows XP / sp2 and office 2003 / sp3 and searching around the web, there's other with the IE7, I choose not to install yet) have installed without a hitch, that's both Windows and Office. Even this (KB936677) update appears to have installed, giving the following message: 'The 2007 23:43:50 Build type: SHIP UNICODE 3.01.4000.4039 Calling process: C: \ WINDOWS \ system32 \ msiexec.exe = = = MSI (c) (2C:F4) [23:43:50:122]: Resetting cached policy values End dialog not enabled MSI (s) (18:A4) [23:43:51:413]: Original package = = > C: \ WINDOWS \ Installer \ 2ff58.msi MSI (s) (18:A4) [23:43:51:423]: Package we're running from = = > C: \ WINDOWS \ Installer \ 2ff58.msi MSI (s) (18:A4) [23:43:51:554]: APPCOMPAT: looking for appcompat copy from system32 MSI (s) (18:A4) [23:43:51:634]: Opening existing patch 'C: \ WINDOWS \ Installer \ 38c3c4.msp'. MSI (s) (18:A4) [23:43:53:767]: Opening existing patch 'C
Automatic updates Windows 7 These updates could not be installed for my computer when it tried to do automatic updates. What is the possible error and how to solve it Security Update for Windows XP (KB873339) Critical Update for Windows XP (KB886185) Security Update for Windows XP (KB885836) Security Update for Windows XP (KB888302) Security Update for Windows XP (KB891781) Security Update for Windows XP (KB885835) Security
Huge Amount Of Updates Won't Upload Windows 7 Please can anyone help? I have tried to download the listed updates but the running Kaspersky Anti Virus if that is any help. Many thanks, Critical Update for Office XP on Windows XP Service Pack 2 (KB885884) Security Update for Windows XP (KB873339) Critical Update for Windows XP (KB886185) Security Update for Windows XP (KB885836) Security Update for Windows XP (KB888302) Security Update
windows will not download securtiy updates Windows 7 Microsoft send me a message to download and install updates to my computer, and when i do. The security updates fail. These are the updates: Critical Update for Office XP on Windows XP Service Pack 2 (KB885884) Security Update for Windows XP (KB873339) Critical Update for Windows XP (KB886185) Security Update for Windows XP (KB885836) Security Update for Windows XP (KB888302) Security
Detecting Windows XP Embedded vs Windows XP Windows 7 Is there a way to programatically detect that windows XP embedded is running rather than windows XP. Registry settings, win32 api, .net api?? Thanks, Dan Windows XP Embedded Discussions Windows XP