+ Reply to Thread
Results 1 to 5 of 5

Extract small string of text from larger string

Hybrid View

  1. #1
    Registered User
    Join Date
    06-12-2008
    Posts
    11

    Extract small string of text from larger string

    I’m working on a bit of code that I want to extract a small piece of text form a larger text file.

    At the moment I can open and read the entire text file and also pin point a start and end marker of the text I want to extract.

    What I’m having trouble doing is creating a variable text string which contains the text between the 2 markers.

    What I have so far is

    
    Function ExtractText()
    Dim sSearchText1 As String
    Dim sSearchText2 As String
    Dim sFileName As String
    Dim sFileText As String
    Dim strText As String
    
    Dim strStart As Integer
    Dim strEnd As Integer
    
    Const ForReading = 1
    
    sSearchText1 = "--**Part1**" ' start point
    sSearchText2 = "--**Part2**" 'end point
    
    sFileName = "C:\Scripts\Script.sql"
    
    'Create instance of FileSystemObject.
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objfile = objFSO.OpenTextFile(sFileName, ForReading)
    
    'read entire contents of file, save to strText variable
    strText = objfile.ReadAll
    
    strStart = InStr(strText, sSearchText1)
    strEnd = InStr(strText, sSearchText2)
    
    End Function
    Last edited by mark_jam3s; 09-23-2010 at 05:34 AM.

  2. #2
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Extract small string of text from larger string

    If it's not a calculation do not use a function but a sub.

    Sub tst()
      open "C:\Scripts\Script.sql" for input as #1
        msgBox split(split(Input(LOF(1),#1),"--**Part2**" )(0),"--**Part1**")(1),,"This is the text you were looking for"
      close #1
    End Sub
    In a function
    Sub exemplum()
      msgbox tst("C:\Scripts\Script.sql"),,"Alternative"
    End Sub
    
    Function tst(c01)
      open c01 for input as #1
        tst =split(split(Input(LOF(1),#1),"--**Part2**" )(0),"--**Part1**")(1),,"This is the text you were looking for"
      close #1
    End Function
    Last edited by snb; 09-22-2010 at 10:38 AM.



  3. #3
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Extract small string of text from larger string

    Mid$(strText, strStart, strEnd - strStart)
    Note: it's very misleading to have integer variables whose names start with 'str'!
    Everyone who confuses correlation and causation ends up dead.

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,015

    Re: Extract small string of text from larger string

    @snb,
    I think you are missing a quote at the start of the msgbox title string.

  5. #5
    Registered User
    Join Date
    06-12-2008
    Posts
    11

    Re: Extract small string of text from larger string

    Thank you both for your replies!

    I'll will post my code once I've finished tidying it up

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1