Re: Build an array from a text file



I suspect that each line in your active document is a new paragraph. Then,
something like
ActiveDocument.Paragraphs(1).Range.Text
would return the text of the first line, etc.

Steve


"Ryan Jamison" <RyanJamison@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:171648C0-6589-41A4-9228-62AE1C561FE4@xxxxxxxxxxxxxxxx
Thanks Steve, but one more question...are there any commands similar to
ReadLine for the active document? Now that I have created my array, I
need
to find lines such as:

MATERIAL 1
.
.
.
MATERIAL 2
.
.
.
etc... and then I will correspond the array number to the number shown and
insert a line. I know how to insert the line I need, etc, but can't quite
figure out how to find the number (1,2,etc) and than coordinate that with
my
array. Thanks

Ryan

"Steve Yandl" wrote:

Ryan,

The dictionary object has a 'keys' method that returns a 0 based array
containing all the keys in that dictionary. To get corresponding items,
use
the item property referencing the specific key as I did in the
subroutine.
There is also an 'items' method that returns an array of all the items.
If
you were to want to build a two dimensional array from the dictionary
object, there is also a 'count' property that returns a count of key/item
pairs. One additional tidbit, the keys in a dictionary must be unique,
items don't have to be.

__________________________________

Dim vArray As Variant
vArray = objDict.Keys
For i = 0 to objDict.Count - 1
MsgBox objDict.Item(vArray(i)) & " " & vArray(i)
Next
__________________________________

The big advantage of the dictionary object over arrays and collections is
the efficiency when you want to quickly check for the presence of some
unique key. I use it mostly in scripting if I want to eliminate
duplicates
from lists of items but I'm guessing it might also work well in the type
of
editing operation I'm guessing you're running based on your question.




Steve Yandl



"Ryan Jamison" <RyanJamison@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:EAE028AA-CAAC-494E-B465-A6A297BED5EA@xxxxxxxxxxxxxxxx
Thanks for the code Steve! It seems to be working just fine. But I
have
one
more question...how do you access the created dictionary? If I just
wanted
to view whats in the dictionary, how would I do this? If I needed to
access
either the numbers or material names, what would I do here? I've never
used
dictionaries, so any help would be great! Thanks again

Ryan

"Steve Yandl" wrote:

Ryan,

This should get you started. For my example, I created a text file
based
on
your example, named it "Materials.txt" and placed it in "C:\Test".

' ------------------------------------------------------

Sub MaterialsList()

Const ForReading = 1

Dim strLine As String
Dim strMaterial As String
Dim strVal As String
Dim strTestCase As String
Dim strResult As String

' Create needed objects
Set objDict = CreateObject("Scripting.Dictionary")
Set FSO = CreateObject("Scripting.FileSystemObject")
Set objFile = FSO.OpenTextFile("C:\Test\Materials.txt")

' Read file line by line to add keys and items to objDict
Do Until objFile.AtEndOfStream
strLine = objFile.ReadLine
If Left(strLine, 18) = "$ Material Record:" Then
strMaterial = Right(strLine, Len(strLine) - 19)
strLine = objFile.ReadLine
If Left(strLine, 4) = "MAT1" Then
strVal = Right(strLine, Len(strLine) - 5)
If Not objDict.Exists(strMaterial) Then
objDict.Add strMaterial, strVal
strMaterial = ""
strVal = ""
End If
Else
strMaterial = ""
strVal = ""
End If
End If
Loop

objFile.Close

' Test dictionary by checking on the value for material Steel
strTestCase = "Steel"

If objDict.Exists(strTestCase) Then
strResult = objDict.Item(strTestCase) & " " & strTestCase
MsgBox strResult
End If

' Release objects used
Set objFile = Nothing
Set FSO = Nothing
Set objDict = Nothing

End Sub


' ------------------------------------------------------

Steve Yandl




"Ryan Jamison" <RyanJamison@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:49BE8A65-79E3-4525-9358-8055F04FAFEE@xxxxxxxxxxxxxxxx
Steve,

Thank you for the reply. I'm having trouble getting started,
particularly
with the Scripting.FileSystemObject. Is there anyway you could post
some
sample lines of code? Thanks!

Ryan

"Steve Yandl" wrote:

Ryan,

I think the best approach would be to use two objects from the
scripting
runtime.

For reading and parsing your text file, you will want to check out
the
"Scripting.FileSystemObject". Assuming the format follows your
example
fairly closely (not a lot of blank lines etc.), you can use the
InStr
and
Split functions to parse through the lines of text. While you
could
create
an array, you will be better off creating a dictionary object
("Scripting.Dictionary") with the material names as the key and the
the
number being the item.

Steve Yandl


"Ryan Jamison" <RyanJamison@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message
news:A287A345-A9AC-479E-A9BE-76CEB76950EB@xxxxxxxxxxxxxxxx
I'm trying to build a 2D array by reading & picking out text from
a
separate
text file. Best way to describe what I am doing is an example:

I have a file called input_deck that is the main file that I am
using
and
wanting to edit. But in order to edit the file, I need
information
from
another file called materials.txt. Problem is materials.txt
looks
like:

$ Material Record: Aluminum
MAT1 3
$ Material Record: Steel
MAT1 2
$ Material Record: Copper
MAT1 1

And I need to build an array (or similar) that contains the
Material
name
(Aluminum, Steel, etc) and number (the number after MAT1).
Ideally,
it
would
be something like:

3 Aluminum
2 Steel
1 Copper

Then I would use this array for editing purposes in my original
file,
input_deck. So, is there a way to do anything like this, with my
working
file being the original file? Any suggestions or hints would be
greatly
appreciated! Thanks

Ryan











.



Relevant Pages

  • Re: Select Distinct from Array
    ... Omitting blanks code be included. ... Dim col As Collection ... The reason I asked is that my recollection is that Dictionaries ... For the array, it is not necessary to sort, but it makes it easier. ...
    (microsoft.public.excel.programming)
  • Re: Select Distinct from Array
    ... Dim col As Collection ... The reason I asked is that my recollection is that Dictionaries allow the features of matching case and omitting blanks, as the posted code does, and that Collections don't provide the required features. ... elements to a new array. ... For Each oElement In oInputArray ...
    (microsoft.public.excel.programming)
  • Re: Build an array from a text file
    ... The dictionary object has a 'keys' method that returns a 0 based array ... There is also an 'items' method that returns an array of all the items. ... Dim vArray As Variant ... dictionaries, so any help would be great! ...
    (microsoft.public.word.vba.general)
  • Re: Build an array from a text file
    ... Now that I have created my array, ... "Steve Yandl" wrote: ... The dictionary object has a 'keys' method that returns a 0 based array ... dictionaries, so any help would be great! ...
    (microsoft.public.word.vba.general)
  • dictionary count is greater than number of keys?
    ... looping over an array made from its keys later. ... However, if I loop to ... Dim ws As Worksheet ...
    (microsoft.public.excel.programming)

Loading