logo

Add thousands Separator in VB Message Box (VB 6.0)
alfred van cleef posted at Saturday, November 01, 2008 6: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

Reply    Reply Using Power Editor
  Rank Winnings Points
February 0 $0.00 0
January 0 $0.00 0
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.890
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

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


Didn't Find The Answer You Were Looking For?

EggHeadCafe has experts online right now that may know the answer to your question.  We pay them a bonus for answering as many questions as they can.  So, why not help them and yourself by becoming a member (free) and ask them your question right now?
Ask Question In Live Forum

If you have an OpenID and do not want to become a member of the EggHeadCafe forum, you can also sign on to Chat Chaos and post your question to our real time Silverlight chat application.
Ask Question In Chat Chaos










  $1000 Contest    [)ia6l0 iii - $228  |  Jonathan VH - $161  |  Huggy Bear - $135  |  F Cali - $95  |  egg egg - $94  |  more Advertise  |  Privacy  |   (c) 2010