locked
Delete Duplicate Strings RRS feed

  • Question

  • I am working in Word 2010 / vba, I am not a programmer.  I get a crude report from a computerized measuring system and want to clean it up in word.  The report is a RTF file that has a format of:  Graphic image -- A line of text -- A table -- The same line of text repeated.   For whatever reason the text that is before the table is repeated following the table for each table included in the output.   I would like to have a bit of code that would go down through the rtf file, find the second incident of each repeated text and delete it, leaving the first instance in place.  Any help would be appreciated.
    Monday, January 16, 2012 8:46 PM

Answers

  • Hi Scott

    Here is a hint

    Sub Select_Tble()
    
    Dim oTable As Table
    Dim sBeforeText
    
     For i = 1 To ActiveDocument.Tables.Count
        Set oTable = ActiveDocument.Tables(1)
        oTable.Select
        Selection.MoveUp
        sBeforeText = Selection.Paragraphs(1).Range.Text
        oTable.Select
        Selection.MoveDown
        If InStr(1, Selection.Paragraphs(1).Range.Text, sBeforeText) Then
            Selection.Paragraphs(1).Range.Delete
        End If
     
     Next i
     
    
    End Sub
    
    

     

    Cheers

    Shasur


    http://www.vbadud.blogspot.com http://www.dotnetdud.blogspot.com
    Wednesday, February 1, 2012 6:51 AM
  • Thanks a bunch Shasur it worked almost perfectly, I just had to change one letter from
        Set oTable = ActiveDocument.Tables(1)
    to

    Set oTable = ActiveDocument.Tables(i)

    Again Thank You


    Scott
    • Marked as answer by danishani Friday, February 3, 2012 12:27 AM
    Thursday, February 2, 2012 9:51 PM

All replies

  • I am working in Word 2010 / vba, I am not a programmer.  I get a crude report from a computerized measuring system and want to clean it up in word.  The report is a RTF file that has a format of:  Graphic image -- A line of text -- A table -- The same line of text repeated.   For whatever reason the text that is before the table is repeated following the table for each table included in the output.   I would like to have a bit of code that would go down through the rtf file, find the second incident of each repeated text and delete it, leaving the first instance in place.  Any help would be appreciated.


    Hi

    I would suggest to loop through tables and find the paragraph/line above and search for the same immediately after the table and then delete it.

    Looping through all paragraphs in a document and deleting the duplicate can be risky as there might be genuine sentences that are duplicated in the document

    Cheers

    Shasur

     


    http://www.vbadud.blogspot.com http://www.dotnetdud.blogspot.com
    Tuesday, January 17, 2012 7:16 AM
  • Hi Shasur,

    That sounds like just what I want to do.  However, my VBA experince is minimal at best.  I do not know the vba commands to make that happen.  That is to say; I don't know how to select a table and then do somthing to a string outside of the selected table.  ...


    Scott
    Tuesday, January 17, 2012 3:43 PM
  • Hi Scott

    Here is a hint

    Sub Select_Tble()
    
    Dim oTable As Table
    Dim sBeforeText
    
     For i = 1 To ActiveDocument.Tables.Count
        Set oTable = ActiveDocument.Tables(1)
        oTable.Select
        Selection.MoveUp
        sBeforeText = Selection.Paragraphs(1).Range.Text
        oTable.Select
        Selection.MoveDown
        If InStr(1, Selection.Paragraphs(1).Range.Text, sBeforeText) Then
            Selection.Paragraphs(1).Range.Delete
        End If
     
     Next i
     
    
    End Sub
    
    

     

    Cheers

    Shasur


    http://www.vbadud.blogspot.com http://www.dotnetdud.blogspot.com
    Wednesday, February 1, 2012 6:51 AM
  • Thanks a bunch Shasur it worked almost perfectly, I just had to change one letter from
        Set oTable = ActiveDocument.Tables(1)
    to

    Set oTable = ActiveDocument.Tables(i)

    Again Thank You


    Scott
    • Marked as answer by danishani Friday, February 3, 2012 12:27 AM
    Thursday, February 2, 2012 9:51 PM