VB code to separate names and put each on a separate line.

  • Thread starter StargateFanFromWork
  • Start date
S

StargateFanFromWork

How would one go about this, pls? If I have a word list with names of
actors in it, say, and it looks something like this:



ANNA LOUISE PLOWMAN
ANNE MARIE LODER
ANTHONY ASHBEE
APRIL TELEK
BENJAMIN EASTERDAY



What vb code could I use to make the macro go through the above list and
separate the names and to put them on separate lines with no extra spaces
left anywhere, so that the above come out like this, instead:



ANNA
LOUISE
PLOWMAN
ANNE
MARIE
LODER
ANTHONY
ASHBEE
APRIL
TELEK
BENJAMIN
EASTERDAY



I don't need to sort or anything, the utility I use to maintain word lists
will do that automatically as well as take care of any duplicates that may
occur.

TIA. :eek:D
 
T

Tony Jollans

This should do it ...

With ActiveDocument.Range.Find
.Execute FindText:="^l", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^p", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^w", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:=" ", ReplaceWith:="^p", Replace:=wdReplaceAll
End With

.... If you have one or more spaces at the beginning of the document you will
get a single blank line at the beginning - and similarly at the end. This
can be dealt with but it's not worth it unless you're likely to need it.
Otherwise it will catch most things.
 
S

StargateFanFromWork

Tony Jollans said:
This should do it ...

With ActiveDocument.Range.Find
.Execute FindText:="^l", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^p", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^w", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:=" ", ReplaceWith:="^p", Replace:=wdReplaceAll
End With

... If you have one or more spaces at the beginning of the document you will
get a single blank line at the beginning - and similarly at the end. This
can be dealt with but it's not worth it unless you're likely to need it.
Otherwise it will catch most things.

Wow, that sure did, thank you! It went through a list of about 300 names in
just a few moments. I did have to go back and check out the results and
make a few corrections (I had a few "Jr."'s and lone middle initials to
contend with, a problem that I hadn't thought of <g>), but that took just a
couple of minutes. Then I added these results to the original list now
residing in my list manager utility which took care of sorting and
duplicates for me and the resulting word list is 828 items long with very
little hassle in under 5 minutes! Pretty kewl. I'm going to be making
puzzles with these word lists so the ones that need the individual words as
well as compound phrases will benefit with this macro very much. Thanks!
:eek:D
 
S

StargateFan

With ActiveDocument.Range.Find
.Execute FindText:="^l", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^p", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^w", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:=" ", ReplaceWith:="^p", Replace:=wdReplaceAll
End With

... If you have one or more spaces at the beginning of the document you will
get a single blank line at the beginning - and similarly at the end. This
can be dealt with but it's not worth it unless you're likely to need it.
Otherwise it will catch most things.

Hi!

1) I found that if I added things, it would make this more
comprehensive. But I ran into a problem with quotation marks. What
if you need to replace
.." (one space after)
to
.." (two spaces after)

This below doesn't work because of course, the quotation mark is used
in the macro to denote the end of the search term:

.Execute FindText:="." ", ReplaceWith:="." ",
Replace:=wdReplaceAll

Is there any way around this?

2) Also, there is another type of text formatting change I sometimes
need to make. It's to correct spacing after sentences with a
reference on the end to footnote, like this:

"... May of 1999.[4] (followed by 1 space)"

need to put two spaces after the "]" so that it looks like this:
"... May of 1999.[4] (2 spaces)"

These attempts don't work <g>:
.Execute FindText:=".[@] ", ReplaceWith:=".[@] ",
Replace:=wdReplaceAll

.Execute FindText:=".[?] ", ReplaceWith:=".[?] ",
Replace:=wdReplaceAll



So just those 2 things:

1) what to use in place of " to search for ."

2) how to replace a changing type of text as in the numbers in this
case (for all those legal docts.). The put 2 spaces after these
types of text entries:

.[3], .[24], .[159]

Thanks. :eek:D
 
G

Graham Mayor

You are trying to mix a wildcard search with Tony's non wildcard search.
Either run the wildcard searches as a separate process or modify the
non-wildcard patterns to those compatible with a wildcard search and add the
wildcard parameter - you might find
http://www.gmayor.com/replace_using_wildcards.htm useful.
As for your bracket issue, search for
(\^93)( [! ])
replace with
\1 \2
with the wildcard option set.

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


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
With ActiveDocument.Range.Find
.Execute FindText:="^l", ReplaceWith:=" ",
Replace:=wdReplaceAll .Execute FindText:="^p", ReplaceWith:="
", Replace:=wdReplaceAll .Execute FindText:="^w",
ReplaceWith:=" ", Replace:=wdReplaceAll .Execute FindText:="
", ReplaceWith:="^p", Replace:=wdReplaceAll End With

... If you have one or more spaces at the beginning of the document
you will get a single blank line at the beginning - and similarly at
the end. This can be dealt with but it's not worth it unless you're
likely to need it. Otherwise it will catch most things.

Hi!

1) I found that if I added things, it would make this more
comprehensive. But I ran into a problem with quotation marks. What
if you need to replace
." (one space after)
to
." (two spaces after)

This below doesn't work because of course, the quotation mark is used
in the macro to denote the end of the search term:

.Execute FindText:="." ", ReplaceWith:="." ",
Replace:=wdReplaceAll

Is there any way around this?

2) Also, there is another type of text formatting change I sometimes
need to make. It's to correct spacing after sentences with a
reference on the end to footnote, like this:

"... May of 1999.[4] (followed by 1 space)"

need to put two spaces after the "]" so that it looks like this:
"... May of 1999.[4] (2 spaces)"

These attempts don't work <g>:
.Execute FindText:=".[@] ", ReplaceWith:=".[@] ",
Replace:=wdReplaceAll

.Execute FindText:=".[?] ", ReplaceWith:=".[?] ",
Replace:=wdReplaceAll



So just those 2 things:

1) what to use in place of " to search for ."

2) how to replace a changing type of text as in the numbers in this
case (for all those legal docts.). The put 2 spaces after these
types of text entries:

.[3], .[24], .[159]

Thanks. :eek:D
 
T

Tony Jollans

1. To put a (single) quotation mark inside quotes, just double it up ...

.Execute FindText:="."" ", ReplaceWith:="."" "

2. This is rather more interesting. To search for a footnote mark use ^f;
when you find it you don't know the actual number so you must have it
replaced with what it found (plus the extra space) - the replace code for
'what I found' is ^&. So it would appear that you want to search for
^f(space) and replace it with ^&(space). There are two issues with this.

The first you should be aware of but probably won't cause you a problem. If
you do this via the UI Dialog it will change the footnote itself as well as
the reference in the document body. If you do it in VBA it will only change
the reference.

The second is that you end up with a rather odd formatting effect of the
added space being in the format of the footnote reference rather than the
following space (assuming they are different) - generally this gives you the
extra space in superscript and thus slightly smaller than you would expect
it to be. You can code round this if it is a problem but it does involve a
little messing about as you have to insert the space yourself rather than
using the Replace functionality of the Find&Replace.

--
Enjoy,
Tony

StargateFan said:
With ActiveDocument.Range.Find
.Execute FindText:="^l", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^p", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^w", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:=" ", ReplaceWith:="^p", Replace:=wdReplaceAll
End With

... If you have one or more spaces at the beginning of the document you
will
get a single blank line at the beginning - and similarly at the end. This
can be dealt with but it's not worth it unless you're likely to need it.
Otherwise it will catch most things.

Hi!

1) I found that if I added things, it would make this more
comprehensive. But I ran into a problem with quotation marks. What
if you need to replace
." (one space after)
to
." (two spaces after)

This below doesn't work because of course, the quotation mark is used
in the macro to denote the end of the search term:

.Execute FindText:="." ", ReplaceWith:="." ",
Replace:=wdReplaceAll

Is there any way around this?

2) Also, there is another type of text formatting change I sometimes
need to make. It's to correct spacing after sentences with a
reference on the end to footnote, like this:

"... May of 1999.[4] (followed by 1 space)"

need to put two spaces after the "]" so that it looks like this:
"... May of 1999.[4] (2 spaces)"

These attempts don't work <g>:
.Execute FindText:=".[@] ", ReplaceWith:=".[@] ",
Replace:=wdReplaceAll

.Execute FindText:=".[?] ", ReplaceWith:=".[?] ",
Replace:=wdReplaceAll



So just those 2 things:

1) what to use in place of " to search for ."

2) how to replace a changing type of text as in the numbers in this
case (for all those legal docts.). The put 2 spaces after these
types of text entries:

.[3], .[24], .[159]

Thanks. :eek:D
 
S

StargateFan

This should do it ...

With ActiveDocument.Range.Find
.Execute FindText:="^l", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^p", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:="^w", ReplaceWith:=" ", Replace:=wdReplaceAll
.Execute FindText:=" ", ReplaceWith:="^p", Replace:=wdReplaceAll
End With

... If you have one or more spaces at the beginning of the document you will
get a single blank line at the beginning - and similarly at the end. This
can be dealt with but it's not worth it unless you're likely to need it.
Otherwise it will catch most things.

Hi!

I've been looking for all this type of word equivalents to things we
might find in a doct.

Through trial and error I've been able to use these ^l and ^w, etc.,
in a couple of other macros I've been perfecting. They came in handy
right at a the right time. But I'd like to know what these are
called. I imagine that somewhere in the vbe one might be able to pull
up a list of these if one knew what they were called (?).

Searching through the archives, a couple of people have referred to
them as aliases, if I've interpreted the info correctly, but Genius
gives me other things that don't seem related to these "commands" (or
whatever they're called).

Do they have a name? I collect tips and keep them on my computer and
thumb drive and I'd definitely like to compile a list of these. TIA.
 
S

StargateFan

Hi!

I've been looking for all this type of word equivalents to things we
might find in a doct.

Through trial and error I've been able to use these ^l and ^w, etc.,
in a couple of other macros I've been perfecting. They came in handy
right at a the right time. But I'd like to know what these are
called. I imagine that somewhere in the vbe one might be able to pull
up a list of these if one knew what they were called (?).

Searching through the archives, a couple of people have referred to
them as aliases, if I've interpreted the info correctly, but Genius
gives me other things that don't seem related to these "commands" (or
whatever they're called).

Do they have a name? I collect tips and keep them on my computer and
thumb drive and I'd definitely like to compile a list of these. TIA.

Sorry, sorry <g>. I stumbled upon the answer. No wonder I wasn't
finding anything. Anyway, found answer here after posting this
question:
http://groups.google.ca/group/micro...05?lnk=gst&q=^l+^p+^w&rnum=4#ba92c1aab424d505

Another thing I'd never seen because the box is hidden is that there
are special characters in the search and replace dialogue box. You
can tell I don't need to use Word much, eh?

Anyway, I've created my tip file for this. This is so neat. One of
the biggest problems with Word and scripting was the lack of this
knowledge. In WordPerfect I'd been able to re-format docts since the
near mid-80s as the scripting language has always been very easy to
understand. But I don't have WP at the office. What a relief that I
can now do this no matter which suite is available.

Thanks for helping me stumble on to this. <g> Very, very much
appreciated.
 

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