Hi,
Please help me create a function that would return if a particular word exists in the given text file or not? I am working in automation testing too TestPartner which uses VBA scripting language
7 60505
there's a built in function in VB and VBA called "instr"
it's format is --->
instr ( position to start search, string to search, what to search for)
so, for example, if you had a string "search for", then it would be
x = instr ( 1, "search for", "arc" )
(in your case, you would have a variable containing individual lines in the text file or the entire text file - in place of "search for" )
and x would be 3 ( VB starts at 1 instead of 0 )
if you need more, search for "instr" on the internet.
I guarantee whatever you're searching for in your document you can do it using "instr"
Yes, the simplest (not necessarily best, depending on things like file size) would be to open the file in Binary mode and load it into a string in one action. Then use Instr to search it. For example, here's a very simple function which will accept a filename and a string, and tell you whether the string is found anywhere in the file... - Public Function StringExistsInFile(ByVal TheString As String, ByVal TheFile As String) As Boolean
-
Dim L As Long, S As String, FileNum As Integer
-
FileNum = FreeFile
-
Open TheFile For Binary Access Read Shared As #FileNum
-
L = LOF(FileNum)
-
S = Space$(L)
-
Get #1, , S
-
Close #FileNum
-
If InStr(1, S, TheString) Then
-
StringExistsInFile = True
-
End If
-
End Function
I believe this will work (haven't tested it) but there are things to keep in mind. For example, since this reads the entire file into memory, a huge file could cause problems. You can get around this by reading the file in smaller chunks, but that coimplicates things a bit because for example the search string could start in one chunk and finish in the next.
Thankyou! this should help.
I have written a simple code to search fro a particular word/words in a word doc: - Function fnSearchWordDoc(strPath, strSrTxt)
-
-
Set objWord = CreateObject("Word.Application")
-
Set A = objWord.Documents.Open(strPath)
-
-
'MsgBox A.Content.Text
-
Doctext = A.Content.Text
-
-
If InStr(Doctext, strSrTxt) <> 0 Then
-
fnSearchWordDoc = "Text found in the document"
-
Else
-
fnSearchWordDoc = "Text not found in the document"
-
End If
-
A.Close
-
-
objWord.Visible = False
-
Set objWord = Nothing
-
End Function
-
And for searching in a text file: - Function fnFindText(strFilePath, strSrTxt)
-
strFileRes = fnFileExistence(strFilePath)
-
If strFileRes = "File Exists" Then
-
Const ForReading = 1
-
-
Set fso = CreateObject("Scripting.FileSystemObject")
-
Set f = fso.OpenTextFile(strFilePath, ForReading, True)
-
Do While f.AtEndOfStream <> True
-
A = f.readline
-
'MsgBox a
-
If InStr(A, strSrTxt) <> 0 Then
-
boolStFnd = "True"
-
fnFindText = "Text found"
-
Exit Do
-
End If
-
Loop
-
If boolStFnd <> "True" Then
-
fnFindText = "Text not found"
-
End If
-
f.Close
-
Else
-
fnFindText = "File does not exists"
-
End If
-
End Function
Regarding "I believe this will work (haven't tested it)":
Function StringExistsInFile works beautifully in Excel 2007 VBA, which is exactly what I needed and where I tested it. Thanks very much -- it's brilliant!
-- Dennis
i like to tyy this.....thanx
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Simon Verona |
last post by:
Not sure if this is the best group... it may be better off in one of the ADO
groups, but I'm sure somebody here knows the answer:
I'm trying to load up a text file using ADO.net, as follows:
...
|
by: Praveen_db2 |
last post by:
Hi all
Db2 8.1.3 windows
Is there any way to write data into a text file using a stored procedure?
The way we return a cursor output to the calling application, can we
return data in a text...
|
by: christine0207 |
last post by:
Hi,
how to generate a fixed length data text file using Scripting.TextStream / filesystemobject.
|
by: asenthil |
last post by:
Hai,
i'm a beginner to java...
just now i had tried to read and write files using java...
and then i had tried to connect a database using jdbc...
now i want to export the data's from a...
|
by: varsha desai |
last post by:
Hello there,
I want to change some data(which is in one line only) of text file using VB 6.0. Which is the best method for it? Another question is I want to delete last two, three lines...
|
by: neveen |
last post by:
i want to open and read text file using j2me that can run on mobile 6630
then i want to make button called read that when pressed the data inside text display
|
by: charlesbritto |
last post by:
A C++ program for counting individual string in a text file using file handling functions.,
for ex:
if the text file contains,
am a boy,am studying +2,am from chennai
Now the result shoud...
|
by: sajidali |
last post by:
i am trying to append a text file using c#. when i executes my application i writes to the text file using more then one functions. i want to append file only during execution of programe. next time...
|
by: Thiem Teddy |
last post by:
Dear all
I use VB6 to export data into xls or text file, I followed this example ( http://bytes.com/topic/visual-basic/answers/530866-how-export-table-data-into-xls-text-file-using-vb6 ). but I...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| | |