Consulting

Results 1 to 3 of 3

Thread: Delete paragraph mark from end of Range

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Regular
    Joined
    Jan 2011
    Posts
    82
    Location

    Delete paragraph mark from end of Range

    This is rather trivial compared to other posts here which concern the more important real world of business.
    I need help finding a way to delete the paragraph mark at the end of a range of found text.

    I have developed a method of saving Word(2003) doc files as HTML without the clutter of additional, unneeded style information from Word even when filtered.

    Briefly, I add all the HTML tags in the document window, save as Text then manually change the file extension to HTML. I have the project working but encountered a problem which I was only able to correct by using Find/Replace code afterwards (hiding my ineptitude). I'm certain there is a way to do a cleaner job. After a week of frustrating effort (read getting up at 2:00am because it's on my mind and I can't sleep) I am turning to you for help.

    Here's an example of the problem:
    I want to add tags to text which is bold as follows:

    Some text which is bold - becomes <span class ="bold">Some text which is bold</span>

    The doc's bold style can be a font style or a character style. Here's my code for font style, but the same thing happens when dealing with character style for which I had a slightly different procedure.

    Sub StyleBold()
        With ActiveDocument.Range.Find
            .ClearFormatting
            .Font.Bold = True
            .Text = ""
            .MatchWildcards = True
            .Replacement.ClearFormatting
            .Replacement.Text = "<span class=""bold"">^&</span>"
            .Forward = True
            .Execute Replace:=wdReplaceAll
        End With
    End Sub
    If the bold style occurs at the end of a paragraph, I only want to select the text, not the paragraph mark at the end; otherwise, the HTML tags enclose the paragraph mark.

    The result:
    original -
    Here is some normal text. Here is some bold text.
    Another paragraph...

    afterwards -
    Here is some normal text. <span class="bold">Here is some bold text.
    </span>
    Another paragraph...

    what I want -
    Here is some normal text. <span class="bold">Here is some bold text.</span>
    Another paragraph...

    The importance is that the next stage of tagging involves surrounding the paragraphs with <p>...</p>
    and the (terrible) result becomes -
    <p>Here is some normal text. <span class="bold">Here is some bold text.</p>
    <p></span>
    Another paragraph...</p>

    Similarly if the second paragraph starts with bold type I get -
    <p>Here is some normal text. <span class="bold">Here is some bold text.</p>
    <p>Start of next paragraph in bold</span>
    Another paragraph...</p>

    which should be -
    <p>Here is some normal text. <span class="bold">Here is some bold text.</span></p>
    <p><span class="bold">Start of next paragraph in bold</span>
    Another paragraph...</p>

    I am able to correct this using F/R but it's clunky!

    I suspect that Range.MoveEnd Unit:=wdCharacter, Count:=-1 should be used to reduce the range and remove the paragraph mark from the range, but for the life in me I can't figure out where to put this code.

    I thought the following may work - but not so.

    Sub removePara()
        Dim aRange As Range
        Set aRange = ActiveDocument.Range(Start:=0, End:=0)
        With aRange.Find
            .ClearFormatting
            .Text = ""
            .Font.Bold = True
            .Execute FindText:="", Format:=True, Forward:=True
            If .Found Then
                aRange.MoveEnd Unit:=wdCharacter, Count:=-1
            End If
            With .Replacement
                .ClearFormatting
                .Text = "<span class=""bold"">^&</span>"
            End With
            .Execute Replace:=wdReplaceAll, Format:=True
        End With
    End Sub
    It looks like I can't change arange in the middle of a Find operation.
    Is the solution along lines of:
    aRange.Text = Left(aRange.Text, Len(aRange.Text) - 1)
    but again, how to incorporate this?
    Last edited by Aussiebear; 03-23-2025 at 08:40 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
  •