View Single Post
 
Old 01-23-2021, 01:08 AM
SMehta SMehta is offline Windows 10 Office 2013
Novice
 
Join Date: Jan 2021
Posts: 29
SMehta is on a distinguished road
Default

Sir I tried all together new method to achieve partly to what was desired.
by using FSO method. I thought the below coding would help me at least get list of Blank Line nos
somehow not successful

I get Subscript out of Range Error

If some idea could be implemented on how to define and set the wildcard characters for blank paragraph
txtToSrch ="[^13]" or "^p" or "[^13]{2}[!^13]*[^13]{2}"

Else if
txtToSrch ="Jim" then there is no Subscript out of Range Error and it shows word Jim in Lines

FYI I've added in reference Microsoft Scripting Runtime as this was required after exploring through few VBA forums. Below code is also implemented after exploring VBA forums

Code:
Option Explicit

Public Type SearchResults
    BlankLineNumber As Long
    CharPosition As Long
    StrLen As Long
End Type

Sub Example()
    Dim MySearch() As SearchResults, i As Long, txtToSrch As String
    
    txtToSrch = "[^13]"
    
    MySearch = GetSearchResults_2("C:\Text-To-MsWord\Sample.txt", txtToSrch, vbTextCompare)
    
    For i = 0 To UBound(MySearch)
        With MySearch(i)
            MsgBox "Blank Lines Are At " & vbCrLf & .BlankLineNumber
        End With
    Next
    
End Sub

Function GetSearchResults_2(FileFullName As String, FindThis As String, Optional CompareMethod As VbCompareMethod = vbBinaryCompare) As SearchResults()
  
    Dim fso As New FileSystemObject, s As String, pos As Long, l As Long, sr As SearchResults, ret() As SearchResults, i As Long
    Dim blnkLineNo As Long
    
    With fso.OpenTextFile(FileFullName)
        Do Until .AtEndOfStream
              l = l + 1
              s = .ReadLine
            pos = 1
            Do
                pos = InStr(pos, s, FindThis, CompareMethod)
                If pos > 0 Then
                    sr.CharPosition = pos
                    sr.BlankLineNumber = l
                    sr.StrLen = Len(FindThis)
                    
                    ReDim Preserve ret(i)
                    ret(i) = sr
                    i = i + 1
                    pos = pos + 1
                End If
            Loop Until pos = 0
        Loop
    End With
    GetSearchResults_2 = ret
End Function
Will await your inputs, corrections and guidance

SMehta
Thread 1: No: 46342 : Post No14 : TM 8
Reply With Quote