Ask a questionAsk a question
 

AnswerRequesting assistance with a Macro

  • Thursday, October 29, 2009 5:10 PMjp91306 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I have only limited experience with macros, but I am a long time Office user, and computer user in general.

    I use MS Word 2007 on an XP Laptop, and on a Vista 64 desktop.

    I need a simple macro to search through a computer "log" (simple text file). I want to remove multiple instances of "trash" (un-needed lines, that repeat throughout the log). Each line ends with a proper cr/lf. Standard "notepad" type text file.

    I can use a "set" of macros to search for each type of line..or one with an array to do the whole thing at once...either will suffice.

    My approach was: Got the top..search for word in a line...select the line...delete the line....search for the next instance...stop when you get to the end.

    It sounded simple. 

    I can't do it.

    Any help would be appreciated.

Answers

  • Saturday, October 31, 2009 10:11 PMJeff - www.SRSoft.usMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    jp,

    try this

    Sub DeletLineIfTextFound()
    Dim cntback As Integer: cntback = ActiveDocument.Paragraphs.Count
    For i = 1 To cntback
        Dim s As String: s = ActiveDocument.Paragraphs.Item(cntback).Range.Text
        Dim locInString As Integer: locInString = InStr(1, s, "test", vbTextCompare)
        If locInString > 0 Then
            ActiveDocument.Paragraphs.Item(cntback).Range.Delete
        End If
        cntback = cntback - 1
    Next
    End Sub


    it loops through the lines and looks for the word "test" using the instr function.  if it finds the word then it will delete the line.  it counts backwards and deletes from the bottom up because if you delete a line, say line 1, then line 2 will become line 1.  as lines are deleted the number of lines left changes so it throws off your count for the loop.  basically you start out looping through the original number or lines before any deletes and then end up looping through lines that don't exist anymore.  if say you start out with 5 lines and delete lines 1 to 3, you will have 2 more lines counted that will continue to loop and will not be found.

    hope all that makes sense.
    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial

All Replies

  • Saturday, October 31, 2009 10:11 PMJeff - www.SRSoft.usMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    jp,

    try this

    Sub DeletLineIfTextFound()
    Dim cntback As Integer: cntback = ActiveDocument.Paragraphs.Count
    For i = 1 To cntback
        Dim s As String: s = ActiveDocument.Paragraphs.Item(cntback).Range.Text
        Dim locInString As Integer: locInString = InStr(1, s, "test", vbTextCompare)
        If locInString > 0 Then
            ActiveDocument.Paragraphs.Item(cntback).Range.Delete
        End If
        cntback = cntback - 1
    Next
    End Sub


    it loops through the lines and looks for the word "test" using the instr function.  if it finds the word then it will delete the line.  it counts backwards and deletes from the bottom up because if you delete a line, say line 1, then line 2 will become line 1.  as lines are deleted the number of lines left changes so it throws off your count for the loop.  basically you start out looping through the original number or lines before any deletes and then end up looping through lines that don't exist anymore.  if say you start out with 5 lines and delete lines 1 to 3, you will have 2 more lines counted that will continue to loop and will not be found.

    hope all that makes sense.
    FREE DEVELOPER TOOLS, CODE & PROJECTS at www.srsoft.us Database Code Generator and Tutorial