PDA

View Full Version : macro for saving forms



psychologist
01-29-2013, 02:32 AM
Hi all.
I state that I don't know nothing about the VBA syntax, this is the first time I have to create a word macro. Here is my problem.

I have a lot of word survey files, each containing forms filled by subjects. Since now, I have manually exported the forms data by saving as txt and choosing the option "save form data as delimited text file". But doing this by hand is a long process and I would want to create a macro that automatically saves as delimited text file all the .doc documents in a given directory. The new txt files must have the same name as the original .doc files.

Someone could help me, considering that (as I stated) I have no knowledge about VBA?

Thanks in advance. :)

Andrea

psychologist
02-02-2013, 02:10 AM
I understand that perhaps this is a hard task. In any way, if I could create a simple macro button that save the data of the forms in my open document as .txt, in the same directory and with the same name as the .doc file, it would be great for me. I have tried with 'record macro', but it doesn't work.

macropod
02-02-2013, 02:33 AM
Have you done a forum/board search? This isn't the first time this kind of topic has been dealt with. Also, one usually only exports form data to input it somewhere else (eg an Excel workbook). There have been posts on automating that too.

psychologist
02-02-2013, 02:51 AM
Yes, indeed looking the other posts this is the code I wrote:

Sub Save_Forms_Txt()
'
'
'
Dim strDocName As String
Dim intPos As Integer

strDocName = ActiveDocument.Name
intPos = InStrRev(strDocName, ".")

strDocName = Left(strDocName, intPos - 1)
strDocName = strDocName & ".txt"

ActiveDocument.SaveFormsData = True
ActiveDocument.SaveAs2 FileName:=strDocName, FileFormat:=wdFormatText, _
LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _
:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _
SaveNativePictureFormat:=False, SaveFormsData:=True, SaveAsAOCELetter:= _
False, Encoding:=1252, InsertLineBreaks:=False, AllowSubstitutions:=False _
, LineEnding:=wdCRLF, CompatibilityMode:=0
End Sub

but it doesn't sanve nothing. Maybe is there an error. Any help?

macropod
02-02-2013, 03:01 AM
Try changing:
strDocName = ActiveDocument.Name
to:
strDocName = ActiveDocument.FullName

psychologist
02-02-2013, 03:10 AM
Great, it works! Thanks. :)

My .doc file names are 1.doc, 2.doc, 3.doc and so on. I suppose that, if I would want to do the same thing for (say) 20 files, without opening each one, I have to set a "for" cycle. Right?

macropod
02-02-2013, 03:40 AM
Yes, you'd need a loop. Try:
Sub ExtractFormData()
Application.ScreenUpdating = False
Dim strFolder As String, strFile As String, wdDoc As Document, strDocName As String
strFolder = GetFolder
If strFolder = "" Then Exit Sub
strFile = Dir(strFolder & "\*.doc", vbNormal)
While strFile <> ""
Set wdDoc = Documents.Open(FileName:=strFolder & "\" & strFile, AddToRecentFiles:=False, Visible:=False)
With wdDoc
strDocName = Left(.FullName, InStrRev(.FullName, ".")) & "txt"
.SaveAs2 FileName:=strDocName, FileFormat:=wdFormatText, AddToRecentFiles:=False, _
SaveFormsData:=True, Encoding:=1252, InsertLineBreaks:=False, LineEnding:=wdCRLF
.Close SaveChanges:=False
End With
strFile = Dir()
Wend
Set wdDoc = Nothing
Application.ScreenUpdating = True
End Sub
'
Function GetFolder() As String
Dim oFolder As Object
GetFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then GetFolder = oFolder.Items.Item.Path
Set oFolder = Nothing
End Function
Simply add the code to a document or template that isn't in the folder containing the forms.

psychologist
02-02-2013, 04:31 AM
Very very thanks macropod. It works perfectly! You saved me a lot of time.
Have a good day. :)

gmaxey
02-02-2013, 08:03 AM
Hopefully Paul won't mind. With a few modificaitons, you can adapt his code to process .doc, docx and docm files and save the converted files in a new sub-folder of the batch folder that you are processing. You will need a referenence to Microsoft Scripting RunTime in the project:

Option Explicit
Private Type typFolder
BatchFolder As String
ProcessedFolder As String
End Type
Private oFolders As typFolder
Sub ExtractFormData()
Dim oDoc As Document
Dim strFile As String, strFileNameNew As String
Application.ScreenUpdating = False
GetFolders
If oFolders.BatchFolder = "" Then GoTo lbl_Exit
strFile = Dir(oFolders.BatchFolder & "\*.doc*", vbNormal)
While strFile <> ""
Set oDoc = Documents.Open(FileName:=oFolders.BatchFolder & Application.PathSeparator & strFile, AddToRecentFiles:=False, Visible:=False)
With oDoc
strFileNameNew = oFolders.ProcessedFolder & Application.PathSeparator & Left(.Name, InStrRev(.Name, ".")) & "txt"
.SaveAs2 FileName:=strFileNameNew, FileFormat:=wdFormatText, AddToRecentFiles:=False, _
SaveFormsData:=True, Encoding:=1252, InsertLineBreaks:=False, LineEnding:=wdCRLF
.Close SaveChanges:=False
End With
strFile = Dir()
Wend
lbl_Exit:
Set oDoc = Nothing
Application.ScreenUpdating = True
Exit Sub
End Sub
Sub GetFolders()
Dim oFSO As FileSystemObject
Dim oFolder As Object
oFolders.BatchFolder = ""
oFolders.ProcessedFolder = ""
Set oFolder = CreateObject("Shell.Application").BrowseForFolder(0, "Choose a folder", 0)
If (Not oFolder Is Nothing) Then oFolders.BatchFolder = oFolder.Items.Item.Path
Set oFSO = CreateObject("Scripting.FileSystemObject")
oFolders.ProcessedFolder = oFolders.BatchFolder & Application.PathSeparator & "Processed"
If Not oFSO.FolderExists(oFolders.ProcessedFolder) Then
oFSO.CreateFolder oFolders.ProcessedFolder
End If
lbl_Exit:
Set oFSO = Nothing
Set oFolder = Nothing
Exit Sub
End Sub

macropod
02-02-2013, 10:11 PM
Hopefully Paul won't mind. With a few modificaitons, you can adapt his code to process .doc, docx and docm files and save the converted files in a new sub-folder of the batch folder that you are processing.
I don't mind at all, but:

if I could create a simple macro button that save the data of the forms in my open document as .txt, in the same directory and with the same name as the .doc file, it would be great for me.