Hi all,
I'm trying to merge my Access data (specific fields) to a World template containing also the same specific fields.
The problem is that my code does not run and there's no any error message that appears. The button I put on the form will not react on the first click...but on the second click it opens the word application with the template document, but it does not fill the required fields.
Here's the code:
Private Sub Merge_Word_Click()
'Declare Word variables
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("C:\Documents and Settings\Mes documents\Documents Access\DEV\rappel2.doc", , True)
With doc
.FormFields("DESTINATAIRE").Result = DESTINATAIRE
.FormFields("ADRESSE").Result = ADRESSE
.FormFields("MUNICIPALITE").Result = MUNICIPALITE
.FormFields("PROVINCE").Result = PROVINCE
.FormFields("CODE_POSTAL").Result = CODE_POSTAL
.FormFields("NO_DOSSIER").Result = NO_DOSSIER
.FormFields("AGENT").Result = AGENT
.FormFields("NO_TEL_AGENT").Result = NO_TEL_AGENT
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub
I'm trying to merge my Access data (specific fields) to a World template containing also the same specific fields.
The problem is that my code does not run and there's no any error message that appears. The button I put on the form will not react on the first click...but on the second click it opens the word application with the template document, but it does not fill the required fields.
Here's the code:
Private Sub Merge_Word_Click()
'Declare Word variables
Dim appWord As Word.Application
Dim doc As Word.Document
'Avoid error 429, when Word isn't open.
On Error Resume Next
Err.Clear
'Set appWord object variable to running instance of Word.
Set appWord = GetObject(, "Word.Application")
If Err.Number <> 0 Then
'If Word isn't open, create a new instance of Word.
Set appWord = New Word.Application
End If
Set doc = appWord.Documents.Open("C:\Documents and Settings\Mes documents\Documents Access\DEV\rappel2.doc", , True)
With doc
.FormFields("DESTINATAIRE").Result = DESTINATAIRE
.FormFields("ADRESSE").Result = ADRESSE
.FormFields("MUNICIPALITE").Result = MUNICIPALITE
.FormFields("PROVINCE").Result = PROVINCE
.FormFields("CODE_POSTAL").Result = CODE_POSTAL
.FormFields("NO_DOSSIER").Result = NO_DOSSIER
.FormFields("AGENT").Result = AGENT
.FormFields("NO_TEL_AGENT").Result = NO_TEL_AGENT
.Visible = True
.Activate
End With
Set doc = Nothing
Set appWord = Nothing
Exit Sub
errHandler:
MsgBox Err.Number & ": " & Err.Description
End Sub