Reply
 
Thread Tools Display Modes
  #1   Report Post  
Posted to microsoft.public.word.docmanagement
Rhino
 
Posts: n/a
Default Newbie question: Parsing a line

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.

--
Rhino


  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Rhino
 
Posts: n/a
Default Newbie question: Parsing a line

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


"Rhino" wrote in message
...
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.

--
Rhino




  #3   Report Post  
Posted to microsoft.public.word.docmanagement
Jezebel
 
Posts: n/a
Default Newbie question: Parsing a line

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




"Rhino" wrote in message
...
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.

--
Rhino




Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NewBie Question 2 Please Help Tom Grassi New Users 2 September 9th 05 05:03 PM
Cannot delete thin double line across page Dick Hob Microsoft Word Help 2 July 21st 05 05:12 AM
Newbie document question please Dudley Henriques New Users 4 January 9th 05 08:38 PM
formatting links from excel into word (extra line feeds) JGT Microsoft Word Help 1 December 9th 04 11:01 AM
Problem: A border line that I can't get rid of... Gary Warner New Users 2 December 1st 04 11:20 PM


All times are GMT +1. The time now is 11:22 AM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"