Newbie question: Parsing a line

  • Thread starter Thread starter Rhino
  • Start date Start date
R

Rhino

Let's say that I'm reading an external file with a macro:

--------------------------------------------------------------------------------------------------------
Sub readFromFile()
Const READ As Integer = 1
Const DO_NOT_CREATE As Boolean = False
Const TristateUseDefault As Integer = -2
Const FILE_NAME As String = "c:\Documents and Settings\Rhino\My
Documents\resume.txt"
Dim file, fileObject, textStream, oneLine

' Open the file
Set fileObject = CreateObject("Scripting.FileSystemObject")
Set file = fileObject.GetFile(FILE_NAME)
Set textStream = file.OpenAsTextStream(READ, TristateUseDefault)

' Read the file a line at a time until it has all been read
Do While textStream.AtEndOfStream <> True
oneLine = textStream.ReadLine

'---Pseudocode starts after this line---

'---Pseudocode ends before this line---

MsgBox oneLine
Loop

' Close the file
textStream.Close
End Sub
--------------------------------------------------------------------------------------------------------

In the pseudocode area within the loop, I want to inspect each line as its
read and format the data in that line based on what key is being displayed.
Something like this:

Pseudocode:

dim positionOfEqualSign as Integer = -1 'this variable holds position of
equal sign on given line
positionOfEqualSign = oneLine.indexOf("=") 'find the equal sign for the
current line
key = oneLine.substring(0, positionOfEqualSign) 'the key is the part of the
line before the = sign
if (key = "Name")
'write and format name
else
if (key = "Address")
'write and format address
else
if (key = "Phone")
'write and format phone number
else
MsgBox 'Unexpected key, ' key ', ignored'
end



Can anyone tell me how to accomplish this in a Word macro? I've looked and I
can't find anything that is capable of looking at a given line of text to
see if it contains a given character, nor can I find anything that gives a
substring of a given string, such as the first x characters of the string.

I'm using Word 2002 on XP Pro.
 
I eventually found what I wanted in the help files; the function to locate a
character within a string is InStr and the functions to extract a portion of
a string are left(), right(), and mid().

I guess I should have looked that much longer before posting :-) Oh well,
the information may be useful to some other newbie someday who is trying to
do the same thing....

Mind you, it would still be useful to know if I can do 'find' searches on
the external file somehow. (See my previous post.)

Rhino
 
To look for a given string within another, use Instr() or InstrRev(). You
can also use LIKE with a regular expression.

To return a substring, use Left$(). Mid$(), or Right$().

Rather than reading the file one line at a time, it is *much* quicker to
read the entire file in one operation, split into an array, then process the
array elements --

Dim pFileNum as long
Dim pFileContents as string
Dim pLines() as string
Dim pIndex as long
Dim pPostion as long
Dim pKey as string

pFileNum = Freefile
Open FILE_NAME for input as pFileNum
pFileContents = Input(LOF(pFileNum), pFileNum)
Close #pFileNum

pLines = split(pFileContents, vbCr) 'Might need to use vbCrLf
for pIndex = lbound(pLines) to uBound(pLines)

pPosition = instr(pLines(pIndex), "=")
if pPosition > 0 then
pKey = trim$(Left$(pLines(pindex), pPosition - 1))
Select case pKey
Case "Name"
...
Case else
End select
end if

Next
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top