PDA

View Full Version : [SOLVED:] Formatting my Word Document as an HTML



stoicw
03-27-2018, 11:12 AM
Hi,

I am trying to format my word document to include HTML "tags". In the context of word, it is useless, but I will be copying this text and posting it in a HTML Text box.

I have 2 separate macros. and I wan't to add a third function.

But I also want to combine all three into one macro so that I don't have to run all three, I can just run one.

The function I don't have that I want to add, is the addition of <p> at the start of each paragraph, and have </p> at the end.

And I'd also like to know how I can combine these 3 functions into one macro.

HTML Tags:

Sub HTMLTag()'
' HTMLTag Macro


Selection.Find.Style = ActiveDocument.Styles("Emphasis")
With Selection.Find
.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.ClearFormatting
.Font.Bold = True
With .Replacement
.ClearFormatting
.Text = "<b>^&</b>"
.Font.Bold = False
End With
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Font.Italic = True
With .Replacement
.ClearFormatting
.Text = "<i>^&</i>"
.Font.Italic = False
End With
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Font.Underline = True
With .Replacement
.ClearFormatting
.Text = "<u>^&</u>"
.Font.Underline = False
End With
.Execute Replace:=wdReplaceAll
End With

End Sub


HTML Breaks:


Sub HTMLBreak()
'
' HTMLBreak Macro

Selection.Find.Style = ActiveDocument.Styles("Emphasis")
With Selection.Find
.Text = "***"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = True
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
.ClearFormatting
.Font.Bold = False
With .Replacement
.ClearFormatting
.Text = "<hr/>"
.Font.Bold = False
End With
.Execute Replace:=wdReplaceAll
End With

End Sub


Thank you so much!

macropod
03-27-2018, 01:58 PM
Try:

Sub ApplyHTMLTags()
With ActiveDocument.Range
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Text = ""
.Replacement.Text = "<b>^&</b>"
.Forward = True
.Format = True
.Font.Bold = True
.Replacement.Font.Bold = False
.Wrap = wdFindContinue
.MatchWildcards = False
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Replacement.Text = "<i>^&</i>"
.Font.Italic = True
.Replacement.Font.Italic = False
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Font.Underline = True
.Replacement.Text = "<u>^&</u>"
.Replacement.Font.Underline = False
.Execute Replace:=wdReplaceAll
.ClearFormatting
.Text = "***"
.Replacement.Text = "<hr/>"
.Execute Replace:=wdReplaceAll
.Text = "^p"
.Replacement.Text = "</p>^p<p>"
.Execute Replace:=wdReplaceAll
End With
.InsertBefore "<p>"
.Paragraphs.Last.Range.Delete
End With
End Sub