Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Save Word form fields as seperate text files, looping through folder

3 views
Skip to first unread message

Parachute Woman

unread,
Mar 15, 2006, 7:26:30 AM3/15/06
to
Hi, I have been trying unsucessfully to manipulate the following code
for my purposes. Could anyone please help? I am trying to loop
through a folder with Word files that are application forms. These
need to be exported (using save forms data) to seperate text files with
the same name as the Word files. When I run this piece of code, two
problems come up -

1) If it works, then I have to press "Save" and then "Insert line
breaks etc." for the text file that is produced. I will eventually be
dealing with a few hundred applications, so pressing two buttons
everytime will be quite tedious.

2) Error - cannot save forms data as the same name as active document
... my text files need to be of the same name!

Many thanks for all your help.

(TO GET WORD FILES INTO TEXT FILES)

Sub CompileFormData()
' Macro recorded 8/19/01 by Debra Dalgleish
Dim strDir As String
Dim i As Integer
strDir = "C:\Documents and Settings\Desktop\Experiment Folder"
Application.DisplayAlerts = wdAlertsNone
With Application.FileSearch
.NewSearch
.LookIn = strDir
.SearchSubFolders = False
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Documents.Open (.FoundFiles(i))
ActiveDocument.SaveFormsData = True
Application.DefaultSaveFormat = ""
ChangeFileOpenDirectory "C:\Documents and
Settings\Desktop\Experiment Folder\text_files"
ActiveDocument.SaveAs FileName:=ActiveDocument.Name, _
FileFormat:=wdFormatText, SaveFormsData:=True
ActiveWindow.Close
Next i
End If
End With


strDir = "C:\Documents and Settings\Desktop\Experiment
Folder\text_files"
With Application.FileSearch
.NewSearch
.LookIn = strDir
.SearchSubFolders = False
If .Execute() > 0 Then
For i = 1 To .FoundFiles.Count
Documents.Open (.FoundFiles(i))
Selection.WholeStory
Selection.Copy
ActiveWindow.Close
Selection.EndKey Unit:=wdStory
Selection.Paste
Next i
End If
End With
Application.DisplayAlerts = wdAlertsAll


End Sub

Jean-Guy Marcil

unread,
Mar 15, 2006, 1:23:19 PM3/15/06
to
Parachute Woman was telling us:
Parachute Woman nous racontait que :

> Hi, I have been trying unsucessfully to manipulate the following code
> for my purposes. Could anyone please help? I am trying to loop
> through a folder with Word files that are application forms. These
> need to be exported (using save forms data) to seperate text files
> with the same name as the Word files. When I run this piece of code,
> two problems come up -
>
> 1) If it works, then I have to press "Save" and then "Insert line
> breaks etc." for the text file that is produced. I will eventually be
> dealing with a few hundred applications, so pressing two buttons
> everytime will be quite tedious.

You had some problems mainly because:

ActiveDocument.SaveAs FileName:=ActiveDocument.Name, _
FileFormat:=wdFormatText, SaveFormsData:=True
ActiveWindow.Close

If you specify a different file format (.txt) but give Word a "*.doc" name
(ActiveDocument.Name), Word will create a doc file, not a txt file.

It is also bad practice to rely on ActiveDocument and ActiveWindow to point
to the right place when dealing with multiple documents. Always use Document
objects. See my code, I use 3 document objects so when I write the code I
always know which document I am dealing with.

Also, for the mostly same reason, avoid using the selection object.


> 2) Error - cannot save forms data as the same name as active document
> ... my text files need to be of the same name!

Already answered above.

Also, your code as it was would not have opened the txt files. You got that
far because you were creating doc files instead of txt files.
This is why I added:
.FileName = "*.txt"
Otherwise, Application.FileSearch use the current setting in the user Open
Dialog setting.

Finally, see how I record the user preferred setting for confirming
conversion, then change it to my need and finally set it back to its
original setting (Which may or may not have been the same), also notice how
I did not use the Selection object to get the text form the txt file into
the main file.

'_______________________________________
Sub CompileFormData()

Const strDir As String = "X:\Office 2003"
Const strDirTest As String = strDir & "\Test"
Dim i As Integer
Dim Doc As Document
Dim CurDoc As Document
Dim TextDoc As Document
Dim boolUserOpenFormat As Boolean

boolUserOpenFormat = Options.ConfirmConversions

Set CurDoc = ActiveDocument

With Application.FileSearch
.NewSearch
.LookIn = strDir
.SearchSubFolders = False
If .Execute() > 0 Then

ChangeFileOpenDirectory strDirTest


For i = 1 To .FoundFiles.Count

Set Doc = Documents.Open(.FoundFiles(i))
With Doc
.SaveFormsData = True
.SaveAs FileName:=Left(.Name, Len(.Name) - 4) & ".txt", _
FileFormat:=wdFormatText, SaveFormsData:=True
.Close
End With


Next i
End If
End With

With Application.FileSearch
.NewSearch
.LookIn = strDirTest
.SearchSubFolders = False
.FileName = "*.txt"
If .Execute() > 0 Then
Options.ConfirmConversions = False


For i = 1 To .FoundFiles.Count

Set TextDoc = Documents.Open(.FoundFiles(i))
CurDoc.Range.InsertAfter TextDoc.Range.Text & vbCrLf
TextDoc.Close
Next i
Options.ConfirmConversions = boolUserOpenFormat
End If
End With

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarci...@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org


Parachute Woman

unread,
Mar 16, 2006, 4:49:05 AM3/16/06
to
Thats absolutely brilliant! Many thanks for explaining your solution
so clearly.

Jean-Guy Marcil

unread,
Mar 16, 2006, 10:49:39 AM3/16/06
to
Parachute Woman was telling us:
Parachute Woman nous racontait que :

> Thats absolutely brilliant! Many thanks for explaining your solution
> so clearly.

No problem, glad I could help.

I forgot to mention one tiny detail.... this is a userform group (for things
that look like dialog boxes), so you should have posted in the vba.general
group instead...

Just thought I'd let you know!

0 new messages