How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string
How to Loop until condition fails in split string How to Loop until condition fails in split string
How to Loop until condition fails in split string
Go Back  Xtreme Visual Basic Talk > > > > How to Loop until condition fails in split string


Reply
 
Thread Tools Display Modes
  #1  
Old 11-29-2014, 04:01 PM
star123 star123 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default How to Loop until condition fails in split string


Hi,
The below code works fine.
The macro works as follows.
1. Split the para from the 2nd Tab to 3rd Tab.
2. Move the cursor to the 10th character.
3. Find the space reversely and Inserts "~~~"

But it do the process only once. How to loop until the condition fails.

For example:
If the split string contains 42 characters it should loop for 4 times. And then to next para....

Code:
Sub Demo()
    
    Dim Para As Paragraph, Rng As Range, i As Long
    Dim StrOut As String, StrTmp As String
    With ActiveDocument
        For Each Para In .Paragraphs
   If UBound(Split(Para.Range.Text, vbTab)) > 1 Then
            If Len(Split(Para.Range.Text, vbTab)(2)) > 10 Then
                StrOut = ""
                Set Rng = Para.Range
                Rng.End = Rng.End - 1
                 For i = 0 To UBound(Split(Rng, vbTab))
                    If i <> 2 Then
                        StrOut = StrOut & Split(Rng, vbTab)(i) & vbTab
                    Else
                        StrTmp = Left(Split(Rng, vbTab)(i), 10)
                        StrTmp = Left(StrTmp, InStrRev(StrTmp, " ") - 1) & "~~~"
                        StrTmp = StrTmp & Mid(Split(Rng, vbTab)(i), Len(StrTmp) - 1,  Len(Split(Rng, vbTab)(i)))
                        StrOut = StrOut & StrTmp & vbTab
                    End If
                 Next
                Rng.Text = Left(StrOut, Len(StrOut) - 1)
             
             End If
       End If
         Next
      End With
End Sub
Thanks in advance
Reply With Quote
  #2  
Old 12-01-2014, 11:14 PM
cacody cacody is offline
Junior Contributor
 
Join Date: Apr 2011
Location: Scottsdale AZ
Posts: 323
Thumbs up

Try a Do Loop, something like:

Code:
       For Each Para In .Paragraphs
       If Not Para Is Nothing Then
       FirstAddress = Para.Address
       Do
       If UBound(Split(Para.Range.Text, vbTab)) > 1 Then
       .........
       .........
       End If
       Loop Until Para Is Nothing Or FirstAddress = Para.Address
       End If
         Next
      End With
End Sub
Of course this is untested.
Reply With Quote
  #3  
Old 12-02-2014, 01:38 PM
star123 star123 is offline
Newcomer
 
Join Date: Mar 2012
Posts: 15
Default

Its shows the compile error in below line

Code:
FirstAddress = Para.Address
Do I composed correctly???

Code:
Sub demo()

Dim Para As Paragraph, Rng As Range, i As Long
    Dim StrOut As String, StrTmp As String
    With ActiveDocument

        For Each Para In .Paragraph
        If Not Para Is Nothing Then
              FirstAddress = Para.Address
              Do
              If UBound(Split(Para.Range.Text, vbTab)) > 1 Then
                    If Len(Split(Para.Range.Text, vbTab)(2)) > 10 Then
                            StrOut = ""
                            Set Rng = Para.Range
                            Rng.End = Rng.End - 1
                             For i = 0 To UBound(Split(Rng, vbTab))
                                If i <> 2 Then
                                    StrOut = StrOut & Split(Rng, vbTab)(i) & vbTab
                                Else
                                    StrTmp = Left(Split(Rng, vbTab)(i), 10)
                                    StrTmp = Left(StrTmp, InStrRev(StrTmp, " ") - 1) & "~~~"
                                    StrTmp = StrTmp & Mid(Split(Rng, vbTab)(i), Len(StrTmp) - 1, Len(Split(Rng, vbTab)(i)))
                                    StrOut = StrOut & StrTmp & vbTab
                                End If
                             Next
                            Rng.Text = Left(StrOut, Len(StrOut) - 1)
                    End If
                   Loop Until Para Is Nothing Or FirstAddress = Para.Address
            End If
        Next
    End With
End Sub
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string How to Loop until condition fails in split string
How to Loop until condition fails in split string
How to Loop until condition fails in split string
 
How to Loop until condition fails in split string
How to Loop until condition fails in split string
 
-->