Home Case Studies MS Office Tips and Tricks Sample Code Helpful Videos Custom Development  Custom Training Join Us Contact Us
Search Our Site:  

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.

 



A Word From Our Sponsor

Great Toys For Kids!
Great Toys For Kids!


 

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

 

References

Journal of Accountancy Articles

Tweaking The Numbers

Block That Spreadsheet Error

Excel Security Issues


 

Clients

Do you need help with Microsoft Office? Contact us

See how we've helped companies like yours develop effective business processes

Client Success Stories


 

Consultants

I Get It! Consultants set their own hours, work with their own clients, and choose their own projects.

If you're a Microsoft Office guru and would like to explore being a full-time consultant, please see our consultant information pages.