Results 1 to 8 of 8
  1. #1
    New Lounger
    Join Date
    May 2010
    Location
    Chennai
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi Rocks,

    I had a coding which find something and insert a text at starting of the para (Coding at the end of this thread)

    Coding 1. Find the first character of the para, and if it is Numeric its Insert “Tip1:” at the starting of the para.
    Coding 2. Searches for “%” & return and Insert Tip2: at the starting of the para.

    Both the coding works fine. But it works separately.

    Can it possible to do it Jointly i.e., if both the condition matches the Insert to be done (which always appears next to next para)

    Sample text for your ref
    (Text to be insert on the 4th and 5th line, 7th line should remains the same.)

    Archer-Daniels-Midland Co.
    Philip Morris International Inc.
    Reynolds American Inc.
    11,886,882
    AGRICULTURE<\!#209>0.35%

    DaimlerChrysler North America Holding Corp.
    Bank of America Corp. 1.52%
    10,253,514
    Bank of America Corp. Series L (FDIC Guaranteed)



    Coding 1.
    Code:
    Sub Insert Tip1()
    
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.HomeKey Unit:=wdLine
    Selection.MoveRight Unit:=wdCharacter, Count:=1, _
    Extend:=wdExtend
    
    Dim strSelText As String
    strSelText = Selection.Text
    
    If IsNumeric(strSelText) = True Then
    Selection.HomeKey Unit:=wdLine
    Selection.TypeText Text:="Tip1:"
    Else
    
    Selection.MoveDown Unit:=wdLine, Count:=1
    
    End If
    End Sub
    Coding 2.
    Code:
    Sub Insert Tip2()
    With ActiveDocument.Content.Find
    .ClearFormatting
    Do While .Execute(FindText:="%" & Chr(13), Forward:=True, _
    Format:=True) = True
    With .Parent
    .StartOf Unit:=wdParagraph, Extend:=wdMove
    .InsertAfter "Tip2:"
    .Move Unit:=wdParagraph, Count:=1
    End With
    Loop
    End With
    End Sub
    Thanks in advance,
    Rakesh

  2. Subscribe to our Windows Secrets Newsletter - It's Free!

    Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Rakesh View Post
    Hi Rocks,
    Hmmm, I'm not familiar with this greeting. ??

    So you want the end result to be:

    Archer-Daniels-Midland Co.
    Philip Morris International Inc.
    Reynolds American Inc.
    Tip1:11,886,882
    Tip2:AGRICULTURE<\!#209>0.35%
    DaimlerChrysler North America Holding Corp.
    Bank of America Corp. 1.52%
    10,253,514
    Bank of America Corp. Series L (FDIC Guaranteed)
    Since the second prefix should only be inserted if it is in the paragraph that immediately follows the first, you don't need to use Find and Replace. Instead, you could:

    Code:
    Sub InsertTip1Tip2()
    ' Mark adjacent lines with Tip1/Tip2
    ' Check that first character of this paragraph is a numeral
    With Selection
        ' Collapse selection to an insertion point
        .Collapse wdCollapseStart
        ' Move to beginning of paragraph if not already there
        If .Start > .Paragraphs(1).Range.Start Then
            .MoveUp wdParagraph, 1
        End If
        ' Act if first character is numeric
        If IsNumeric(Left(.Text, 1)) Then
            ' Insert "Tip1:" at the insertion point
            .TypeText "Tip1:"
            ' Move down to the next paragraph
            .MoveDown wdParagraph, 1
            ' Check for a % sign as the last character in the paragraph
            If InStr(1, Right(.Paragraphs(1).Range.Text, 2), "%") Then
                ' Insert "Tip2:" at the insertion point
                .TypeText "Tip2:"
            End If
        End If
        ' Move down to the next paragraph
        .MoveDown wdParagraph, 1
    End With
    End Sub
    Does that work?

  4. #3
    New Lounger
    Join Date
    May 2010
    Location
    Chennai
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for your kind reply.

    Moreover the Code works fine.

    If the 1st condition matches and 2nd condition doesn't match. Then also its inserting text as the below colored text.

    I'm working to fix the problem. If you can please let me know.

    Archer-Daniels-Midland Co.
    Philip Morris International Inc.
    Reynolds American Inc.
    Tip1:11,886,882
    Tip2:AGRICULTURE<\!#209>0.35%
    DaimlerChrysler North America Holding Corp.
    Bank of America Corp. 1.52%
    Tip1:10,253,514
    Bank of America Corp. Series L (FDIC Guaranteed)

    Thanks,
    Rakesh

  5. #4
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Rakesh View Post
    If the 1st condition matches and 2nd condition doesn't match. Then also its inserting text as the below colored text.
    Okay, so you want either both inserts or none at all. How about:

    Code:
    Sub InsertTip1Tip2()
    ' Mark adjacent lines with Tip1/Tip2
    Dim rngTemp As Word.Range   ' Used to store location for Tip1
    ' Check that first character of this paragraph is a numeral
    With Selection
        ' Collapse selection to an insertion point
        .Collapse wdCollapseStart
        ' Move to beginning of paragraph if not already there
        If .Start > .Paragraphs(1).Range.Start Then
            .MoveUp wdParagraph, 1
        End If
        ' Act if first character is numeric
        If IsNumeric(Left(.Text, 1)) Then
            ' Store location
            Set rngTemp = Selection.Range
            ' Move down to the next paragraph
            .MoveDown wdParagraph, 1
            ' Check for a % sign as the last character in the paragraph
            If InStr(1, Right(.Paragraphs(1).Range.Text, 2), "%") Then
                ' Insert "Tip1:" at previously marked location
                rngTemp.InsertBefore "Tip1:"
                ' Insert "Tip2:" at the insertion point
                .TypeText "Tip2:"
            End If
            If Not (rngTemp Is Nothing) Then Set rngTemp = Nothing
        End If
        ' Move down to the next paragraph
        .MoveDown wdParagraph, 1
    End With
    End Sub

  6. #5
    New Lounger
    Join Date
    May 2010
    Location
    Chennai
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Its Works fine. Thank you so much for your kind work.

    One final Question!!

    How to workout this for the whole Document.

    Charmfully
    Rakesh

  7. #6
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Rakesh View Post
    One final Question!!

    How to workout this for the whole Document.
    You need to start at the beginning and just run the procedure over and over again until you get to the last paragraph. Rather than change the working code, you can call it with this procedure (placed in the same module):

    Code:
    Sub InsertTipsGlobal()
    ' Store reference to last paragraph so we can stop
    Dim parLast As Word.Paragraph
    With ActiveDocument.Content
        Set parLast = .Paragraphs(.Paragraphs.Count)
    End With
    With Selection
        ' Move insertion point to the top of the document
        .HomeKey wdStory
        ' Loop through the document until the end
        While .Start < parLast.Range.Start
            ' Call procedure to check/insert (Note: the called
            ' procedure moves to the next paragraph automatically)
            InsertTip1Tip2
        Wend
    End With
    ' Clean up
    If Not (parLast Is Nothing) Then Set parLast = Nothing
    End Sub

  8. #7
    New Lounger
    Join Date
    May 2010
    Location
    Chennai
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Once again thanks for your reply and tips.
    The Coding works fine but it skips sometimes.
    (Its skips when the first character of the 2 lines is numeric, like the below bold text).

    How to rectify it.


    Archer-Daniels-Midland Co.
    Philip Morris International Inc.
    Reynolds American Inc.
    11,886,882
    AGRICULTURE<\!#209>0.35%

    DaimlerChrysler North America Holding Corp.
    1.5% Bank of America Corp. 1.52
    10,253,514
    Bank of America Corp. Series L%

    Reynolds American Inc.

    Thanks,
    Rakesh

  9. #8
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    Quote Originally Posted by Rakesh View Post
    The Coding works fine but it skips sometimes.
    (Its skips when the first character of the 2 lines is numeric, like the below bold text).
    I didn't think of that. Try this instead:

    Code:
    Sub InsertTip1Tip2()
    ' Mark adjacent lines with Tip1/Tip2
    Dim rngTemp As Word.Range   ' Used to store paragraph for possible Tip2
    ' Check that first character of this paragraph is a numeral
    With Selection
        ' Collapse selection to an insertion point
        .Collapse wdCollapseStart
        ' Move to beginning of paragraph if not already there
        If .Start > .Paragraphs(1).Range.Start Then
            .MoveUp wdParagraph, 1
        End If
        ' Act if first character is numeric
        If IsNumeric(Left(.Text, 1)) Then
            ' Set range to beginning of next paragraph
            Set rngTemp = Selection.Next(wdParagraph, 1)
            ' Check for a % sign as the last character in the next paragraph
            If InStr(1, Right(rngTemp.Text, 2), "%") Then
                ' Insert "Tip1:" at the insertion point
                Selection.TypeText "Tip1:"
                ' Move down to the next paragraph
                .MoveDown wdParagraph, 1
                ' Insert "Tip2:" at the insertion point
                .TypeText "Tip2:"
            End If
            If Not (rngTemp Is Nothing) Then Set rngTemp = Nothing
        End If
        ' Move down to the next paragraph
        .MoveDown wdParagraph, 1
    End With
    End Sub

Posting Permissions

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