En or Em dash in character set

L

Laura Townsend

Greetings,

I have a little routine that adds a barcode field to the end of an address
(for the zip code).
To determine which part of the address is the zip code, it starts at the end
and goes backwards as long as it sees consecutive numbers and/or a dash.

My problem is that some users have gotten endashes and em dashes in their
zip codes instead of a regular dash. I don't know how to include them in
the character set. Copy/paste just put a regular dash in the VBA code. I
don't know their ascii value and wouldn't know how to include something like
that in the character set if I did.

Any suggestions? Below is the code of my little procedure. It starts with
the selection at the end of the address. (Feel free to recommend a more
elegant way to do this, too -- but my big concern is en/em dashes.

Many Thanks in advance!
Laura

Private Sub AddBarCode()
Dim rngZipCode As Range, sZipCode As String
Set rngZipCode = Selection.Range
rngZipCode.MoveStartWhile cset:="0123456789-", Count:=wdBackward
sZipCode = rngZipCode.Text
Selection.TypeParagraph
Selection.Fields.Add Range:=Selection.Range, Type:=wdFieldEmpty, Text:=
_
"BARCODE \u " & sZipCode, PreserveFormatting:=True
Selection.Collapse wdCollapseEnd
End Sub
 
J

JGM

Hi Laura,

The code for the dashes you are looking for are:
Chr(150) and Chr(151) - I am confused about en and em dashes... don't ask me
which one is which! (150 is the short one and 151 is the long one!)

So, in your code, change
rngZipCode.MoveStartWhile cset:="0123456789-", Count:=wdBackward
to
rngZipCode.MoveStartWhile cset:="0123456789-" & Chr(150) & Chr(151), _
Count:=wdBackward
etc.

A little trick if you ever want to find out the ASCII code for any
character:
Type/paste it somewhere in a document;
Select it;
then run this code:
myASCII = Asc(Selection.Text)
MsgBox myASCII

myASCII will give you the number you have to use in the brackets after the
Chr function...

HTH
Cheers!
 
L

Laura Townsend

Thank you so much!
The En dash is the shorter of the two, by the way -- typically used between
two spaces to separate numbers, whereas the em dash is longer, used w/out
spaces between two words. But I digress...

I got as far as the asc( ) but did it in the immediate window where the
pasting of the "dash" didn't work! Didn't think to select it in the doc.

MANY MANY MANY Thanks!
 

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