Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Text File help

Status
Not open for further replies.

mgkSHA

Programmer
Jul 12, 2002
126
0
0
US
Hello...I am trying to loop through a large folder with many Word templates...I need to extract the names of the boomarks, fields, and macros in these templates. I have the following steps done:

1) Function to recursively loop through the directories
2) A nested function to get the file attributes

I am not very good with writing to text files. What I want to do is simply take the data I am getting from the function and write to a text file. This is in a recursive loop, so the text file should keep writing lines until the directory is finished. I have attached the function where I get the file attributes: (again, this is nested in another recursive function)

[vb]

Public Function getAttributes(fn1 As String)


Dim s1, j, m As String
Dim r As Document
Dim obj As Object
Dim str As String



str = Right(fn1, 4)

On Error Resume Next

Application.ScreenUpdating = False


If ((str = ".dot") Or str = ".doc") Then

Documents.Open FileName:=fn1

With Documents(1)

For Each aBook In .Bookmarks
j = fn1 & " " & .Bookmarks(aBook).Name
Next aBook

For Each amacro In .VBProject.VBComponents
If ActiveDocument.VBProject.Protection = 0 Then
If amacro.Name <> &quot;This Document&quot; Then
m = fn1 & &quot; &quot; & amacro.Name
End If
End If

Next amacro

For Each aBar In .CommandBars
If aBar.BuiltIn = False Then
n = fn1 & &quot; &quot; & aBar.Name
End If
Next aBar


For Each aField In .Fields
s1 = fn1 & &quot; &quot; & .Fields(aField).Code
Next aField


End With

Documents(1).Close
End If

Application.ScreenUpdating = True
End Function

[/vb]

so basically, I want to write m, n, and s1 to a text file(the strings being populated in the for loops)..does anyone know how to do this? I really need some help here. Thanks
 
Dim intFile as Integer

' Gets next free file number
intFile = FreeFile

' Opens the file
Open &quot;OutputFilename&quot; For Output As #intFile

' Prints to the file
Print #intFile, &quot;YourTextHere&quot;

' Closes the file
Close intFile


Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;Why does my program keep showing error messages every time something goes wrong?&quot;
 
Andy:

I tried this, it does not work...see the code below, all I get is one line printed from the very first entry:

With Documents(1)


For Each aBook In .Bookmarks
j = fn1 & &quot; &quot; & .Bookmarks(aBook).Name
Open &quot;C:\worddocs.txt&quot; For Output As #ff
Print #ff, j
Close #ff
Next aBook

For Each amacro In .VBProject.VBComponents
If ActiveDocument.VBProject.Protection = 0 Then
If amacro.Name <> &quot;This Document&quot; Then
m = fn1 & &quot; &quot; & amacro.Name
Open &quot;C:\worddocs.txt&quot; For Output As #ff
Print #ff, m
Close #ff
End If
End If
Next amacro

For Each aBar In .CommandBars
If aBar.BuiltIn = False Then
n = fn1 & &quot; &quot; & aBar.Name
Open &quot;C:\worddocs.txt&quot; For Output As #ff
Print #ff, n
Close #ff
End If
Next aBar


For Each aField In .Fields
s1 = fn1 & &quot; &quot; & .Fields(aField).Code
Open &quot;C:\worddocs.txt&quot; For Output As #ff
Print #ff, s1
Close #ff
Next aField


End With

I thought this logic would work, but it fails terribly, hence I turned to the board...for whatever reason this is not looping and I not getting all four strings to enter the text file (and then recursively, do it again until the directory is over)...and ideas, I am stumped here
 
If you Open the file at the beginning of the routine (after &quot;With Documents(1)&quot;) and Close it at the end (just before &quot;End With&quot;), everything you Print should be there. If you want to Open a file and write more to it Open the file For Append rather than For Output. For Output creates a new file each time.


Andy
&quot;Logic is invincible because in order to combat logic it is necessary to use logic.&quot; -- Pierre Boutroux
&quot;Why does my program keep showing error messages every time something goes wrong?&quot;
 
Thanks Andy, it was the append feature..now it works
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top