Ureader.com  
Microsoft software help and Community
   home   |   control panel login   |   archive   |  
 
Word
application.errors
conversions
docmanagement
drawing.graphics
formatting.longdocs
international
internet.assistant
mail
mailmerge.fields
menustoolbars
newusers
numbering
oleinterop
pagelayout
printingfonts
setup.networking
spelling.grammar
tables
vba.addins
vba.beginners
vba.customization
vba.general
vba.userforms
web.authoring
word6-7macros
word97vba
  
 
date: Mon, 11 Feb 2008 05:30:00 -0800,    group: microsoft.public.word.vba.beginners        back       


Parsing a txt file into Word   
Hello!
I've been asked to create a word document that parse a txt file and draw out 
the needed information.

Does anyone have any handy resources or usefull ideas on how to do this with 
the least amount of problems?

The txt file is a report that is exported from a Cisco router / firewall or 
the likes and contain basic formatting as well as different info down. Sadly, 
it can only export to txt and not XML or the likes which is much easier to 
work with.
-- 
Cato Larsen
HelpDesk Monkey
date: Mon, 11 Feb 2008 05:30:00 -0800   author:   Cato Larsen

RE: Parsing a txt file into Word   
A quick note and some additional info:
This would be easier to do in Excel. Sadly, this is not an option.
Thus this request.

The text will be parsed and inserted into different kinds of tables.
-- 
Cato Larsen
HelpDesk Monkey
date: Mon, 11 Feb 2008 05:35:01 -0800   author:   Cato Larsen

Re: Parsing a txt file into Word   
You would have to give us some information about the file with which you 
have to work.  How is the data arranged in it?  What constitutes a discrete 
piece of data, etc. etc?

-- 
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Cato Larsen"  wrote in message 
news:A7C8E6B9-76B4-4293-A72E-FA0511C982CB@microsoft.com...
>A quick note and some additional info:
> This would be easier to do in Excel. Sadly, this is not an option.
> Thus this request.
>
> The text will be parsed and inserted into different kinds of tables.
> -- 
> Cato Larsen
> HelpDesk Monkey
date: Tue, 12 Feb 2008 05:51:40 +1000   author:   Doug Robbins - Word MVP

Re: Parsing a txt file into Word   
Cato Larsen wrote:
> I've been asked to create a word document that parse a txt file and draw out
> the needed information.

What part of this are you having trouble with?  Reading the text file?  Separating 
it into lines?  Separating the lines into fields?  Understanding that a job like 
this has numerous specific requirements, each of which must be precisely defined?
-- 
.NET: It's About Trust!
 http://vfred.mvps.org
date: Mon, 11 Feb 2008 15:25:11 -0800   author:   Karl E. Peterson

Re: Parsing a txt file into Word   
The formatting of the text file is as follows:
At the top there are about 4-5 lines of junk. This can be discarded.
Next up are some 30 lines (has to be counted dynamicly) with the following 
formatting:
name ipAddress hostAdress
Example of the line formatted:
name 169.154.0.1 server.myWorld.com

name is static to all these lines.

Next up is interfaces, and they are separated by an exclamation mark before 
each "box" of text.
Formatting:
interface interfaceName
 nameif nameOfService
 security-level lvl
 ip address ipAddress hostMask

Example:
interface vlan123
 nameif Inside
 security-level 100
 ip address 169.154.0.1 255.255.255.0

interface, nameif, security-level & ip address are static to this bulk of 
text.

Next are some object groups
Formatting;
object-group type GR-name
 network-object ipAddress HostMask

Example:
object-group network GR-internet
 network-object 169.154.0.0 255.255.255.0
 network-object 169.155.0.0 255.255.0.0
 port-object eq 123
 group-object GR-here-internett
 icmp-object echo-reply

One object-group can contain an unknown amount of objects and other types of 
service descriptors.



------------------------------
My biggest issue is how to get this info into tables with a formatting & 
having it dynamicly update if the data.txt file is changed.
I've never parsed text in Word before, so the whole approach is new to me, 
and google was for once not that helpfull in my search for parsing txt files 
in word.

-- 
Cato Larsen
HelpDesk Monkey
date: Mon, 11 Feb 2008 23:19:02 -0800   author:   Cato Larsen

Re: Parsing a txt file into Word   
Cato Larsen wrote:
> I've never parsed text in Word before, so the whole approach is new to me,
> and google was for once not that helpfull in my search for parsing txt files
> in word.

Last thing first.  You're parsing text files in VB(A), not Word!  That's the first 
mindset that needs to change.  This isn't a job for Word.

> The formatting of the text file is as follows:

How big are these files?  Presumably, very small, right?  A few hundred K, at most?

I would advise reading the entire file into an array of "lines" and parsing them one 
by one.  That's pretty easy to do, sorta like this:

   Dim TheLines() As String
   TheLines = Split$(ReadFile(MyFile$), vbCrLf)

   Public Function ReadFile(ByVal FileName As String) As String
      Dim hFile As Long
      On Error GoTo Hell
      hFile = FreeFile
      Open FileName For Binary As #hFile
         ReadFile = Space$(LOF(hFile))
         Get #hFile, , ReadFile
      Close #hFile
   Hell:
   End Function

Then, you start looping through the file, line by line.

> At the top there are about 4-5 lines of junk. This can be discarded.

Is there a "comment" like marker for these?  Something to identify them as junk?  If 
not, you'll need to decide whether it's 4 or 5.  That matters.  At any rate, in your 
loop through TheLines(), you simply skip processing these.  (I'd probably advise 
also adding a test here to be sure the line isn't empty, and skip those too?)

I would suggest you create a series of flags that keep track of what section you're 
in, if the sections always follow the same pattern.  For instance:

  Const secJunk = 0
  Const secNames = 1
  Const secInterfaces = 2
  'etc
  Private m_Section As Long

> Next up are some 30 lines (has to be counted dynamicly) with the following
> formatting:
>
> name ipAddress hostAdress
> Example of the line formatted:
> name 169.154.0.1 server.myWorld.com
>
> name is static to all these lines.

Dim ThisLine As Variant

If m_Section <= secNames Then
   If InStr(1, TheLines(i), "name", vbTextCompare) = 1 Then
      ' Set section flag to Names section!
      m_Section = secNames
      ' Parse this line.
      ThisLine = Split(Trim$(TheLines(i)), " ")
      ipAddress = ThisLine(1)
      hostAddress = ThisLine(2)
      ' Do something with this data?
   End If
End IF

> Next up is interfaces, and they are separated by an exclamation mark before
> each "box" of text.
> Formatting:
> interface interfaceName
> nameif nameOfService
> security-level lvl
> ip address ipAddress hostMask
>
> Example:
> interface vlan123
> nameif Inside
> security-level 100
> ip address 169.154.0.1 255.255.255.0
>
> interface, nameif, security-level & ip address are static to this bulk of
> text.

Same pattern as last section.

If m_Section <= secInterfaces Then
   If InStr(1, TheLines(i), "interface", vbTextCompare) = 1 Then
      ' Set section flag to Names section!
      m_Section = secInterfaces
      ' Parse this line.
      ThisLine = Split(Trim$(TheLines(i)), " ")
      interfaceName = ThisLine(1)
      ' Parse next line.
      ThisLine = Split(Trim$(TheLines(i + 1)), " ")
      nameif = ThisLine(1)
      ' Parse next line.
      ThisLine = Split(Trim$(TheLines(i + 2)), " ")
      security-level = ThisLine(1)
      ' Parse next line.
      ThisLine = Split(Trim$(TheLines(i + 3)), " ")
      ipAddress = ThisLine(2)
      hostMask = ThisLine(3)
      ' Do something with this data?
   End If
End IF

You'll have to insure your naming scheme is logically consistent with the above 
assumptions, of course.

> Next are some object groups
> Formatting;
> object-group type GR-name
> network-object ipAddress HostMask
>
> Example:
> object-group network GR-internet
> network-object 169.154.0.0 255.255.255.0
> network-object 169.155.0.0 255.255.0.0
> port-object eq 123
> group-object GR-here-internett
> icmp-object echo-reply
>
> One object-group can contain an unknown amount of objects and other types of
> service descriptors.

Same pattern as before.  <g>

> My biggest issue is how to get this info into tables with a formatting &
> having it dynamicly update if the data.txt file is changed.

Now(!), you're talking about using Word.  Up until now, it's all been VB(A).  I 
don't know diddly about Word, so perhaps someone else can lend a hand here.

Have fun...
-- 
.NET: It's About Trust!
 http://vfred.mvps.org
date: Tue, 12 Feb 2008 16:42:17 -0800   author:   Karl E. Peterson

Re: Parsing a txt file into Word   
Excellent reply Karl. Thanks a lot for the insight on how to best solve this 
with with VB in Word. :)

Can anyone help out regarding the word part of this issue? How to insert the 
data gathered into tables?

-- 
Cato Larsen
HelpDesk Monkey
date: Tue, 12 Feb 2008 23:10:01 -0800   author:   Cato Larsen

Re: Parsing a txt file into Word   
You will need to declare a document object, a table object and a row object

Dim ddoc as Document
Dim dtable as Table
Dim drow as Row

then at the beginning create a new document with a one row table in it

Set ddoc = Documents.Add
set dtable = ddoc.Tables.Add(ddoc.Range, 1, n)

where n is the required number of columns,

Then, at the point where you have accumulated the data for one record, use

Set drow = dtables.Rows.Add

then for each piece of data use

drow.cells(i).Range.Text = the data

and increment i for each piece of data

Then repeat the adding of rows and populating of the cells in that row for 
each record.

-- 
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Cato Larsen"  wrote in message 
news:6E9DFC3E-03C8-4BC7-8BF2-88F5EE4F73EB@microsoft.com...
> Excellent reply Karl. Thanks a lot for the insight on how to best solve 
> this
> with with VB in Word. :)
>
> Can anyone help out regarding the word part of this issue? How to insert 
> the
> data gathered into tables?
>
> -- 
> Cato Larsen
> HelpDesk Monkey
>
date: Wed, 13 Feb 2008 21:46:37 +1000   author:   Doug Robbins - Word MVP

Re: Parsing a txt file into Word   
Excellent!

Thank you for the in-depth explanation. Will help me out a lot!
Time to get down and dirty and start working on this bugger.

Thank you all who contributed to this thread!

-- 
Cato Larsen
HelpDesk Monkey
date: Wed, 13 Feb 2008 04:44:00 -0800   author:   Cato Larsen

Google
 
Web ureader.com


    COPYRIGHT 2007, YARDI TECHNOLOGY LIMITED, ALL RIGHT RESERVE  |   contact us