How to put dashes (-) after every third number?

I have documents that consists of a table on each page where I have to type in numbers in this format (###-###-###). I would like to just type in the numbers and then run a macro that would automatically enter the dashes.

I would like the macro to run from a command button on the right side of the first page but it can't print when the page is printed if that is possible!

I've attached a copy of the file I use to enter the numbers.

Thanks in advance for any help!
2011Box1-000-020-.doc
calbaisAsked:
Who is Participating?
 
GrahamSkanRetiredCommented:
If you require a macro, this will do it:
Sub FormatFileNumbers()
    With ActiveDocument.Range.Find
        .MatchWildcards = True
        .Text = "<([0-9]{3})([0-9]{3})([0-9]{3})>"
        .Replacement.Text = "\1-\2-\3"
        .Execute Replace:=wdReplaceAll
    End With
End Sub

Open in new window

However, don't put a button on a document, especially one like yours. It is multi-page, so the button would not always be visible. You could create a QAT button or a command button. Or you could make a keystroke shortcut.
You need to report the Word version for further details.
0
 
GrahamSkanRetiredCommented:
You could do a WildCard Find and Replace

Find: <([0-9]{3})([0-9]{3})([0-9]{3})>
Replace: \1-\2-\3

This is a good article on Word wildcards:
http://word.mvps.org/FAQs/General/UsingWildcards.htm
0
 
Let's GoCommented:
Alternatively, you could embed an Excel spreadsheet in the Word document (or just use Excel) and set the number format in Excel.

To do this in Word 2010, click on the arrow next to Insert Table, then choose Excel Spreadsheet.
Inserting an Excel spreadsheet
Then select the relevant cells in the (Excel) table, right-click and choose Format, select Custom and type in the format as 000-000-000.
Setting the number format
The numbers will then automatically be displayed in your desired format.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
calbaisAuthor Commented:
Pfsnet... I would love to use Excel since I'm more familiar with it but I have no control over that at work. We have to use the Word document they provide. Thanks though!

GrahamSkan... I'm going to give your macro idea a try at work today. That looks like what I want. I'm using Word 2010. I'll put a button on the ribbon to activate the macro.
Thanks for your suggestion!
0
 
calbaisAuthor Commented:
Thanks GrahamSkan. That macro works perfectly!!

I put a button on the ribbon so it would always be available to click but when I gave the document to a co-worker, the button was missing. Do you know if there is a way to make the button stay active when the document is distributed to someone else?

Regards

Cal
0
 
GrahamSkanRetiredCommented:
You would have to put the code and the keystroke in the document - or in its attached template which would also have to be distributed to your colleague.
0
 
calbaisAuthor Commented:
Yeah, I did that but it didn't work!

Anyway, I'll figure it out. You answered my question here so here's your points.

Thanks for your help.

Regards

Cal
0
 
calbaisAuthor Commented:
GrahamSkan answered quickly  and accurately. His solution worked perfectly the first time!!

Thanks very much!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.