Results 1 to 7 of 7

Thread: Using VBA to put in HTML tags for bolded, italicized or underlined text

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #6
    Quote Originally Posted by gmaxey View Post
    Something like this perhaps:

    Sub TagAndClearFormating()
    Dim oRng As Word.Range
    Dim arrTagPairs() As String, arrTags() As String
    Dim arrChars() As String, arrEntities() As String
    Dim lngIndex As Long
      arrTagPairs = Split("<i><b>*</i></b>|<i>*</i>|<b>*</b>|<u>*</u>", "|")
      For lngIndex = 0 To UBound(arrTagPairs)
        arrTags = Split(arrTagPairs(lngIndex), "*")
        Set oRng = ActiveDocument.Range
        With oRng.Find
          .ClearFormatting
          If InStr(arrTags(0), "b") > 0 Then .Font.Bold = True
          If InStr(arrTags(0), "i") > 0 Then .Font.Italic = True
          If InStr(arrTags(0), "u") > 0 Then .Font.Underline = True
          While .Execute
            With oRng
              If InStr(arrTags(0), "b") > 0 Then .Font.Bold = False
              If InStr(arrTags(0), "i") > 0 Then .Font.Italic = False
              If InStr(arrTags(0), "u") > 0 Then .Font.Underline = False
              If .Characters.Last = vbCr Then .End = .End - 1
              .Text = arrTags(0) & .Text & arrTags(1)
              .Collapse wdCollapseEnd
            End With
          Wend
        End With
      Next lngIndex
      arrChars = Split("38|34|39|145|146|147|148|162", "|")
      arrEntities = Split("&|"|'|'|'|&|&|¢", "|")
      For lngIndex = 0 To UBound(arrChars)
        Set oRng = ActiveDocument.Range
        With oRng.Find
          .ClearFormatting
          .Text = Chr(arrChars(lngIndex))
          .Replacement.Text = arrEntities(lngIndex)
          .Execute Replace:=wdReplaceAll
        End With
      Next lngIndex
    End Sub

    Greg - I can't thank you enough for doing this. You did a lot more than just point out my problems - you wrote the code to accomplish the task. This macro works fantastically and is exactly what I needed. I just checked out your personal site and see you are a Navy vet - my father-in-law is also a Navy vet and I have 2 nephews presently serving in the Navy (chocks and locks). I made a small donation through your site and I truly appreciate you doing this for me. You are a good egg.
    Last edited by gmaxey; 02-05-2021 at 08:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •