Open any file to look for a string inside

I need to open any kind of file to look for a string inside. I tried it with open but in the case of .doc (Word) documents are rare characters that do not allow me to read the complete document. So is it any way to open the file as ascii mode?
familineAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
AzraSoundConnect With a Mentor Commented:
thanks for the reminder randy
0
 
GivenRandyCommented:
Read a file "YourFileName":
---
    On Error Resume Next
    Open "YourFileName" For Input As #1
    If (Err.Number = 0) Then
        Do While (Not EOF(1))
            Line Input #1, S
            ' do something with S
        End If
    End If
    Close #1
    On Error GoTo 0
---
0
 
AzraSoundCommented:
you can cheat and load the text into a richtextbox control and use its find method
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
VbmasterCommented:
You can open the file in binary mode, this way you can get all the characters in one step and search for the string using code like this...

Dim Filenr As Integer
Dim Content As String

Filenr = FreeFile
Open filename For Binary As #Filenr
Content = Space$(LOF(Filenr))
Get #Filenr, , Content
Close #Filenr

Now the complete file is inside the Content string, and you can use Instr(Content, search_to_string_for) to search the content.
0
 
wsh2Commented:
Dim strBuffer as String
Open "MyFile" For Binary As #1
strBuffer = Space$(LOF(#1))
Get #1,,strBuffer
Close #1

If INSTR$(strBuffer, "MyString", 1) > 0 _
Then
   MsgBox "String Found"
Else
   MsgBox "String NOT Found"
End If
0
 
j3877Commented:
Given - you are bracketing things that do not need 2 b bracketed:

do while not eof(1)
    'DO SOMETHING!!!!!!!
loop

will work as well as:

do while (not eof(1))
    'DO SOMETHING!!!!!!!
loop


Also, putting in so many brackets make code look messy, and less like VB code - or is that your idea? Ashamed of being a VB programmer?
0
 
PaulHewsCommented:
(OffTopic)
I bracket conditional statements for readability.  I probably wouldn't have done so here, but I see it as a  personal preference kind of thing.  I don't think it looks messy.
0
 
StingRaYCommented:
Yup!! familine.
I agree with AzraSound.
Try his suggestion. 8-)
0
 
StingRaYCommented:
familine:
Because .DOC is not a plain text file.
It's a Word-Formatted file. You cannot read it as a text file.
Rich Text Box can read a simple .DOC file. So you can do as AzraSound mentioned above.
0
 
AzraSoundCommented:
opening the file as binary is acceptable here as well...my method is pretty lame to be truthful, though i appreciate your vote of confidence in me stingray!  =)

even a richtextbox doesnt handle all the formatting of a word document.  plus it would add more unwanted weight to the project.
0
 
csameer1Commented:
if you open a .doc file in ascii mode you see a lot of strange chars.but you can also read all the text unless it is password protected


search for file system object in vbhelp

It provides a neat way to open files(don't open .bmp or .jpg files with it ,it has some bug it seems)

user instr keyword to search for  a test
0
 
familineAuthor Commented:
Not valid, sorry... l did finally with the richtext trick :-)) it works quite well, as l can use also the find method. l will give the points to AzraSound
0
 
GivenRandyCommented:
To do that, click on the "Accept..." button to the right of his comment.
0
 
GivenRandyCommented:
If AzraSound posts it as an answer, you can just accept it then.
0
 
familineAuthor Commented:
Thanks for help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.