Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 09-09-2020, 12:20 AM
djced djced is offline Delete text between 'from' 'to' text strings Windows XP Delete text between 'from' 'to' text strings Office 2007
Novice
Delete text between 'from' 'to' text strings
 
Join Date: Jan 2020
Posts: 5
djced is on a distinguished road
Question Delete text between 'from' 'to' text strings

In various stereotyped documents, I am trying



1. to delete few lines and paragraphs in between text strings "service on the seller" to "service on the Buyer"
I tried this code, but it deletes all the lines after "service on the seller" till end of the document, even after removing or changing "myrange.End"

Code:
Sub SelectRangeStarttext()
    Selection.HomeKey Unit:=wdStory
    
    Dim myrange As Range
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
        .Execute FindText:="service on the seller", Forward:=True, Wrap:=wdFindStop 'this will initiate the start word '"The value for the purpose of court fee and jurisdiction"
        Set myrange = Selection.Range
        myrange.End = ActiveDocument.Range.End
        myrange.Start = myrange.Start
        myrange.End = myrange.End + InStr(myrange, "service on the Buyer") ' this will initiate the end word
        myrange.Select
        
        Selection.Delete
    End With
End Sub
2. also i need to delete page containing text strings "removal Slip"

thanking you for your support
Reply With Quote
  #2  
Old 09-09-2020, 05:33 AM
gmayor's Avatar
gmayor gmayor is offline Delete text between 'from' 'to' text strings Windows 10 Delete text between 'from' 'to' text strings Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Hmmm. The following will do what you ask, however if you delete the text block this will affect the second part because of text flow:
Code:
Sub Macro1()
Dim oRng As Range
Dim sText1 As String, sText2 As String
    sText1 = "service on the seller"
    sText2 = "service on the Buyer"
    Set oRng = ActiveDocument.Range
    If InStr(1, oRng, sText1) = 0 Then
        MsgBox sText1 & " not found"
        GoTo lbl_Exit
    End If
    If InStr(1, oRng, sText2) = 0 Then
        MsgBox sText2 & " not found"
        GoTo lbl_Exit
    End If
    With oRng
        .Start = .Start + InStr(oRng, sText1) - 1
        .End = .Start + InStr(oRng, sText2) + Len(sText2)
        .Text = ""
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub

Sub Macro2()
Dim oRng As Range
Dim sText1 As String
    sText1 = "removal Slip"
    Set oRng = ActiveDocument.Range
    If InStr(oRng, sText1) > 0 Then
        With oRng
            .Start = .Start + InStr(oRng, sText1) - 1
            .Collapse 1
            .Select
        End With
        ActiveDocument.Bookmarks("\page").Range.Delete
    Else
        MsgBox sText1 & " not found"
    End If
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 09-09-2020, 06:57 AM
djced djced is offline Delete text between 'from' 'to' text strings Windows XP Delete text between 'from' 'to' text strings Office 2007
Novice
Delete text between 'from' 'to' text strings
 
Join Date: Jan 2020
Posts: 5
djced is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
Hmmm. The following will do what you ask, however if you delete the text block this will affect the second part because of text flow:
Code:
Sub Macro1()
Dim oRng As Range
Dim sText1 As String, sText2 As String
    sText1 = "service on the seller"
    sText2 = "service on the Buyer"
    Set oRng = ActiveDocument.Range
    If InStr(1, oRng, sText1) = 0 Then
        MsgBox sText1 & " not found"
        GoTo lbl_Exit
    End If
    If InStr(1, oRng, sText2) = 0 Then
        MsgBox sText2 & " not found"
        GoTo lbl_Exit
    End If
    With oRng
        .Start = .Start + InStr(oRng, sText1) - 1
        .End = .Start + InStr(oRng, sText2) + Len(sText2)
        .Text = ""
    End With
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub

Sub Macro2()
Dim oRng As Range
Dim sText1 As String
    sText1 = "removal Slip"
    Set oRng = ActiveDocument.Range
    If InStr(oRng, sText1) > 0 Then
        With oRng
            .Start = .Start + InStr(oRng, sText1) - 1
            .Collapse 1
            .Select
        End With
        ActiveDocument.Bookmarks("\page").Range.Delete
    Else
        MsgBox sText1 & " not found"
    End If
lbl_Exit:
    Set oRng = Nothing
    Exit Sub
End Sub
Thank you very much for this real quick solution. This is where the forum and the legends help the novice like us...!
Reply With Quote
Reply

Tags
delete page, delete text between, from to text strings

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Extracting Specific Text Strings PWH68 Word VBA 6 10-08-2019 02:07 AM
Locating/deduping similar but not identical text strings strodden Excel 2 06-25-2018 10:37 AM
Delete text between 'from' 'to' text strings Renaming several strings of text by their pattern noname91 Word 10 01-05-2017 05:46 PM
how do I search the content of excel files for text strings soren Excel 2 03-19-2015 09:42 AM
Delete text between 'from' 'to' text strings VBA to copy bookmark-hyperlinks to plain text strings Marrick13 Word 2 04-29-2012 05:12 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:12 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft