Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2009, 02:43 PM
Bird_FAT's Avatar
Bird_FAT Bird_FAT is offline Macro or Function to know wether a string is included in a text Macro or Function to know wether a string is included in a text Office 2007
Expert
 
Join Date: Apr 2009
Location: South East
Posts: 271
Bird_FAT is on a distinguished road
Default

In the following example, I am running a Find and Replace string for the word 'dog', Then I am using If statements to choose what to do if I find the word, or not. (read the green tags for info on each section)

Code:
Sub Find_Then_IF_Macro()
' Example by Bird
'
' This first section is the Find string - the only really important
' line is the one in RED, as this is what we are searching for

    Selection.HomeKey Unit:=wdStory
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    With Selection.Find
        .Text = "dog"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With

' This next part is where we decide on the action to be taken if
' the text we are looking for IS present.
' I just chose to move the cursor 5 places to the right!
    If Selection.Find.Execute = True Then
        Selection.MoveRight Unit:=wdCharacter, Count:=5

' This next section is what we want to happen if the text we are
' looking for is NOT found; in this case, I have stopped the code
' by using Exit Sub (i.e. STOP)
    Else
        If Selection.Find.Execute = False Then
            Exit Sub

' Now, to finish off, we have to add an End If statement for
' every If that we had.
' NOTE: If you had values other than 'Yes/No' or 'True/False'
' then you could have more Else, If statements.
        End If
    End If
End Sub
Hope that helps - I'll be monitoring this post if you have any problems!
Reply With Quote
  #2  
Old 06-15-2009, 01:21 AM
Eduardo Eduardo is offline Macro or Function to know wether a string is included in a text Windows XP Macro or Function to know wether a string is included in a text Office XP
Novice
Macro or Function to know wether a string is included in a text
 
Join Date: Jun 2009
Posts: 3
Eduardo is on a distinguished road
Default

Your solution works.

Thank you!

It's my first question. I don't know how to do to add to your reputation in return. What should I do?
Reply With Quote
  #3  
Old 06-15-2009, 01:55 AM
Bird_FAT's Avatar
Bird_FAT Bird_FAT is offline Macro or Function to know wether a string is included in a text Macro or Function to know wether a string is included in a text Office 2007
Expert
 
Join Date: Apr 2009
Location: South East
Posts: 271
Bird_FAT is on a distinguished road
Default

Just click on the scales - top right of any of my posts!

Thanks - glad to have helped!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Macro or Function to know wether a string is included in a text Help! for using an appropriate function/Macro in Excel pawan2paw Excel 1 06-04-2009 12:28 PM
Extract from String using Wildcard whousedmy Word 0 05-21-2009 01:35 AM
Templates: automatic text generation from Rich Text content control Chickenmunga Word 0 10-01-2008 11:16 AM
Macro or Function to know wether a string is included in a text Need help on what function to use??? Primeraman Excel 1 06-13-2006 10:16 AM
Help with Combination function sanasath Excel 0 12-13-2005 07:24 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 08:29 AM.


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