MS Word VBA - Delete all lines until criteria met

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • iheartvba
    New Member
    • Apr 2007
    • 171

    MS Word VBA - Delete all lines until criteria met

    Hi,
    Sorry if I am posting in the wrong place, but I couldn't find the forum for MS Word specifically.

    I want to be able to have word search through a document starting from the top of the first page, and delete every line until it reaches a line with a certain criteria.

    Thanks
  • jg007
    Contributor
    • Mar 2008
    • 283

    #2
    there might be some better code out there but as a start maybe this will help

    you will need to check for the end of the page and also go to the start of the page before beginning and I cant remember how to do that also this is very basic and just deletes all the lines till it gets to one that starts end

    see if you can work out some code and post it here if you have problems

    Code:
    Do
    
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    If UCase(Left$(Selection.Text, 3)) = "END" Then Exit Do
    Selection.Delete
    
    Loop

    Comment

    • iheartvba
      New Member
      • Apr 2007
      • 171

      #3
      Originally posted by jg007
      there might be some better code out there but as a start maybe this will help

      you will need to check for the end of the page and also go to the start of the page before beginning and I cant remember how to do that also this is very basic and just deletes all the lines till it gets to one that starts end

      see if you can work out some code and post it here if you have problems

      Code:
      Do
      
      Selection.EndKey Unit:=wdLine, Extend:=wdExtend
      If UCase(Left$(Selection.Text, 3)) = "END" Then Exit Do
      Selection.Delete
      
      Loop
      That code deletes the lines, but it doesn't stop on the text that I want it to. I amended the code so "Exit Do" would happen at:

      Your tax file number (TFN) 111 191 206 Are you an Australian resident? Y Print Y for yes

      I have tried to input that entire line of text but it doesn't work.

      Actually my Ideal situation is that it doesn't need the entire line of text to identify the line to stop at, for example for this text, if I put in "Your tax file number (TFN)" like in the below shown code, the line deletion subroutine should stop.

      Code:
      If UCase(Left$(Selection.Text, 3)) = "Your tax file number (TFN) " Then Exit Do

      Thanks

      Comment

      • jg007
        Contributor
        • Mar 2008
        • 283

        #4
        one problem with the selection is that it picks up the CR on the end of the text as you will see if you break and then check selection.text , that is why I was chopping off the first 3 characters with 'left'

        how about this code?

        Code:
        Public Sub deleteme()
        
        Do
          
        Selection.EndKey Unit:=wdLine, Extend:=wdExtend
        If InStr(1, Selection.Text, "Your tax file number (TFN)") Then Exit Do
        
        Selection.Delete
          
        Loop
        
        End Sub

        Comment

        • iheartvba
          New Member
          • Apr 2007
          • 171

          #5
          Originally posted by jg007
          one problem with the selection is that it picks up the CR on the end of the text as you will see if you break and then check selection.text , that is why I was chopping off the first 3 characters with 'left'

          how about this code?

          Code:
          Public Sub deleteme()
          
          Do
            
          Selection.EndKey Unit:=wdLine, Extend:=wdExtend
          If InStr(1, Selection.Text, "Your tax file number (TFN)") Then Exit Do
          
          Selection.Delete
            
          Loop
          
          End Sub
          Hi,

          That worked Magnificently. Thanks alot

          Comment

          Working...