Results 1 to 5 of 5
  1. #1
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    'Not' Found Action (Word 2003)

    I have the following code which searches for text and then deletes the line the text is on. I only want it to delete the line if the text is found. I know this is a simple question, but again, could not find my answer in the VB Documentation in Word.

    Selection.Find.ClearFormatting
    With Selection.Find
    .Text = "Control="
    .Replacement.Text = ""
    .Forward = True
    .Wrap = wdFindStop
    .Format = False
    .MatchCase = False
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    End With
    Selection.Find.Execute
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1

  2. Get our unique weekly Newsletter with tips and techniques, how to's and critical updates on Windows 7, Windows 8, Windows XP, Firefox, Internet Explorer, Google, etc. Join our 480,000 subscribers!

    Excel 2013: The Missing Manual

    + Get this BONUS — free!

    Get the most of Excel! Learn about new features, basics of creating a new spreadsheet and using the infamous Ribbon in the first chapter of Excel 2013: The Missing Manual - Subscribe and download Chapter 1 for free!

  3. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 16 Times in 16 Posts

    Re: 'Not' Found Action (Word 2003)

    Execute returns True if the text was found, False if not, so you can use

    If Selection.Find.Execute Then
    Selection.HomeKey Unit:=wdLine
    Selection.EndKey Unit:=wdLine, Extend:=wdExtend
    Selection.Delete Unit:=wdCharacter, Count:=1
    End If

  4. #3
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'Not' Found Action (Word 2003)

    If "Control=" is at the beginning of the paragraph, and if you want to delete the paragraph rather than the line, then you could delete it with a wildcard replacement:
    Find what: Control=*^13
    (Replace with nothing, replace all)

    Your original macro looks like something from the macro recorder (say, "Selection.Delete Unit:=wdCharacter, Count:=1", which nobody would write, but which is recorded when you hit Delete), and the macro recorder often records a "line" when you really meant a "paragraph", say when you select a paragraph/line with Shift+Down. The keyboard shortcuts to go to the beginning of the paragraph, and then select it, would be Ctrl+Up, Ctrl+Shift+Down... but I don't usually use them either.

    <img src=/S/cheers.gif border=0 alt=cheers width=30 height=16> Klaus

  5. #4
    2 Star Lounger
    Join Date
    Jul 2003
    Location
    Park City, Utah
    Posts
    189
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Re: 'Not' Found Action (Word 2003)

    Thanks, guys, I will work on both solutions. The second is "cleaner" if it works in my environment as we are actually getting rid of 21 "lines" or paragraphs of text and it has to repeat this process for each.

    ...and Klaus, yes I used the macro recorder because sometimes that is the quickets, albeit dirtiest, way to get something done. Thanks again!

  6. #5
    5 Star Lounger
    Join Date
    May 2001
    Location
    Stuttgart, Baden-W, Germany
    Posts
    931
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: 'Not' Found Action (Word 2003)

    > ...and Klaus, yes I used the macro recorder because sometimes that is the quickets, albeit dirtiest, way to get something done.

    Well: I use it, too! Have seen your lines of code in my recorded macros a thousand times...

    <img src=/S/grin.gif border=0 alt=grin width=15 height=15> Klaus

Posting Permissions

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