Ciao ragazzi, ho la necessità, su un evento click, di salvare un file di word al termine di ciascun ciclo for in modo che abbia il nome dei campi [cognome] [nome] presi da una tabella.
Ho il codice che segue, ma mi salva solo il primo file al termine del primo ciclo di for. Come posso risolvere il problema e salvare anche gli altri?
codice:
Private Sub Comando20_Click()
Dim i As Integer
'Aggiungo il contatore - MODIFICA LIMITE A SECONDA DEI RECORD'
For i = 1 To 3 Dim myApp As Word.Application, myDoc As Word.Document
Set myApp = CreateObject("Word.Application")
Set myDoc = myApp.Documents.Add(Template:="scheda2.dot")
Dim myData As DAO.Database, myRec As DAO.Recordset
Set myData = CurrentDb
'Compila il documento con i rispettivi campi'
Set myRec = myData.OpenRecordset("SELECT [cognome] FROM [esponenti] where ID like '" & i & "' ") myDoc.FormFields("cognome").Result = myRec![cognome]
Set myRec = myData.OpenRecordset("SELECT nome FROM esponenti where ID like '" & i & "' ") myDoc.FormFields("nome").Result = myRec![nome]
Set myRec = myData.OpenRecordset("SELECT cf FROM esponenti where ID like '" & i & "' ") myDoc.FormFields("cf").Result = myRec![cf]
Set myRec = myData.OpenRecordset("SELECT data FROM esponenti where ID like '" & i & "' ") myDoc.FormFields("data").Result = myRec![data]
ActiveDocument.SaveAs ("C:\Sound\" & cognome & " " & nome & ".doc")
myApp.Visible = True
Next
End Sub