Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / March 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Parachute Woman - 15 Mar 2006 13:26 GMT
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 - 15 Mar 2006 19:23 GMT
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
[quoted text clipped - 7 lines]
> 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
'_______________________________________

Signature

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

Parachute Woman - 16 Mar 2006 10:49 GMT
Thats absolutely brilliant!  Many thanks for explaining your solution
so clearly.
Jean-Guy Marcil - 16 Mar 2006 16:49 GMT
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!

Signature

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

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.