Format thousand separator and identify 4 digit year

Hi, every one.

 

 

Sometimes I want to display 4 digits number with thousand separator in MS-Word, if I write VBA macro for this problem( Microsoft Word 2003.WinXP SP3) . using the selection.find .Text = "[0-9]{4}" to find the number. I confused about one trouble, this document is a long one that 4 digits number may be date(Year) such as 2008,2012. So I plan to use the MsgBox to let the user decide which number is number or date.

 

so I think the process will be the steps below:

step 1: find the 4 digits number in this document;

step 2: if the finding condition satisfied, then use the Msgbox to ask the user confirm the number is not a date;

step 3: if the number is not a date, then display this one with thousand separator;

step 4: find the next 4 digits number( equally go to the step 1) until the whole document finished.

 

The big trouble I faced was: I don’t know how to write all of them in Word-VBA.

 

Could you write them for me or give me some key hints about the frustrated thousand separator adding.

 

Thanks a lot. I would appreciate your help with this.

Answer
Answer
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Word.Range
  Set oRng = ActiveDocument.Range
  With oRng.Find
    .Text = "[0-9]{3,}"
    .MatchWildcards = True
    While .Execute
      oRng.Select
      If MsgBox("Do you want to format this instance", vbQuestion + vbYesNo, "FORMAT") = vbYes Then
        oRng = Format(oRng, "#,##0")
      End If
      oRng.Collapse wdCollapseEnd
    Wend
  End With
End Sub
Greg Maxey
***
Death smiles at us all, but all a man can do is smile back.


For more help with Word visit:
http://gregmaxey.com/word_tips.html

6 people found this reply helpful

·

Was this reply helpful?

Sorry this didn't help.

Great! Thanks for your feedback.

How satisfied are you with this reply?

Thanks for your feedback, it helps us improve the site.

How satisfied are you with this reply?

Thanks for your feedback.

 
 

Question Info


Last updated January 14, 2023 Views 2,100 Applies to: