Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you use the Input# function? 2

Status
Not open for further replies.

monak

MIS
Jul 2, 2002
22
US
I am trying to figure out this function. I know this function grabs lines out of your file. Could someone give more details as to how this function works?
 
Open textfilename For Input As #1 'read existing
Open RoutingsTableFile For Output As #2 'write to new

'use Append instead of Output to write to an existing file
 
The Input method does not necessarily read an entire line.
Here are a couple examples:
Code:
Dim sName As String
Dim sInits As String
Dim sPhone As String
Dim sSSN As String

Suppose File1.dat contains this:
Bill A. Smith, BAS, 555-1212, 123-12-1234
Jody W. Harris, JWH, 555-2121, 312-21-4321
etc.

'Read Each Record
Open "File1.dat" For Input As #1  'Note - use complete path
  Do While Not EOF(1)
    Input #1, sName, sInits, sPhone, sSSN
    'Looking for JWH
    If sInits Like "JWH" Then
      Exit Do
    End If
  Loop
Close#1

Suppose File2.dat contains this:
Bill A. Smith
Jody W. Harris
etc.

'Read Each Record
Open "File2.dat" For Input As #1  'Note - use complete path
  Do While Not EOF(1)
    Input #1, sName
    MsgBox "Current Name: " & sName
  Loop
Close#1

Suppose File3.dat contains this:
This is a single line of text, used for testing.
This line contains more text, again, just as a sample.
etc.

'Read Each Line - Not Comma Delimited
Dim sLine As String
Open "File3.dat" For Input As #1  'Note - use complete path
  Do While Not EOF(1)
    Line Input #1, sLine
    MsgBox "Current Line: " & sLine
  Loop
Close#1
[code]
Hope these samples help...
 
Thank you to both of you! Both these examples were very helpful. I was wondering something else. I thinking to use this Input function in a macro. The problem is the file I want to use is already open. I don't think I need to use an open command but using this Input function it looks like I will need to use the Opem command. Is there another way I grab the lines in my file?
 
Ok....these examples were VB not VBA.
Question, is this a text file you are trying to read or is it a workbook because different code will be used ?
 
Thank you for all you help. There one thing I am not clear of. I want to use this function inside of a macro in a word document. The file I want to use is already open. I do not think i need to open it again but in the open statement the file is marked as input. Is there way I can avoid using the open statement and still mark the file as Input?
 
the file I am trying to read is a Rich Text Format document.
I am trying to do a search through the doc for /strike and /cf6. Meaning this text that has strikeout and is red highlighted.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top