Используйте следующий код, пожалуйста:
Option Explicit
Dim objFSO, objTF, fullFilename, strIn, El, arr, arrI
Dim obj1, strVendorPath, strVendor, k, startV, endV, strV, i
'Put the searching elements in an array (they look to appear twice: ones with a short name and second with a longger one:
arr = Split("LINCOLN|LINCOLN NATIONAL,PROTECTIVE|PROTECTIVE LIFE,AMGENERAL|AMERICAN GENERAL,JOHN HANCOCK|JOHN HANCOCK,PACIFIC|PACIFIC LIFE INS,PRUDENTIAL|PRUCO LIFE", ",")
fullFilename = "C:\Users\P148044\Documents\INS050E.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTF = objFSO.OpenTextFile(fullFilename, 1)
strIn = objTF.ReadAll 'read the text to be analized at once
objTF.Close
For Each El In arr
arrI = Split(El, "|") 'split the array elements in short and long parts
startV = InStr(strIn, arrI(0)) 'find first element occurrence
'look for the last occurence
Dim lngStart
For i = startV To Len(strIn)
If endV = 0 then
lngstart = startV
Else
lngstart = endV + Len(arrI(0))
End If
If InStr(lngStart, strIn, arrI(0)) >= startV Then
endV = InStr(lngStart, strIn, arrI(0))
Else
'I observed inconsistencies in the file construction and the short name is not all the time in the same place.
'I also observed that after the last occurrence a specific string ends the vendor area
endV = InStr(endV, strIn, "TOTAL NEW PURCHASE LIFE")
endV = InStr(endV, strIn, vbLf) + 1 'include the end of the line, too
If endV = 0 Then endV = Len(strIn)'for the last array element in the string
strVendor = Mid(strIn, startV, (endV - startV)) 'string area to be extracted
'building the vendor name file, in the same location with the initial one
strVendorPath = left(fullFilename, InStrRev(fullFilename, "\")) & arrI(1) & ".txt"
Set obj1 = objFSO.CreateTextFile(strVendorPath, True, True)
obj1.Write strVendor 'write the string at once
obj1.Close
Exit For 'exiting the loop
End If
Next
startV = 0: endV = 0 'reinitialising variables
Next
MsgBox "Ready..."
Пожалуйста, проверьте его и дайте мне знать, если он делает то, что вам нужно. Надеюсь, вы понимаете код логи c. Я попытался прокомментировать это, чтобы прояснить ...