0
Good morning,
I have following problem in a VBA code that I use in Excel to pull data from forms created in Word.
If the files. Docm are inside the same folder I can pull all the data I need from the forms without problem, the problem is that the number of files became mt large and it was necessary to separate them in subdirectories for a better organization.
Follow my code below:
Sub getWordFormData()
Dim wdApp As Object, myDoc As Object
Dim myFolder As String, strFile As String
Dim i As Long, j As Long
myFolder = ThisWorkbook.Path & "\"
If Len(Dir(myFolder)) = 0 Then
MsgBox myFolder & vbCrLf & "Not Found", vbInformation, "Cancelled - getWordFormData"
Exit Sub
End If
Application.ScreenUpdating = False
Set wdApp = CreateObject("Word.Application")
With ActiveSheet
.Cells.Clear
With .Range("A1:F1")
.Value = Array("Nome", "NIS", "CPF", "Endereço", "CEP", "Bairro")
.Font.Bold = True
End With
strFile = Dir(myFolder & "\*.docm", vbNormal)
i = 1
While strFile <> ""
i = i + 1
On Error Resume Next
Set myDoc = wdApp.Documents.Open(Filename:=myFolder & "\" & strFile, ReadOnly:=True, AddToRecentFiles:=False, Visible:=False)
.Cells(i, 1).Value = myDoc.txtNome.Text
.Cells(i, 2).Value = myDoc.txtNis.Text
.Cells(i, 3).Value = myDoc.txtCpf.Text
.Cells(i, 4).Value = myDoc.txtEnd.Text
.Cells(i, 5).Value = myDoc.txtCep.Text
.Cells(i, 6).Value = myDoc.Combobox1.Value
myDoc.Close SaveChanges:=False
strFile = Dir()
Wend
wdApp.Quit
Application.ScreenUpdating = True
End With
End Sub
Is there any way to modify this code so that it can fetch data from forms . Docm that are inside the subdirectories?
Edit
Example: I have the folder of the month of June and inside it I have 50 folders each with a form . Docm, wanted to run excel from inside the June folder and grab the data from the forms that are inside the subdirectories.
Thank you very much!!!
Change the directory path?
myFolder = ThisWorkbook.Path & "\"
formyFolder = ThisWorkbook.Path & "\Subdiretorio\"
, in whichThisWorkbook.Path
is the path of the Excel file. Or you can add a subdirectory variable, where it is possible to choose which folder is chosen. Or by typing manually, or with a combobox– danieltakeshi
Good morning, thank you for the suggestion. But what I need is for the code to search within several subdirectories automatically. For example: I have the folder for the month of June and within it I have 50 folders each with a form. Docm, wanted to run excel from inside the June folder and get the data of the forms from inside the subdirectories. I could understand?
– Fabricio Andrade