CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2013
    Posts
    17

    Need some changes in macro

    Hi Guys,
    I got the below code from net which 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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Need some changes in macro

    Show a typical string that you are reading. You'll need to determine IF there are 42 characters, then INSERT a split character, and try it again. Should be easier.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Mar 2013
    Posts
    17

    Re: Need some changes in macro

    For an example only I have mentioned 42 characters. It may me any amount of characters.

  4. #4
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Need some changes in macro

    You say you want to loop until the condition fails. What condition would that be?

    Right now you have two loops one looping on paragraphs and one looping on elements of an array.
    Always use [code][/code] tags when posting code.

  5. #5
    Join Date
    Mar 2013
    Posts
    17

    Re: Need some changes in macro

    I want to loop in between the split string. i.e., Text from 2nd tab to 3rd tab

  6. #6
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Need some changes in macro

    I'm still not sure what condition you are talking about.

    After looking at your code again I can see that the way you are using split() is not good.
    Rather than using split() multiple times in the for next loop you should be doing it once above the loop and storing that in an array then using the array for the stuff in the loop. Much more efficient that way, faster and may help in solving your problem.
    Always use [code][/code] tags when posting 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
  •  





Click Here to Expand Forum to Full Width

Featured