Solved

HELP!!!...Search and Replace text

Posted on 1998-09-01
2
153 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 50 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now