PDA

View Full Version : VB script or macro to italicize words



charliew001
09-06-2012, 09:02 PM
Hi all,

I have a list of words that need to be italicized in all word documents. Normally, I would copy and paste the below macro to include each word, which in this case are all bacteria. But in an attempt to learn learn more VB, I was hoping to get some help on creating a script that find words that I specified in a word document, or rather just not coded into the script, and italicize the words in the active document which match the words in the aforementioned list.

Thanks


Sub Italicize1()
'
' Italicize1 Macro
'
'
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
Selection.Find.Replacement.Font.Italic = True
With Selection.Find
.Text = "S. pneumoniae"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub

macropod
09-06-2012, 11:21 PM
Try the following. To use it, create a reference document holding the strings you want to italicze, one expression per paragraph, then modify "Drive:\FilePath\RefList.doc" in the macro to point to that file.
Sub BulkItalics()
Application.ScreenUpdating = False
Dim DocRef As Document, StrList As String, j As Long
'Load the strings from the reference doc into a text string to be used as an array.
Set DocRef = Documents.Open("Drive:\FilePath\RefList.doc")
StrList = DocRef.Range.Text
DocRef.Close False
Set DocRef = Nothing
With ActiveDocument.Range.Find
.ClearFormatting
With .Replacement
.ClearFormatting
.Text = "^&"
.Font.Italic = True
End With
.MatchWholeWord = True
.MatchCase = True
.Format = True
'Process each 'paragraph' from the StrList
For j = 0 To UBound(Split(StrList, vbCr)) - 1
.Text = Split(StrList, vbCr)(j)
.Execute Replace:=wdReplaceAll
Next
End With
Application.ScreenUpdating = True
End Sub
Whilst it is easy enough to code a macro to process its own internal list, that makes the list harder to maintain.

charliew001
09-07-2012, 08:30 AM
Thank you macropod.