Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 06-12-2009, 07:31 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
Post Macro or Function to know wether a string is included in a text

Hello!!



I need to gather information from standard commercial reports, which include standard information, like "number of employees", or "issued capital".

Not all the information is available in every commercial report, so I need a macro / function to search wether "number of employees" (for instance) is a string included in the current commercial report.

Depending of the result I would run other instructions.

Thanks in advance
Reply With Quote
  #2  
Old 06-12-2009, 07:52 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

Not knowing how exactly you are going to want to use this, I would suggest a standard find and replace string (you don't need to be replacing the text).

To get the code, record a new macro and go through the process of using find and replace to find your selected text. Once you have done that, edit the macro and add the other instructions that you want to run!
Reply With Quote
  #3  
Old 06-12-2009, 08:07 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

Quote:
Originally Posted by Eduardo View Post
Hello!!

I need to gather information from standard commercial reports, which include standard information, like "number of employees", or "issued capital".

Not all the information is available in every commercial report, so I need a macro / function to search wether "number of employees" (for instance) is a string included in the current commercial report.

Depending of the result I would run other instructions.

Thanks in advance
Thanks Bird!!

Currently, my macro searchs the string and, if the string is found, execute the following instructions.

The problem appears in case the string is not found, because the macro ALSO EXECUTE THE FOLLOWING INSTRUCTIONS.

That`s because I need a way to make a difference between both cases (when the search is successfull and when it is not).

I though about preparing a function or a variable to store the value TRUE / FALSE, depending on the result of the search (successfull or not successfull). My idea is to use such variable / function within an IF instruction, executing a different group of instructions depending on the success of the search.

I don't know how to build such variable / function. That's my problem.

Thanks any way
Reply With Quote
  #4  
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
  #5  
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
  #6  
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

Thread Tools
Display Modes


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 - Senior Forums

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


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