Consulting

Results 1 to 4 of 4

Thread: Add tags to Bold text only at start of selected paragraphs

  1. #1
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location

    Add tags to Bold text only at start of selected paragraphs

    I want to make a macro that will be selective in adding some <link> tags fore and aft of text only at the start of paragraphs and limited to a certain font size and color. A copy of the text is placed at the head.... easier to show as below. Word has a problem with spaces it seems.


    Some sort of heading in bold with spaces followed by some more text in a plain font, maybe a different size but not necessarily.


    I want to be able to select a range of paragraphs and tag just the bold text and place a copy before the found text.

    <link>Some sort of heading in bold with spaces</link>Some sort of heading in bold with spacesfollowed by some more text in a plain font, maybe a different size but not necessarily

  2. #2
    Simple enough
    Sub Macro1()
    Dim orng As Range
        Set orng = ActiveDocument.Range
        With orng.Find
            .ClearFormatting
            .Replacement.ClearFormatting
            .Text = ""
            .Replacement.Text = ""
            .Font.Bold = True
            Do While .Execute()
                If orng.Start = orng.Paragraphs(1).Range.Start Then
                    orng.InsertBefore "<link>"
                    orng.InsertAfter "</link>"
                End If
                orng.Collapse 0
            Loop
        End With
    lbl_Exit:
        Set orng = Nothing
        Exit Sub
    End Sub
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    Thanks but not what I stated, but I really appreciate the help.
    1. Selected paragraphs only
    2. Copy of bold text with tags is to be placed in front, so
    Some sort of heading in bold with spaces ​followed by some more text in a plain font, maybe a different size but not necessarily.
    to become

    <link>Some sort of heading in bold with spaces</link>Some sort of heading in bold with spaces ​followed by some more text in a plain font, maybe a different size but not necessarily.

  4. #4
    VBAX Regular
    Joined
    Mar 2020
    Posts
    79
    Location
    This appears to do what I need.
    I wanted also to limit by font size and color.
    Thanks for pointing me in the right direction.


      
    InSelection = False
    If selection.Type = wdSelectionIP Then InSelection = True
    If InSelection = True Then
        MsgBox ("select some text")
        Exit Sub
    End If
    Application.ScreenUpdating = False
    Dim RngFnd As Range, StrTxt As String
    With selection
        Set RngFnd = .Range
        With .Range
            With .Find
                .ClearFormatting
                .Replacement.ClearFormatting
                .text = ""
                .Replacement.text = ""
                .Forward = True
                .Format = False
                .Wrap = wdFindStop
                .MatchWildcards = True
                .Font.Bold = True
                .Font.ColorIndex = wdAuto
                .Font.Size = 10
                .Execute
            End With
            Do While .Find.Found
                If .InRange(RngFnd) Then
                    If .Paragraphs.Count > 1 Then .Start = .Paragraphs(1).Range.End
                    If .Start = .Paragraphs(1).Range.Start Then
                        StrTxt = .text
                        .InsertBefore "<link>" & StrTxt & "</link>"
                        .Start = .End - Len(StrTxt)
                    End If
                End If
                .Collapse wdCollapseEnd
                .Find.Execute
            Loop
        End With
    End With
    RngFnd.Select

    Finally I think I worked it out. Will add some comboboxes for setting font and color to parse and should be good to go.

    Thanks for your examples, and time.
    Last edited by JPG; 03-16-2020 at 10:21 AM. Reason: Fixed the code

Posting Permissions

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