Sub toHTML()
' A simple Word macro originally by Greg Maxey
' Replaces italics, bold and underline text with HTML tags. <em> is used instead of <i>
' Modified to also convert these to HTML encoding:
' Em dashes to —
' En dashes to –
' Centered text with <center> </center> (Yes I know it should be <p align="center"> nowadays)
' Line breaks to <br> (for song lyrics etc.)
' Based on https://answers.microsoft.com/en-us/msoffice/forum/all/need-a-vba-script-to-convert-word-text-formatting/e6445b50-7149-48f0-93ea-6d0691b2b600
Dim oRng As Word.Range
' Boldface:
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.MatchWildcards = True
.Format = True
.Font.Bold = True
While .Execute
With oRng
.InsertBefore "<b>"
.InsertAfter "</b>"
.Font.Bold = False
.Collapse wdCollapseEnd
End With
Wend
End With
' Underline:
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.MatchWildcards = True
.Format = True
.Font.Underline = True
While .Execute
With oRng
.InsertBefore "<u>"
.InsertAfter "</u>"
.Font.Underline = False
.Collapse wdCollapseEnd
End With
Wend
End With
' Italics:
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.MatchWildcards = True
.Format = True
.Font.Italic = True
While .Execute
With oRng
.InsertBefore "<em>"
.InsertAfter "</em>"
.Font.Italic = False
.Collapse wdCollapseEnd
End With
Wend
End With
' Centered paragraphs:
Dim para As Paragraph
For Each para In ActiveDocument.Paragraphs
If para.Alignment = wdAlignParagraphCenter Then
para.Range.Text = "<center>" & Left(para.Range.Text, Len(para.Range.Text) - 1) & "</center>" & Chr(13)
End If
Next para
' Em dash:
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "^+"
.Replacement.ClearFormatting
.Replacement.Text = "—"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
' En dash:
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "^="
.Replacement.ClearFormatting
.Replacement.Text = "–"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
' Line breaks (for song lyrics and similar formatting):
Set oRng = ActiveDocument.Range
With oRng.Find
.ClearFormatting
.Text = "^l"
.Replacement.ClearFormatting
.Replacement.Text = "<br>"
.Execute Replace:=wdReplaceAll, Forward:=True, _
Wrap:=wdFindContinue
End With
MsgBox ("Conversion to HTML done.")
End Sub