Discussion:
Need help writing macro
(too old to reply)
Joe Payne
2005-08-16 10:57:15 UTC
Permalink
I appologize for posting in two newsgroups. I didn't know I wasn't
supposed to until I had already posted the second message.

Need help writing macro
I frequently receive emails that I forward on to other people. Because
some of them are forwarded to me the text is sometimes scattered and
contains symbols that need to be removed. I have created a macro to
remove the symbols but I can’t figure out how to fix the formatting. I
want to consolidate the text in each paragraph, removing the blank
spaces, and then seperate the paragraphs with double blank lines.The
macro would also need to wrap the lines in each paragraph. I may not
be using the correct terminology so I will show an example below. This
would be what is left after I remove the symbols.
------------------------------------------------------------------------------------------------------------


On a visit to my wife's native
England for our honeymoon, we arrived at London's Gatwick Airport.
Tania headed for the British- passport control line while I, an
American, waited in the foreigners' line. When my turn came,
the customs officer asked me the purpose of my
visit. "Pleasure," I replied. "I'm on my honeymoon." The officer
looked first to one side of me, then the other. "That's very
interesting, sir," he
said as he stamped my passport. "Most men bring their wives with
them."






One afternoon shortly after I was married,
the phone rang. "Collect call from Dorothy," said the operator. "Will
you accept the charges?" Because I couldn't think of anyone
named Dorothy, I said no and hung up. The phone rang again a moment
later. "Hi, Brenda, it's Dorothy," said the familiar voice.
"Your mother-in- law."
-------------------------------------------------------------------------------------------------------------------


And this is what I want it to look like after running the macro.


On a visit to my wife's native England for our honeymoon, we arrived
at London's Gatwick Airport. Tania headed for the British- passport
control line while I, an American, waited in the foreigners' line.
When my turn came, the customs officer asked me the purpose of my
visit. "Pleasure," I replied. "I'm on my honeymoon." The officer
looked first to one side of me, then the other. "That's very
interesting, sir," he said as he stamped my passport. "Most men bring
their wives with them."


One afternoon shortly after I was married, the phone rang. "Collect
call from Dorothy," said the operator. "Will you accept the charges?"
Because I couldn't think of anyone named Dorothy, I said no and hung
up. The phone rang again a moment later. "Hi, Brenda, it's Dorothy,"
said the familiar voice. "Your mother-in- law."
-------------------------------------------------------------------------------------------------------------------

Any help would be greatly appreciated. Thanks. Joe
Chuck Henrich
2005-08-16 11:56:01 UTC
Permalink
Hi Joe

You didn't say whether you're fixing the formatting in Outlook or Word.
Assuming you're working in Word and that you're manually selecting the text
to be reformatted, the following code replaces returns and extra spaces. The
extra spaces search-replace uses wildcards, which you might want to play
around with. The FlushFind portion of the code cleans out search-specific
settings in the replace dialog. There are fancier, more efficient ways of
doing this, but hopefully the following code is a starting point for you to
get the basic concepts.

Sub RemoveReturnsSpaces()

With Selection.Find
.Text = "^p"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute Replace:=wdReplaceAll
End With

GoSub FlushFind

Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = " {2,}"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
End With
Selection.Find.Execute Replace:=wdReplaceAll

GoSub FlushFind

Exit Sub

FlushFind:

With Selection.Find
.ClearFormatting
.ClearAllFuzzyOptions
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.Execute
End With

Return

End Sub

HTH
--
Chuck Henrich
www.ProductivityApps.com
Post by Joe Payne
I appologize for posting in two newsgroups. I didn't know I wasn't
supposed to until I had already posted the second message.
Need help writing macro
I frequently receive emails that I forward on to other people. Because
some of them are forwarded to me the text is sometimes scattered and
contains symbols that need to be removed. I have created a macro to
remove the symbols but I can’t figure out how to fix the formatting. I
want to consolidate the text in each paragraph, removing the blank
spaces, and then seperate the paragraphs with double blank lines.The
macro would also need to wrap the lines in each paragraph. I may not
be using the correct terminology so I will show an example below. This
would be what is left after I remove the symbols.
------------------------------------------------------------------------------------------------------------
On a visit to my wife's native
England for our honeymoon, we arrived at London's Gatwick Airport.
Tania headed for the British- passport control line while I, an
American, waited in the foreigners' line. When my turn came,
the customs officer asked me the purpose of my
visit. "Pleasure," I replied. "I'm on my honeymoon." The officer
looked first to one side of me, then the other. "That's very
interesting, sir," he
said as he stamped my passport. "Most men bring their wives with
them."
One afternoon shortly after I was married,
the phone rang. "Collect call from Dorothy," said the operator. "Will
you accept the charges?" Because I couldn't think of anyone
named Dorothy, I said no and hung up. The phone rang again a moment
later. "Hi, Brenda, it's Dorothy," said the familiar voice.
"Your mother-in- law."
-------------------------------------------------------------------------------------------------------------------
And this is what I want it to look like after running the macro.
On a visit to my wife's native England for our honeymoon, we arrived
at London's Gatwick Airport. Tania headed for the British- passport
control line while I, an American, waited in the foreigners' line.
When my turn came, the customs officer asked me the purpose of my
visit. "Pleasure," I replied. "I'm on my honeymoon." The officer
looked first to one side of me, then the other. "That's very
interesting, sir," he said as he stamped my passport. "Most men bring
their wives with them."
One afternoon shortly after I was married, the phone rang. "Collect
call from Dorothy," said the operator. "Will you accept the charges?"
Because I couldn't think of anyone named Dorothy, I said no and hung
up. The phone rang again a moment later. "Hi, Brenda, it's Dorothy,"
said the familiar voice. "Your mother-in- law."
-------------------------------------------------------------------------------------------------------------------
Any help would be greatly appreciated. Thanks. Joe
Loading...