Meilleur auteur de réponses
Comment créer un tableau dans le corps du mail outlook 2010

Question
-
Bonjour,
Je veux envoyer un mail par Outlook 2010 dans lequel j'insère un tableau créé dynamiquement via VBA (Access). Mais comment procéder?
- Injection d'un fichier html => mais COMMENT ?
- Copie dans le corps du mail Outlook 2010 d'un document Excel formaté via VBA => mais
COMMENT ?
- Autre méthode => c'est possible ?
Merci par avance!
- Injection d'un fichier html => mais COMMENT ?
Réponses
-
J'ai fait une petite verification et ceci fonctionne pour moi
Sub emailFromoDoc() Dim oWord As Object Dim oWordEditor As Object Dim oDoc As Object Dim oOutlook As Object Dim oMail As Object Const olFormatRichText = 3 On Error GoTo Error_Handler Set oWord = CreateObject("Word.Application") Set oDoc = oWord.Documents.Open("C:\Users\Daniel\Desktop\test.Docx") oDoc.Content.Copy oDoc.Close Set oOutlook = CreateObject("Outlook.Application") Set oMail = oOutlook.CreateItem(0) With oMail .BodyFormat = olFormatRichText Set oWordEditor = .GetInspector.WordEditor oWordEditor.Content.Paste .Display End With Error_Handler_Exit: On Error Resume Next If Not oMail Is Nothing Then Set oMail = Nothing If Not oOutlook Is Nothing Then Set oOutlook = Nothing If Not oDoc Is Nothing Then Set oDoc = Nothing If Not oWord Is Nothing Then Set oWord = Nothing Exit Sub Error_Handler: MsgBox "The following error has occured" & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Source: emailFromoDoc" & vbCrLf & _ "Error Description: " & Err.Description & _ Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _ , vbOKOnly + vbCritical, "An Error has Occured!" Resume Error_Handler_Exit End Sub
Mais si tu fait ceci pour avoir une table avec les données provenant d'Access, il y a bien plus facile que ça.
Daniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net- Modifié Daniel Pineault (MVP)MVP mercredi 4 avril 2018 10:27
- Marqué comme réponse technet65 lundi 16 avril 2018 06:39
-
Çeci marhce pour moi.
Sub SendTable()
Dim sMsg As StringsMsg = sMsg & " <style>" & vbCrLf
sMsg = sMsg & " th.Col1{background-color: #ffcc33;}" & vbCrLf
sMsg = sMsg & " </style>" & vbCrLf
sMsg = sMsg & " <table>" & vbCrLf
sMsg = sMsg & " <tr>" & vbCrLf
sMsg = sMsg & " <th class=""Col1"">Colonne 1</th>" & vbCrLf
sMsg = sMsg & " <th>Colonne 2</th>" & vbCrLf
sMsg = sMsg & " <th>Colonne 3</th>" & vbCrLf
sMsg = sMsg & " <th>Colonne 4</th>" & vbCrLf
sMsg = sMsg & " </tr>" & vbCrLf
sMsg = sMsg & " <tr>" & vbCrLf
sMsg = sMsg & " <td>1,1</td>" & vbCrLf
sMsg = sMsg & " <td>1,2</td>" & vbCrLf
sMsg = sMsg & " <td>1,3</td>" & vbCrLf
sMsg = sMsg & " <td>1,4</td>" & vbCrLf
sMsg = sMsg & " </tr>" & vbCrLf
sMsg = sMsg & " <tr>" & vbCrLf
sMsg = sMsg & " <td>2,1</td>" & vbCrLf
sMsg = sMsg & " <td>2,2</td>" & vbCrLf
sMsg = sMsg & " <td>2,3</td>" & vbCrLf
sMsg = sMsg & " <td>2,4</td>" & vbCrLf
sMsg = sMsg & " </tr>" & vbCrLf
sMsg = sMsg & " </table>" & vbCrLfDebug.Print sMsg
Call SendHTMLEmail("quelquun@...", "Test Table", sMsg, True)
End SubDaniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net- Marqué comme réponse technet65 lundi 16 avril 2018 06:39
Toutes les réponses
-
Pour faire l'envoie d'un courriel avec Outlook d'Access regarde la function http://www.devhut.net/2010/09/03/vba-send-html-emails-using-outlook-automation/
ensuite tu lui passe un message en format html du contenu pour le message.
Es-tu familier avec le html pour créer une table?
Sais-tu itérer à travers une table en VBA?Daniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net -
Bonjour Daniel,
Merci pour ta réponse mais il n'y a aucune explication sur HTMLBody pour ajouter un tableau.
D'autre part, j'essaye de copier/coller un document word dans le corps d'un mail sans succès car j'ai le message "error 91: object variable and with block variable not set" dans le code suivant:
Function test()
Dim oOutlook As Object
Dim wd As Object
Dim oInspector As Outlook.Inspector
Dim doc As Object
Dim oMail As MailItem
Dim sPathFile As String
Set wd = CreateObject("Word.Application")
sPathFile = "D:\VBA\SESSION.docx"
Set doc = wd.documents.Open(sPathFile)
doc.Content.Copy
wd.Quit
Set wd = Nothing
Set oOutlook = CreateObject("Outlook.Application")
Set oMail = oOutlook.CreateItem(0)
With oMail
.BodyFormat = olFormatRichText
Set oInspector = .GetInspector.WordEditor '----> Null
oInspector.Content.Paste --------------------------> Error 91 ce qui est normal puisque oInspector = null
.Display
End With
Set oMail = Nothing
Set oOutlook = Nothing
End FunctionMerci pour ton aide
-
.HTMLBody permet de passer du code HTML brut. Tu dois ainsi la créer, ça peut-être n'importe quell code.
Par exemple:
sHTML = "
<table>
Colonne 1
<tr>
<th></th>
Colonne 2
<th></th>
Colonne 3
<th></th>
Colonne 4
<th></th>
"
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>'...
.HTMLBody = sHTML
Tu profiterais peut-être de lire https://www.w3schools.com/html/html_intro.asp pour apprendre les principles de base du HTML.
Daniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net- Modifié Daniel Pineault (MVP)MVP mercredi 4 avril 2018 10:10
-
Est-ce que tu as Word configure comme éditeur pour tes courriels?
Une autre approche est de faire la sauvegarde du document word comme fichier HTML puis copier le code HTML généré et l'envoyer au .HTMLBody. Tu peux le faire en VBA et supprimer le fichier une fois l'envoie du courriel est complèté. Regarde la dernière réponse dans la conversation suivante https://www.experts-exchange.com/questions/23606380/Copy-Word-Doc-to-Body-of-Email.html
Daniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net -
J'ai fait une petite verification et ceci fonctionne pour moi
Sub emailFromoDoc() Dim oWord As Object Dim oWordEditor As Object Dim oDoc As Object Dim oOutlook As Object Dim oMail As Object Const olFormatRichText = 3 On Error GoTo Error_Handler Set oWord = CreateObject("Word.Application") Set oDoc = oWord.Documents.Open("C:\Users\Daniel\Desktop\test.Docx") oDoc.Content.Copy oDoc.Close Set oOutlook = CreateObject("Outlook.Application") Set oMail = oOutlook.CreateItem(0) With oMail .BodyFormat = olFormatRichText Set oWordEditor = .GetInspector.WordEditor oWordEditor.Content.Paste .Display End With Error_Handler_Exit: On Error Resume Next If Not oMail Is Nothing Then Set oMail = Nothing If Not oOutlook Is Nothing Then Set oOutlook = Nothing If Not oDoc Is Nothing Then Set oDoc = Nothing If Not oWord Is Nothing Then Set oWord = Nothing Exit Sub Error_Handler: MsgBox "The following error has occured" & vbCrLf & vbCrLf & _ "Error Number: " & Err.Number & vbCrLf & _ "Error Source: emailFromoDoc" & vbCrLf & _ "Error Description: " & Err.Description & _ Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _ , vbOKOnly + vbCritical, "An Error has Occured!" Resume Error_Handler_Exit End Sub
Mais si tu fait ceci pour avoir une table avec les données provenant d'Access, il y a bien plus facile que ça.
Daniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net- Modifié Daniel Pineault (MVP)MVP mercredi 4 avril 2018 10:27
- Marqué comme réponse technet65 lundi 16 avril 2018 06:39
-
Merci Daniel pour ton retour.
J'ai essayé ce simple code html généré sur BlueGriffon
"<table style="width: 1662px; height: 74px;">
<tbody>
<tr>
<th style="background-color: #ffcc33;">Colonne 1</th>
</tr>
</tbody>
</table>"Mais la commande <thstyle="background-color:#ffcc33;">Colonne1</th> n'est absolument pas interprétée dans Outlook 2010.
J'ai lu par ailleurs que Outlook 2010 a comme moteur HTML celui de WORD et donc n'interprète pas du tout le html standard.
Sur les technos web, j'ai quelques notions j'en ai d'ailleurs profité pour développer mon site personnel Voir ici (html, css,javascript, php)
- Modifié technet65 samedi 14 avril 2018 10:13
-
Çeci marhce pour moi.
Sub SendTable()
Dim sMsg As StringsMsg = sMsg & " <style>" & vbCrLf
sMsg = sMsg & " th.Col1{background-color: #ffcc33;}" & vbCrLf
sMsg = sMsg & " </style>" & vbCrLf
sMsg = sMsg & " <table>" & vbCrLf
sMsg = sMsg & " <tr>" & vbCrLf
sMsg = sMsg & " <th class=""Col1"">Colonne 1</th>" & vbCrLf
sMsg = sMsg & " <th>Colonne 2</th>" & vbCrLf
sMsg = sMsg & " <th>Colonne 3</th>" & vbCrLf
sMsg = sMsg & " <th>Colonne 4</th>" & vbCrLf
sMsg = sMsg & " </tr>" & vbCrLf
sMsg = sMsg & " <tr>" & vbCrLf
sMsg = sMsg & " <td>1,1</td>" & vbCrLf
sMsg = sMsg & " <td>1,2</td>" & vbCrLf
sMsg = sMsg & " <td>1,3</td>" & vbCrLf
sMsg = sMsg & " <td>1,4</td>" & vbCrLf
sMsg = sMsg & " </tr>" & vbCrLf
sMsg = sMsg & " <tr>" & vbCrLf
sMsg = sMsg & " <td>2,1</td>" & vbCrLf
sMsg = sMsg & " <td>2,2</td>" & vbCrLf
sMsg = sMsg & " <td>2,3</td>" & vbCrLf
sMsg = sMsg & " <td>2,4</td>" & vbCrLf
sMsg = sMsg & " </tr>" & vbCrLf
sMsg = sMsg & " </table>" & vbCrLfDebug.Print sMsg
Call SendHTMLEmail("quelquun@...", "Test Table", sMsg, True)
End SubDaniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net- Marqué comme réponse technet65 lundi 16 avril 2018 06:39
-
Essaie http://www.devhut.net/2018/04/04/ms-access-use-word-document-as-outlook-e-mail-body/
Daniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net -
Merci une nouvelle Daniel pour ton aide précieuse et dont les solutions fonctionnent à la perfection..
-
Pour faire l'envoie d'un courriel avec Outlook d'Access regarde la function http://www.devhut.net/2010/09/03/vba-send-html-emails-using-outlook-automation/
ensuite tu lui passe un message en format html du contenu pour le message.
Es-tu familier avec le html pour créer une table?
Sais-tu itérer à travers une table en VBA?
Daniel Pineault, 2010-2017 Microsoft MVP
Professional Support: http://www.cardaconsultants.com
MS Access Tips and Code Samples: http://www.devhut.net -
- Modifié technet65 mardi 17 avril 2018 15:26