Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    I am trying to have Word search a line of text in a table cell in the footer, looking specifically for a "*" and then if its found, delete the line. If it's not found, search for a "%" and if that's found, delete the line. If it's not found, search for a "?" and then delete the line and lastly, if none of these characters are found then delete the existing text and type "No Wildcards were used in this search."

    I just can't seem to get the code to work. It does the first search fine, but then doesn't do the second two. I'm sure I'm just missing something easy. Here's the code I've tried a few times, with no success:

    Selection.HomeKey unit:=wdStory
    ActiveWindow.ActivePane.View.SeekView = wdSeekCurrentPageFooter
    Selection.MoveDown unit:=wdLine, Count:=1
    Selection.MoveRight unit:=wdCell
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
    .Text = "*"
    .Forward = True
    .Wrap = wdFindStop
    If .Execute Then
    Selection.SelectCell
    Selection.Delete
    ElseIf Selection.Find.Text = "%" Then
    If .Execute Then
    Selection.SelectCell
    Selection.Delete
    End If
    ElseIf Selection.Find.Text = "?" Then
    If .Execute Then
    Selection.SelectCell
    Selection.Delete
    End If
    Else
    Selection.SelectCell
    Selection.TypeText Text:="No wildcards used in this search"
    End If
    End With

    Thanks in advance for your help and/or suggestions.

  2. #2
    Super Moderator jscher2000's Avatar
    Join Date
    Feb 2001
    Location
    Silicon Valley, USA
    Posts
    23,112
    Thanks
    5
    Thanked 93 Times in 89 Posts
    [quote name='maverik' post='798937' date='19-Oct-2009 18:09']It does the first search fine, but then doesn't do the second two.[/quote]
    Your selection might be coallpsing before you run the second and third searches. If you use the VB Editor's Debug toolbar and single-step through the code, can you see what happens to the selection after the first search finds nothing?

    === Edit ===

    That was wrong. It's the ElseIf statements that are the problem.

  3. #3
    Star Lounger
    Join Date
    Sep 2007
    Posts
    54
    Thanks
    7
    Thanked 1 Time in 1 Post
    [quote name='jscher2000' post='798943' date='20-Oct-2009 04:15']Your selection might be coallpsing before you run the second and third searches. If you use the VB Editor's Debug toolbar and single-step through the code, can you see what happens to the selection after the first search finds nothing?[/quote]

    I got it, I think... Apparently the execute in the If codes was wrong. It should be like this...

    If .Execute(findtext:="*") Then
    Selection.SelectCell
    Selection.Delete
    ElseIf .Execute(findtext:="?") Then
    Selection.SelectCell
    Selection.Delete
    ElseIf .Execute(findtext:="%") Then
    Selection.SelectCell
    Selection.Delete
    Else
    Selection.SelectCell
    Selection.TypeText Text:="No wildcards used in this search"
    End If

    Thanks for your reply, though. Muchly appreciated.

Posting Permissions

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