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.
--------------------------------------------------------------------------------------------------------
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.
Oh well,