Great success!!
I did it in two steps:
- First, I added the filenames to my database. I used a textbox to enter the sucesive subdirectories and the program checked them to extract and record the paths (I could probably made something to recognize the subdirectories but it didn't worth it).
Code:
Private Sub cmdDir_Click()
Dim strDirRaiz As String
Dim strFF As String
Dim strDirectorio As String
Dim strFichero As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb()
strSQL = "SELECT tblDocumentos.Titulo, tblDocumentos.NombreArchivo, tblDocumentos.Referencia, tblDocumentos.Enlace FROM tblDocumentos"
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
strDirRaiz = "C:\mydir\"
strFF = Me.txtDir 'Name of the secondary dir.
strDirectorio = strDirRaiz & "\" & strFF
strFichero = Dir(strDirectorio & "\*.doc")
If strFichero = "" Then
Exit Sub
Else
rs.AddNew
rs!NombreArchivo = strDirectorio & "\" & strFichero
rs.Update
Do Until strFichero = ""
strFichero = Dir()
rs.AddNew
rs!NombreArchivo = strDirectorio & "\" & strFichero
rs.Update
'I believe there is a way to avoid repeating this lines, but I don't have time now.
Loop
End If
Set rs = Nothing
Set db = Nothing
End Sub
- Second, with all the paths in my database, open the docs and grab the text.
Code:
Dim strTitulo As String
Dim db As DAO.Database
Dim rs As DAO.Recordset
Dim strSQL As String
Set db = CurrentDb()
strSQL = "SELECT tblDocumentos.Titulo, tblDocumentos.NombreArchivo,..."
Set rs = db.OpenRecordset(strSQL, dbOpenDynaset)
Do While Not rs.EOF
Set mdocDoc = GetObject(rs!NombreArchivo)
mdocDoc.Application.Visible = True
mdocDoc.Activate
'This part is the action I wanted to perform in word.
Selection.MoveDown Unit:=wdLine, Count:=4
Selection.MoveUp Unit:=wdLine, Count:=1, Extend:=wdExtend
'end
Selection.Copy
strTitulo = Left(Selection, Len(Selection) - 1)
rs.Edit
rs!Titulo = strTitulo
rs.Update
mdocDoc.Close
Debug.Print strTitulo
rs.MoveNext
Loop
Set rs = Nothing
Set db = Nothing
And it worked. In a couple of minutes I recorded 336 docs, with no mistakes.
I know it can be improved, but I believe this is a very powerful tool, especially for those who deal with lot's of standard documents (ISO, i. eg.).
And I am very thankful to you Rod!
P.S.: I am looking forward to do it with pdf.