Consulting

Results 1 to 6 of 6

Thread: Hypen (-) is not recognized as paragraph begin

  1. #1
    VBAX Regular
    Joined
    Jan 2018
    Posts
    45
    Location

    Hypen (-) is not recognized as paragraph begin

    Hi, a following code not working when at begin of paragraph occurs hypen, how to solve this.
    File to testing: insertafter.docx


    Sub add_before_xml()
              
        Dim bRng As Range
        Set bRng = ActiveDocument.Range
        With bRng.Find
            
                    
                     With .Font
                        .Size = 8
                        .Underline = wdUnderlineSingle
                    End With
              
            Do While .Execute(MatchWholeWord:=True)
           
                    
             bRng.End = bRng.Paragraphs(1).Range.End - 1
                 With bRng
                    .InsertBefore "<tr><td>"
                    .InsertAfter "</td></tr>"
                    .Collapse 0
                End With
             
            Loop
        End With
    lblb_Exit:
        Set bRng = Nothing
    
    End Sub
    Karol

  2. #2
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,888
    Location
    It seems to work here. You are going to have to do a better job at providing and example. What specifically isn't working. Show us a small sample of text before and after your process. Showing what you start with, what you get, and what you expect to get.
    Greg

    Visit my website: http://gregmaxey.com

  3. #3
    VBAX Regular
    Joined
    Jan 2018
    Posts
    45
    Location
    ok,
    text before:
    1. San-Pajda 7 21 21:2
    2. PZPS Szczyrk 7 16 18:7
    3. Tomasovia 7 16 18:8
    8. Marba 7 4 6:19
    - - - -
    9. Pogon 7 4 7:20
    - - -

    Text after process:

    <tr><td>1. San-Pajda 7 21 21:2</td></tr>
    <tr><td>2. PZPS Szczyrk 7 16 18:7</td></tr>
    <tr><td>3. Tomasovia 7 16 18:8</td></tr>
    <tr><td>8. Marba 7 4 6:19</td></tr>
    - - - -
    <tr><td>9. Pogon 7 4 7:20</td></tr>
    - - -

    My expectation:

    <tr><td>1. San-Pajda 7 21 21:2</td></tr>
    <tr><td>2. PZPS Szczyrk 7 16 18:7</td></tr>
    <tr><td>3. Tomasovia 7 16 18:8</td></tr>
    <tr><td>8. Marba 7 4 6:19</td></tr>
    <tr><td>- - - -<tr><td>
    <tr><td>9. Pogon 7 4 7:20</td></tr>
    <tr><td>- - - </tr></td>
    Karol

  4. #4
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,888
    Location
    Well, I'm sorry, but your code as posted does not produce what you show as "Text after process" (at least not here). What are really trying to do? Since everything in your example text is underlined, why are bothering to find underline?
    Greg

    Visit my website: http://gregmaxey.com

  5. #5
    VBAX Regular
    Joined
    Jan 2018
    Posts
    45
    Location
    Quote Originally Posted by gmaxey View Post
    Well, I'm sorry, but your code as posted does not produce what you show as "Text after process" (at least not here). What are really trying to do? Since everything in your example text is underlined, why are bothering to find underline?
    I don't know what is wrong but code doesn't insert before and doesn't insert after any desired text, where paragraph start with hypen, i've check it on two different computers. How I should prove it for You?
    I've added underline because my document has much more different types format of text.
    Karol

  6. #6
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    2,888
    Location
    Try:

    Sub add_before_xml()
    Dim bRng As Range
    Dim oPar As Paragraph
      Set bRng = ActiveDocument.Range
      With bRng.Find
        With .Font
          .Size = 8
          .Underline = wdUnderlineSingle
        End With
        Do While .Execute(MatchWholeWord:=True)
          bRng.End = bRng.Paragraphs(1).Range.End - 1
          With bRng
            .Select
            .InsertBefore "<tr><td>"
            .InsertAfter "</td></tr>"
            .Collapse 0
          End With
        Loop
      End With
      For Each oPar In ActiveDocument.Range.Paragraphs
        If oPar.Range.Characters.First = "-" And Not InStr(oPar.Range.Text, "<tr>") > 0 Then
          oPar.Range.InsertBefore "<tr><td>"
          oPar.Range.InsertAfter "</tr></td>"
        End If
      Next
    lblb_Exit:
      Set bRng = Nothing
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

Posting Permissions

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