473,394 Members | 1,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Searching for a particualr word in a text file,using VBA??

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
Apr 24 '07 #1
7 60505
manontheedge
175 100+
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"
Apr 24 '07 #2
Killer42
8,435 Expert 8TB
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...
Expand|Select|Wrap|Line Numbers
  1. Public Function StringExistsInFile(ByVal TheString As String, ByVal TheFile As String) As Boolean
  2.   Dim L As Long, S As String, FileNum As Integer
  3.   FileNum = FreeFile
  4.   Open TheFile For Binary Access Read Shared As #FileNum
  5.   L = LOF(FileNum)
  6.   S = Space$(L)
  7.   Get #1, , S
  8.   Close #FileNum
  9.   If InStr(1, S, TheString) Then
  10.     StringExistsInFile = True
  11.   End If
  12. 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.
Apr 25 '07 #3
Thankyou for the reply!
Apr 25 '07 #4
Thankyou! this should help.
Apr 25 '07 #5
I have written a simple code to search fro a particular word/words in a word doc:

Expand|Select|Wrap|Line Numbers
  1. Function fnSearchWordDoc(strPath, strSrTxt)
  2.  
  3. Set objWord = CreateObject("Word.Application")
  4. Set A = objWord.Documents.Open(strPath)
  5.  
  6. 'MsgBox A.Content.Text
  7. Doctext = A.Content.Text
  8.  
  9. If InStr(Doctext, strSrTxt) <> 0 Then
  10.    fnSearchWordDoc = "Text found in the document"
  11. Else
  12.    fnSearchWordDoc = "Text not found in the document"
  13. End If
  14. A.Close
  15.  
  16. objWord.Visible = False
  17. Set objWord = Nothing
  18. End Function
  19.  
And for searching in a text file:

Expand|Select|Wrap|Line Numbers
  1. Function fnFindText(strFilePath, strSrTxt)
  2.       strFileRes = fnFileExistence(strFilePath)
  3.       If strFileRes = "File Exists" Then
  4.          Const ForReading = 1
  5.  
  6.          Set fso = CreateObject("Scripting.FileSystemObject")
  7.          Set f = fso.OpenTextFile(strFilePath, ForReading, True)
  8.          Do While f.AtEndOfStream <> True
  9.             A = f.readline
  10.             'MsgBox a
  11.             If InStr(A, strSrTxt) <> 0 Then
  12.                boolStFnd = "True"
  13.                fnFindText = "Text found"
  14.          Exit Do
  15.             End If
  16.          Loop
  17.          If boolStFnd <> "True" Then
  18.             fnFindText = "Text not found"
  19.          End If
  20.          f.Close
  21.       Else
  22.         fnFindText = "File does not exists"
  23.       End If
  24.     End Function
Apr 25 '07 #6
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
Oct 15 '08 #7
i like to tyy this.....thanx
Oct 18 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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: ...
2
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...
1
by: christine0207 | last post by:
Hi, how to generate a fixed length data text file using Scripting.TextStream / filesystemobject.
2
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...
4
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...
1
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
2
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...
2
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...
0
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
jinu1996
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...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.