Je souhaite insérer un tableau de données de la feuille Sheet1 de mon classeur Excel et ma signature par défaut.
J'ai essayé d'utiliser HTMLBody mais il affiche la signature avant que le tableau ne soit affiché ou rien du tout.
J'ai essayé de changer les positions du .HTMLBody.
Je dois envoyer un mail au format ci-dessous:
Voici le code.
Sub esendtable()
Dim outlook As Object
Dim newEmail As Object
Dim xInspect As Object
Dim pageEditor As Object
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
With newEmail
.To = "avc@123.com"
.CC = ""
.BCC = ""
.Subject = "Data - " & Date
.Body = "Please find below the data"
.Display
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
Sheet1.Range("B3:F3").Copy
pageEditor.Application.Selection.Start = Len(.Body)
pageEditor.Application.Selection.End =
pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdFormatPlainText)
.Display
'.Send
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Set newEmail = Nothing
Set outlook = Nothing
End Sub
3 Réponses :
Cela fonctionne pour moi
Sub esendtable()
Dim rng As Range
Dim Outlook As Object
Dim newEmail As Object
Dim SigString As String
Dim Signature As String
Dim xInspect As Object
Dim pageEditor As Object
Set rng = Nothing
On Error Resume Next
' Only send the visible cells in the selection.
Set rng = ActiveSheet.Range("A3:F3")
' You can also use a range with the following statement.
Set rng = Sheets("YourSheet").Range("A3:F3").SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If rng Is Nothing Then
MsgBox "The selection is not a range or the sheet is protected. " & _
vbNewLine & "Please correct and try again.", vbOKOnly
Exit Sub
End If
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set Outlook = CreateObject("Outlook.Application")
Set newEmail = Outlook.CreateItem(0)
SigString = "C:\Users\chipz\AppData\Roaming\Microsoft\Signatures\chipz_1.htm" ' Change chipz in path and signature file name
If Dir(SigString) <> "" Then
Signature = GetBoiler(SigString)
Else
Signature = ""
End If
On Error Resume Next
With newEmail
.To = "recipient@test.com"
.CC = ""
.BCC = ""
.Subject = "Data - " & Date
.BodyFormat = olFormatHTML
.HTMLBody = RangetoHTML(rng) & "" & Signature
.Display
' In place of the following statement, you can use ".Display" to
' display the e-mail message.
'.Send
End With
On Error GoTo 0
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
Set newEmail = Nothing
Set Outlook = Nothing
Set newEmail = Nothing
Set Outlook = Nothing
End Sub
Function RangetoHTML(rng As Range)
' Ron de Bruin
'
Dim fso As Object
Dim ts As Object
Dim TempFile As String
Dim TempWB As Workbook
TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"
'Copy the range and create a new workbook to past the data in
rng.Copy
Set TempWB = Workbooks.Add(1)
With TempWB.Sheets(1)
.Cells(1).PasteSpecial Paste:=8
.Cells(1).PasteSpecial xlPasteValues, , False, False
.Cells(1).PasteSpecial xlPasteFormats, , False, False
.Cells(1).Select
Application.CutCopyMode = False
On Error Resume Next
.DrawingObjects.Visible = True
.DrawingObjects.Delete
On Error GoTo 0
End With
'Publish the sheet to a htm file
With TempWB.PublishObjects.Add( _
SourceType:=xlSourceRange, _
Filename:=TempFile, _
Sheet:=TempWB.Sheets(1).Name, _
Source:=TempWB.Sheets(1).UsedRange.Address, _
HtmlType:=xlHtmlStatic)
.Publish (True)
End With
'Read all data from the htm file into RangetoHTML
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
RangetoHTML = ts.readall
ts.Close
RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
"align=left x:publishsource=")
'Close TempWB
TempWB.Close savechanges:=False
'Delete the htm file we used in this function
Kill TempFile
Set ts = Nothing
Set fso = Nothing
Set TempWB = Nothing
End Function
Function GetBoiler(ByVal sFile As String) As String
Dim fso As Object
Dim ts As Object
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.GetFile(sFile).OpenAsTextStream(1, -2)
GetBoiler = ts.readall
ts.Close
End Function
Oui, ça marche maintenant ... Merci beaucoup. Je suis un novice dans ce domaine. J'étais donc à la croisée des chemins. J'ai essayé beaucoup de choses. Merci, vous êtes un sauveur de vie.
Encore une petite question. comment ajouter plusieurs adresses dans la liste À à partir d'une feuille Excel?
@Chipz vous devrez parcourir la plage de cellules contenant les adresses e-mail et concaténer avec ; . Veuillez vous référer à mon article récent << a href = "https://stackoverflow.com/questions/54914673/adding-email-function-to-excel-search/54930845#54930845" title = "Ajout d'une fonction de messagerie à la recherche Excel"> stackoverflow.com/questions/54914673/... > pour plus de détails. En cas de difficulté, posez une nouvelle question.
Oui maintenant ça marche. Je n'ai pas pensé à la concaténation. Merci beaucoup.!!!! vous m'avez vraiment beaucoup aidé.
Vous pouvez gérer le corps de votre e-mail en
Outlook.CreateItem (olMailItem) .GetInspector.WordEditor.Range
Voici donc un simple extrait de code
With pageEditor.Range
.Collapse 1 ' wdCollapseStart
.InsertBefore "Hi Please find below the details" & vbCrLf
.Collapse 0 ' wdCollapseEnd
.InsertAfter "Text before signature" & vbCrLf
.Collapse 1 ' wdCollapseStart
Sheet1.Range("B3:F3").Copy
.Paste
'.PasteAndFormat 13 ' wdChartPicture
'.PasteAndFormat 22 ' wdFormatPlainText
End With
Si vous ajoutez une référence à "Microsoft Word xx Object Library" (et "Microsoft Outlook xx Object Library") pour une liaison anticipée, vous pouvez remplacer les nombres par les constantes Word ENUM correspondantes.
Vous pouvez utiliser mon code comme ci-dessous
Set outlook = CreateObject("Outlook.Application")
Set newEmail = outlook.CreateItem(0)
With newEmail
.display
signature = newEmail.HTMLBody
sig = HtmlToText(signature)
.To = ""
.CC = ""
.Subject = "Test"
.HTMLBody = "Dear team," & "<br>" & "<br>" & "Please check and fix the issue below. Thank you!"
Set xInspect = newEmail.GetInspector
Set pageEditor = xInspect.WordEditor
wb.Sheets(1).Range("a1:h" & lr).SpecialCells(xlCellTypeVisible).Copy
pageEditor.Application.Selection.Start = Len(.body)
pageEditor.Application.Selection.End = pageEditor.Application.Selection.Start
pageEditor.Application.Selection.PasteAndFormat (wdformatplaintext)
.display
.HTMLBody = .HTMLBody & signature
Set pageEditor = Nothing
Set xInspect = Nothing
End With
Avez-vous envisagé de l'inclure sur la feuille avec la table et de la déplacer en une seule fois?
Oui, j'ai essayé ça. Mais ça n'a pas marché.
Comment ajouter plusieurs adresses e-mail dans
To, cliquez ici pour vous inspirer: stackoverflow.com/q/54219147/ 10908769