Insérez un bouton de commande Contrôle ActiveX dans le document (Onglet Développeur, groupe Contrôles, bouton Outils hérités, rubrique Contrôles ActiveX).
Pour modifier le texte du bouton, cliquez sur Propriétés, puis mettez le texte plour la propriété Caption.
Alors que vous êtes toujours en mode Création, double-cliquez sur le bouton afin d'ouvrir Visual Basic. Puis collez les macros suivantes en fonction de votre type de formulaire.
Champs de formulaires hérités
Private Sub CommandButton1_Click()
Dim champ As FormField
For Each champ In ActiveDocument.FormFields
Do Until champ.Result <> ""
MsgBox "Vous devez remplir tous les champs du formulaire avant d'envoyer le fichier."
Exit Sub
Loop
Next
Call envoi
End Sub
Sub envoi()
Dim nfichier As String, nfichier2 As String, intpos As Byte, chemin As String
Dim ol As Object, monItem As Object, mondoc
Set ol = CreateObject("outlook.application")
Set monItem = ol.CreateItem(0)
If ActiveDocument.Path = vbNullString Then
MsgBox "Sauvegardez d'abord votre fichier"
Exit Sub
End If
chemin = ActiveDocument.Path & "\"
nfichier = ActiveDocument.Name
intpos = InStrRev(nfichier, ".")
nfichier = Left(nfichier, intpos - 1)
nfichier2 = nfichier & ".pdf"
ActiveDocument.ExportAsFixedFormat outputFileName:=chemin & nfichier2, _<br>exportFormat:=wdExportFormatPDF
monItem.To = "
monItem.Subject = "objet du mail"
monItem.Body = "Bonjour" & Chr(13) & Chr(13) & "Je vous prie de bien vouloir trouver blabla"
Set mondoc = monItem.Attachments
mondoc.Add chemin & nfichier2
monItem.Send
Set ol = Nothing
MsgBox "le fichier a bien été transmis."
Kill nfichier2
End Sub
Contrôles de contenu
Private Sub CommandButton1_Click()
Dim controle As ContentControl
Dim cont_texte
For Each controle In ActiveDocument.ContentControls
If controle.Type <> wdContentControlCheckBox Then
cont_texte = controle.Range
If cont_texte = controle.PlaceholderText Then
MsgBox "Vous devez remplir tous les champs du formulaire avant d'envoyer le fichier."
Exit Sub
End If
End If
Next
Call envoi
End Sub
Sub envoi()
Dim nfichier As String, nfichier2 As String, intpos As Byte, chemin As String
Dim ol As Object, monItem As Object, mondoc
Set ol = CreateObject("outlook.application")
Set monItem = ol.CreateItem(0)
If ActiveDocument.Path = vbNullString Then
MsgBox "Sauvegardez d'abord votre fichier"
Exit Sub
End If
chemin = ActiveDocument.Path & "\"
nfichier = ActiveDocument.Name
intpos = InStrRev(nfichier, ".")
nfichier = Left(nfichier, intpos - 1)
nfichier2 = nfichier & ".pdf"
ActiveDocument.ExportAsFixedFormat outputFileName:=chemin & nfichier2, _<br>exportFormat:=wdExportFormatPDF
monItem.To = "
monItem.Subject = "objet du mail"
monItem.Body = "Bonjour" & Chr(13) & Chr(13) & "Je vous prie de bien vouloir trouver blabla"
Set mondoc = monItem.Attachments
mondoc.Add chemin & nfichier2
monItem.Send
Set ol = Nothing
MsgBox "le fichier a bien été transmis."
Kill nfichier2
End Sub