Format Zip Code for Barcoding

P

Paul

I have just inherited a system which consists of WORD 2000 forms and macros.
Many of the forms are launched by another application which passes parameters
to the form; so I have little control over the format of the input data. Zip
codes are passed to the forms as either five digits or nine digits with no
dash. I have a routine that will create a barcode for any zip code formatted
as "12345" or "12345-6789", but will not work for "123456789". How can I
change "123456789" into "12345-6789"?
 
G

Graham Mayor

If you can do this with fields in your document - see the Zip section of
http://www.gmayor.com/formatting_word_fields.htm

You can replace 9 digits with the required format in a document using
replace
i.e. a wildcard replace of
([!0-9][0-9]{5})([0-9]{4}[!0-9])
with
\1-\2
Much depends on where and how you want to replace the numbers.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
P

Paul

Thank you very much, Graham, but the VB editor is not liking the "find" part
of the replace statement as I am using it. It flags the [0-9] after the
first [!0-9], and says "Compile error: expected: )". Here is the code as I
have it:

If Len(ZIP$) = 9 Then

Replace(ZIP$,([!0-9][0-9]{5})([0-9]{4}[!0-9]),\1-\2)

End If

I'm sure it's just a syntax error, but I'm not seeing it.


Graham Mayor said:
If you can do this with fields in your document - see the Zip section of
http://www.gmayor.com/formatting_word_fields.htm

You can replace 9 digits with the required format in a document using
replace
i.e. a wildcard replace of
([!0-9][0-9]{5})([0-9]{4}[!0-9])
with
\1-\2
Much depends on where and how you want to replace the numbers.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
I have just inherited a system which consists of WORD 2000 forms and
macros. Many of the forms are launched by another application which
passes parameters to the form; so I have little control over the
format of the input data. Zip codes are passed to the forms as
either five digits or nine digits with no dash. I have a routine
that will create a barcode for any zip code formatted as "12345" or
"12345-6789", but will not work for "123456789". How can I change
"123456789" into "12345-6789"?
 
R

Russ

Paul,
Graham was not talking about using the Replace() function, he meant the
..Find method in Word VB*A*, which uses those wildcards. This forum is for
word.vba.general not VB.
However they are close enough that I would suggest using the format()
function.
ZIP$ = Format(ZIP$,"#####-####")
Thank you very much, Graham, but the VB editor is not liking the "find" part
of the replace statement as I am using it. It flags the [0-9] after the
first [!0-9], and says "Compile error: expected: )". Here is the code as I
have it:

If Len(ZIP$) = 9 Then

Replace(ZIP$,([!0-9][0-9]{5})([0-9]{4}[!0-9]),\1-\2)

End If

I'm sure it's just a syntax error, but I'm not seeing it.


Graham Mayor said:
If you can do this with fields in your document - see the Zip section of
http://www.gmayor.com/formatting_word_fields.htm

You can replace 9 digits with the required format in a document using
replace
i.e. a wildcard replace of
([!0-9][0-9]{5})([0-9]{4}[!0-9])
with
\1-\2
Much depends on where and how you want to replace the numbers.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
I have just inherited a system which consists of WORD 2000 forms and
macros. Many of the forms are launched by another application which
passes parameters to the form; so I have little control over the
format of the input data. Zip codes are passed to the forms as
either five digits or nine digits with no dash. I have a routine
that will create a barcode for any zip code formatted as "12345" or
"12345-6789", but will not work for "123456789". How can I change
"123456789" into "12345-6789"?
 
P

Paul

Russ,

Thank you. The format function did the trick. Part of the problem was my
unfamiliarity with the barcode logic. I thought I needed some more complex
VBA code, when a simple VB statement was all I really needed.

Paul

Russ said:
Paul,
Graham was not talking about using the Replace() function, he meant the
..Find method in Word VB*A*, which uses those wildcards. This forum is for
word.vba.general not VB.
However they are close enough that I would suggest using the format()
function.
ZIP$ = Format(ZIP$,"#####-####")
Thank you very much, Graham, but the VB editor is not liking the "find" part
of the replace statement as I am using it. It flags the [0-9] after the
first [!0-9], and says "Compile error: expected: )". Here is the code as I
have it:

If Len(ZIP$) = 9 Then

Replace(ZIP$,([!0-9][0-9]{5})([0-9]{4}[!0-9]),\1-\2)

End If

I'm sure it's just a syntax error, but I'm not seeing it.


Graham Mayor said:
If you can do this with fields in your document - see the Zip section of
http://www.gmayor.com/formatting_word_fields.htm

You can replace 9 digits with the required format in a document using
replace
i.e. a wildcard replace of
([!0-9][0-9]{5})([0-9]{4}[!0-9])
with
\1-\2
Much depends on where and how you want to replace the numbers.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP

My web site www.gmayor.com

<>>< ><<> ><<> <>>< ><<> <>>< <>><<>

Paul wrote:
I have just inherited a system which consists of WORD 2000 forms and
macros. Many of the forms are launched by another application which
passes parameters to the form; so I have little control over the
format of the input data. Zip codes are passed to the forms as
either five digits or nine digits with no dash. I have a routine
that will create a barcode for any zip code formatted as "12345" or
"12345-6789", but will not work for "123456789". How can I change
"123456789" into "12345-6789"?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top