MS Word VBA Code
I Get It! Development has been creating custom
applications using Office VBA and teaching Office VBA to clients for years. Word
is probably the most used of all of the Microsoft Office applications -- and
also the most under-used. You can add custom functionality to Word using VBA
programs.
If you've ever used Macro Recorder, you're
partially familiar with VBA. Macro recorder creates simple VBA. Macro Recorder
doesn't provide the ability to make decisions, create loops or call custom
functions. For that, you must write your own code in the VB Editor.
The routine below was created entirely in the Word Macro Recorder. Although the routine could use a lot of
enhancements using loops and pattern recognition, the example shows that even
without programming knowledge, you can create useful macros.
Excel Webinars
CPE Link is a new provider of web-based continuing professional education (CPE)
for accounting professionals. Practitioners can choose from approximately ten live
webcasts scheduled per month plus online self-study courses. To view all CPE Link’s
products and services, visit
http://www.cpelink.com |
The routine below searches through a typical
forwarded (and re-forwarded) email and gets rid of extra paragraph marks and
those annoying chevrons. Greater-than signs. Arrow thingies.
Have a look at the code below and if you'd like
to see it work, here's a bad joke from email you can try it on:
CopiedFromEmail.doc.
'---------------------- Start module ------
'I Get It! Development provides programming examples
'for illustration only, without warranty either
'expressed or implied, including, but not limited to,
'the implied warranties of merchantability and/or
'fitness for a particular purpose. This article
'assumes that you are familiar with the programming
'language being demonstrated and the tools used to
'create and debug procedures. These examples assume that
'you have licensed copies of all relevant software installed
'on the machine upon which the examples will be run.
'This routine first replaces all >> signs with ""
(nothing)
'We want to keep double paragraph marks but kill singles
'So we replace double paragraph marks with a special
'meaningless symbol &&
'We then delete all paragraph marks and change the special
'symbol BACK to double paragraph marks
Sub FormatEmail()
'
' FormatEmail Macro
' Macro recorded 6/19/2003 by Theo Callahan
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = ">>"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p^p"
.Replacement.Text = "&&"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^p"
.Replacement.Text = " "
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "&&"
.Replacement.Text = "^p^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub