Solved

Open any file to look for a string inside

Posted on 2000-05-17
15
157 Views
Last Modified: 2010-05-02
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?
0
Comment
Question by:familine
  • 3
  • 3
  • 2
  • +6
15 Comments
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2818054
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2818074
you can cheat and load the text into a richtextbox control and use its find method
0
 
LVL 7

Expert Comment

by:Vbmaster
ID: 2818079
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
 
LVL 14

Expert Comment

by:wsh2
ID: 2818086
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
 
LVL 1

Expert Comment

by:j3877
ID: 2818117
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
 
LVL 38

Expert Comment

by:PaulHews
ID: 2818385
(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
 
LVL 15

Expert Comment

by:StingRaY
ID: 2818386
Yup!! familine.
I agree with AzraSound.
Try his suggestion. 8-)
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 15

Expert Comment

by:StingRaY
ID: 2818403
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2818412
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
 
LVL 1

Expert Comment

by:csameer1
ID: 2832058
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
 

Author Comment

by:familine
ID: 2903904
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
 
LVL 9

Expert Comment

by:GivenRandy
ID: 2906651
To do that, click on the "Accept..." button to the right of his comment.
0
 
LVL 9

Expert Comment

by:GivenRandy
ID: 3527493
If AzraSound posts it as an answer, you can just accept it then.
0
 
LVL 28

Accepted Solution

by:
AzraSound earned 50 total points
ID: 3529418
thanks for the reminder randy
0
 

Author Comment

by:familine
ID: 4009788
Thanks for help
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now