Consulting

Results 1 to 8 of 8

Thread: Solved: Find and delete text between two areas

  1. #1

    Solved: Find and delete text between two areas

    Hi,

    If in a word document I have a table filled with data, then something else, then the word Dear whoever, how do I find the section in the middle and delete it all out. The bit in the middle may vary between 1 and 10 lines.

    It looks something like this (nb Name down to Postcode is in a table). In this example it would be the data all with the X in I want to delete.

    Name
    Job
    Company
    Line1
    Line2
    Line3
    Postcode


    Mr X
    Company X
    X Line 1
    X Line 2
    X Line 3
    City X
    Postcode X


    Dear Martin




    Thanks, Mark

  2. #2
    VBAX Wizard lucas's Avatar
    Joined
    Jun 2004
    Location
    Tulsa, Oklahoma
    Posts
    7,323
    Location
    Why not just have a blank template to re use each time...could be filled using bookmarks from a userform...
    Steve
    "Nearly all men can stand adversity, but if you want to test a man's character, give him power."
    -Abraham Lincoln

  3. #3
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Certainly if this is going to be an on-going operation you would be best to have that chunk bookmarked. Then you could delete it with a one-line instruction.[vba]ActiveDocument.Bookmarks("DumpChunk").Range.Delete[/vba]That being said, perhaps if you elaborate on what exactly is going on.

    Why are you deleting this?
    WHEN are you deleting this? On document open? When ever the user decides to? When YOU decide to?
    Do you know the table number, that is, what its index number is? The third table in the document - Tables(3)? The fourth table in the document - Tables(4)?
    Are the "lines" (eg. Mr X, Company X, X Line 1) actual paragraphs?

    In any case, this may help. Assumption: the table in question is the first table in the document - Tables(1).[vba]Sub DumpChunk()
    Dim r As Range
    With Selection
    .HomeKey Unit:=wdStory
    With .Find
    .ClearFormatting
    .Text = "Dear"
    .Execute
    End With
    .Collapse Direction:=wdCollapseStart
    End With
    Set r = ActiveDocument.Range( _
    Start:=ActiveDocument.Tables(1).Range.End + 1, _
    End:=Selection.Start - 1)
    r.Delete
    End Sub[/vba]What it does:

    1. move Selection to start of document
    2. search forward for "Dear"
    3. collapse selection to its Start (just before "Dear")
    4. set a Range object with Start = just after Table(1), and End = just before the Selection
    5.. delete the range

    Done. You may want to error trap the search for "Dear". If there IS no "Dear", there could be unwanted effects.

    Note: using a Range object to make the deletion is the best way to go. By using a Range, it does not matter the size of the "chunk". One line, 10 lines - no difference. It simply deletes everything from just after the table, to just before "Dear".

  4. #4
    VBAX Regular
    Joined
    Jul 2007
    Posts
    16
    Location
    Very cool.

    I'm about to try out your code fumei.

    At the moment I am pulling data from an SQL dB, and pasting it into a document so the user can preview it, and pass comment.

    It works to the extent that the data comes in, and gets all dressed up into sexy little black number (aka a table with formatting)

    My problem is that if the user realises they have selected the wrong data to comment on, and they rehit, the button to get the right set, the original "wrong" data/table doesn't get cleared properly.

    See below:

    [vba]Dim DataToPaste As String
    DataToPaste = "" + txtData
    ' ActiveDocument.Sections(1).ProtectedForForms = False
    ' ActiveDocument.Sections(2).ProtectedForForms = True
    ' etc etc etc

    ' Method 3
    ' Method 1 sucked!
    ' Method two is being silly
    ' how many methods do I have to try before one "just" works !!!
    ' GRRRRR!

    ' Dim StartWord As String, EndWord As String
    'StartWord = "Start word"
    'EndWord = "End word"
    '
    'With ActiveDocument.Content.Duplicate
    ' .Find.Execute Findtext:=StartWord & "*" & EndWord, MatchWildcards:=True
    ' .MoveStart wdCharacter, Len(StartWord)
    ' .MoveEnd wdCharacter, -Len(EndWord)
    ' .Font.Bold = True ' Or whatever you want to do
    'End With


    Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=2, Name:=""
    Selection.Find.ClearFormatting
    Selection.TypeText "?????"
    Selection.TypeParagraph
    Selection.TypeText (DataToPaste)
    Selection.TypeParagraph
    Selection.TypeText "?????"

    '''''''''''''''''''''''''''''
    Dim myrange As Range
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting

    With Selection.Find
    .Execute Findtext:="?????", Forward:=True, Wrap:=wdFindStop

    Set myrange = Selection.Range
    myrange.End = ActiveDocument.Range.End
    myrange.Start = myrange.Start + 5
    myrange.End = myrange.Start + InStr(myrange, "?????") - 1
    myrange.Select
    End With

    '''''
    '' Convert The Data Retrieved into a Table
    '''''

    Selection.ConvertToTable Separator:="|", _
    NumColumns:=7, NumRows:=16, AutoFitBehavior:=wdAutoFitFixed

    With Selection.Tables(1)
    .Style = "Table Grid"
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = True
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = True
    End With
    Selection.Font.Name = "Verdana"
    Selection.Font.Size = 9
    With Selection.Tables(1)
    .Style = "Table Colorful 2"
    .ApplyStyleHeadingRows = True
    .ApplyStyleLastRow = False
    .ApplyStyleFirstColumn = True
    .ApplyStyleLastColumn = False
    End With
    With Selection.Tables(1)
    .TopPadding = CentimetersToPoints(0)
    .BottomPadding = CentimetersToPoints(0)
    .LeftPadding = CentimetersToPoints(0.19)
    .RightPadding = CentimetersToPoints(0.19)
    .Spacing = 0
    .AllowPageBreaks = True
    Selection.Range.HighlightColorIndex = wdNoHighlight
    End With

    Selection.GoTo What:=wdGoToBookmark, Name:="Comment_Slot_01"
    Selection.Find.ClearFormatting
    DataRetrieved = 1

    ' With Selection.Find
    ' .Execute Findtext:="?????", Forward:=True, Wrap:=wdFindStop
    ' Selection.Delete
    ' End With
    '
    ' With Selection.Find
    ' .Execute Findtext:="?????", Forward:=True, Wrap:=wdFindStop
    ' Selection.Delete
    ' End With

    rstData.Close
    Conn.Close[/vba]
    I have been struggling with:
    [vba]On Error GoTo Error_ClearStage

    Selection.GoTo What:=wdGoToSection, Which:=wdGoToFirst, Count:=2, Name:=""
    Selection.Find.ClearFormatting

    Dim myrange As Range
    Selection.HomeKey wdStory
    Selection.Find.ClearFormatting
    With Selection.Find
    .Execute Findtext:="?????", Forward:=True, Wrap:=wdFindStop

    Set myrange = Selection.Range
    myrange.End = ActiveDocument.Range.End
    myrange.Start = myrange.Start ' + 5
    myrange.End = myrange.Start + InStr(myrange, "?????") - 5
    myrange.Select
    End With[/vba]

    As you can see by my comments, getting the range has been causing me no small amount of frustration.

    I've been struggling, because the defined range will over select since the myrange.end value gets lost after pasting the first set and deleting it.


    I have thought of a workaround, and why this is happening.

    In fact I should stop posting, and get jiggy while the idea is fresh in my mind.


  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Have you thought of doing this as as mailmerge?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    Gerry, thanks for the code. Amended it to work with bookmarks instead of a table.

    Cheers, Mark

    [VBA]Sub DumpChunk()
    Dim r As Range
    With Selection
    .GoTo What:=wdGoToBookmark, Name:="name"
    With .Find
    .ClearFormatting
    .Text = "Dear"
    .Execute
    End With
    .Collapse Direction:=wdCollapseStart
    End With
    Set r = ActiveDocument.Range( _
    Start:=ActiveDocument.Bookmarks("name").Range.End, _
    End:=Selection.Start - 1)
    r.Delete
    Selection.MoveUp Unit:=wdLine, Count:=1
    End Sub[/VBA]

  7. #7
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Bookmarks are good. They make things VERY handy to work with.
    Amended it to work with bookmarks instead of a table.
    Huh??????

    Is not the point getting whatver is between a specified location ("Dear") and the previous table? Now, if that range IS bookmarked fine, great, just be specific and careful about how you put content INTO the bookmark.

    Your code though is odd.

    You move the Selection to the bookmark. Bleeeech. Avoid using Selection if possible. However, in any case, you select the bookmark, and the assumption is that "Dear" is IN the selected bookmark.

    OK. So there is a search within the selected bookmark for "Dear". It finds it, collapses to Start - that is, just BEFORE "Dear".

    You set r with a Start of the end of the bookmark (which is....where exactly????), and a Start of Selection.Start - 1.

    Does this not include "Dear"????? I looks like the bookmark .End is after "Dear" - how else could it find it???

    So you are deleting "Dear"?

  8. #8
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Oh, and Malcolm's suggestion re: mail merge is a good idea.

Posting Permissions

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