Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

HELP!!!...Search and Replace text

Posted on 1998-09-01
2
Medium Priority
?
162 Views
Last Modified: 2010-04-30
Essentially my problem is locating if one or both strings exist, if they do delete them, then insert the text from the textbox.

I need to search the autoexec.bat for two strings.

SET COMPUTERNAME = something
SET USERNAME = something

If either one of these is found delete them, then insert the text from a texbox that contains:

SET COMPUTERNAME = %computername
SET USERNAME = %username

I can read the computername into a variable, and the username into a variable and then place the above statement into a textbox. I also have the autoexec.bat into a rtf box.  I am using the Commondialog to retrieve the filename, place it into the rtf box, and then use the CommonDialog to save the file.

0
Comment
Question by:UpAllNite
2 Comments
 
LVL 15

Expert Comment

by:Eyal
ID: 1432463
Use RichTextBox, It has a find method.
this method selects the appropriate words found. the last thing you have to do is to put the string you want in the SelText property.

Hope this helps
0
 
LVL 14

Accepted Solution

by:
waty earned 150 total points
ID: 1432464
Use the folowing function :

Function ReplaceString(sSearch As String, sSearchFor As String, sReplaceWith As String) As String
   ' *** Searches the sSearch variable for sSearchFor
   ' *** and replaces it with sReplaceWith.

   On Error GoTo ERROR__ReplaceString

   Dim lFoundLoc        As Long   ' Location of match.
   Dim lLenRemove       As Long   ' Length of string being replaced.
   
   ' *** Set length of original text to skip.
   lLenRemove = Len(sSearchFor)    ' Set location of match.
   
   lFoundLoc = InStr(1, sSearch, sSearchFor)
   
   ' If sSearchFor isn't found in sSearch
   ' just return the original string.
   If lFoundLoc = 0 Then
      ReplaceString = sSearch
      ' If match is found, return original string up to match
      ' location, concatenate new text, and search the rest of
      ' the string recursively for additional matches.
   Else
      ReplaceString = Left(sSearch, lFoundLoc - 1) & sReplaceWith & ReplaceString(Mid(sSearch, lFoundLoc + lLenRemove), sSearchFor, sReplaceWith)
   End If

EXIT_ReplaceString:
   Exit Function

ERROR__ReplaceString:
   ' Print error to Debug window and don't interrupt query.
   Debug.Print "Error Replacing String """ & sSearchFor & """ with """ & sReplaceWith & """ in text """ & sSearch & """"
   ' If there is an error, return original string
   ' and exit the function.        ReplaceString = sSearch
   Resume EXIT_ReplaceString

End Function


0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question