locked
Bold, Italic Tag Help!! RRS feed

  • Question

  • Hi Guys,
    I got this below macro by googling, which is useful for my work around. It is used to insert tags in MS Word.
    Its works fine. It’s insert tags for bold, italic and underlined text.
    What’s the problem is, when a bold text, or Italic text or underlined text continued to next para it insert the tag (red text) as follows.

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over <b>the lazy dog.

    The quick brown fox jumps over the lazy dog.
    </b>

    But What I need is, If the bold text flow to the next para. It should be break by para to para as follows:

    The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog. The quick brown fox jumps over <b>the lazy dog.</b>

    <b>The quick brown fox jumps over the lazy dog.
    </b>

    Macro as follows:

    Sub Demo()
    Application.ScreenUpdating = False
    With ActiveDocument.Range
      With .Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .Text = ""
        .Font.Bold = True
        .Replacement.Text = "<b>^&</b>"
        .Replacement.Font.Bold = False
        .execute replace:=wdReplaceAll
        .ClearFormatting
        .Font.Italic = True
        .Replacement.Text = "<i>^&</i>"
        .Replacement.Font.Italic = False
        .execute replace:=wdReplaceAll
        .ClearFormatting
        .Font.Underline = True
        .Replacement.Text = "<u>^&</u>"
        .Replacement.Font.Underline = False
        .execute replace:=wdReplaceAll
      End With
    End With
    Application.ScreenUpdating = True
    End Sub

    Is there a way to do this.

    Thanks in advance

    • Edited by Mathewss Saturday, October 18, 2014 10:53 AM
    Saturday, October 18, 2014 10:52 AM

Answers

  • If you are only interested in full paragraphs then you could use

    Dim oPara As Paragraph
    Dim oRng As Range
        For Each oPara In ActiveDocument.Range.Paragraphs
            If oPara.Range.Font.Bold = True Then
                Set oRng = oPara.Range
                oRng.End = oRng.End - 1
                oRng.InsertBefore "<b>"
                oRng.InsertAfter "</b>"
                oRng.Font.Bold = False
            End If
            If oPara.Range.Font.Italic = True Then
                Set oRng = oPara.Range
                oRng.End = oRng.End - 1
                oRng.InsertBefore "<i>"
                oRng.InsertAfter "</i>"
                oRng.Font.Italic = False
            End If
            If oPara.Range.Font.Underline = wdUnderlineSingle Then
                Set oRng = oPara.Range
                oRng.End = oRng.End - 1
                oRng.InsertBefore "<u>"
                oRng.InsertAfter "</u>"
                oRng.Font.Underline = wdUnderlineNone
            End If
        Next oPara


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by danishani Monday, October 20, 2014 5:03 PM
    Saturday, October 18, 2014 2:36 PM
  • Hi Graham Mayor,

    Thanks for your kind reply. I am not "only interested in full paragraph". I need to apply tags wherever the bold, italic and underlined text occurs.

    By combining your macro and the macro which I got, fulfill my need.

    Once again thanks for you.

    • Marked as answer by Mathewss Monday, October 20, 2014 2:59 PM
    Monday, October 20, 2014 10:41 AM

All replies

  • If you are only interested in full paragraphs then you could use

    Dim oPara As Paragraph
    Dim oRng As Range
        For Each oPara In ActiveDocument.Range.Paragraphs
            If oPara.Range.Font.Bold = True Then
                Set oRng = oPara.Range
                oRng.End = oRng.End - 1
                oRng.InsertBefore "<b>"
                oRng.InsertAfter "</b>"
                oRng.Font.Bold = False
            End If
            If oPara.Range.Font.Italic = True Then
                Set oRng = oPara.Range
                oRng.End = oRng.End - 1
                oRng.InsertBefore "<i>"
                oRng.InsertAfter "</i>"
                oRng.Font.Italic = False
            End If
            If oPara.Range.Font.Underline = wdUnderlineSingle Then
                Set oRng = oPara.Range
                oRng.End = oRng.End - 1
                oRng.InsertBefore "<u>"
                oRng.InsertAfter "</u>"
                oRng.Font.Underline = wdUnderlineNone
            End If
        Next oPara


    Graham Mayor - Word MVP
    www.gmayor.com

    • Marked as answer by danishani Monday, October 20, 2014 5:03 PM
    Saturday, October 18, 2014 2:36 PM
  • Hi Graham Mayor,

    Thanks for your kind reply. I am not "only interested in full paragraph". I need to apply tags wherever the bold, italic and underlined text occurs.

    By combining your macro and the macro which I got, fulfill my need.

    Once again thanks for you.

    • Marked as answer by Mathewss Monday, October 20, 2014 2:59 PM
    Monday, October 20, 2014 10:41 AM