Macro : Lire une donnée d'une PJ pour l'intégrer dans un texte email.

roidurif

XLDnaute Occasionnel
Bonjour,

Cette Macro me permet de répondre à tous a un email avec PJ, et en plus de cela, écrire une message automatique avec l'historique du mail.

OBJECTIF
=======
Mon souhait est d'intégrer au message automatique une donnée située en celulle ("H26") de la Pièce Jointe.

J'arrive bien à lire la donnée lors de l'extration du fichier, mais je n'arrive pas à l'ajouter au texte :

Code:
Bonjour,

Votre fiche "celulle ("H26")" est actuellement disponible." 

Cordialement

Cette celulle est appellée dans le code :

Code:
Set A = appxl.Range("H26")

Je vous remercie infiniement de l'aide que vous pouvez m'apporter.

Voci mon code :
Code:
Sub Repondre_email_avec_PJ()

Dim myItem As Outlook.MailItem
Dim myAttachments As Outlook.Attachments
Dim i As Long
Dim Att As String
Dim ol As New Outlook.Application
Dim olMail As MailItem
Dim CurrFile As String
Dim Pj As String
Dim OLinbox As Outlook.MAPIFolder '***
Dim myItems As Outlook.Selection '**

On Error Resume Next
Select Case TypeName(Application.ActiveWindow)
Case "Explorer"
Set myItem = ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set myItem = ActiveInspector.CurrentItem
Case Else
End Select
On Error GoTo 0

If myItem Is Nothing Then
GoTo ExitProc
End If


Set olApp = New Outlook.Application '***
Set olSpace = olApp.GetNamespace("MAPI") '*
Set OLinbox = olSpace.GetDefaultFolder(olFolderInbox) '***
Set myDestFolder = OLinbox.Folders("3- TRAITER") '**
Set myItems = ol.ActiveExplorer.Selection '***


Set myAttachments = myItem.Attachments


Set ol = New Outlook.Application
Set outExplorer = Application.ActiveExplorer
Set olMail = outExplorer.Selection.Item(1).ReplyAll
strLink = "http://Site.net"



With olMail


.Subject = "Re:" & myItem.Subject
.Body = "Bonjour," & vbCrLf & vbCrLf & "Votre fiche est actuellement disponible." & vbCrLf & A & vbCrLf & "Cordialement," & vbCrLf & vbCrLf _
& vbCrLf & vbCrLf & vbCrLf _
& "Retrouvez tout sur le site" & " " & strLink & vbCrLf _
& "---------------------------------------------------------------------------------------------------------------------" & vbCrLf & vbCrLf & myItem.Body
End With

CopyAttachments myItem, olMail
olMail.Display

For Each myItem In myItems
myItem.Move myDestFolder
Next myItem


ExitProc:
Set myAttachments = Nothing
Set myItem = Nothing
End Sub

Sub CopyAttachments(objSourceItem, objTargetItem)
   
   Set appxl = CreateObject("Excel.application") '**
    
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
   strPath = fldTemp.Path & "\"
   For Each objAtt In objSourceItem.Attachments
      strFile = strPath & objAtt.FileName
      
      If Right(objAtt.FileName, 5) = ".xlsx" Or Right(objAtt.FileName, 4) = ".xls" Or Right(objAtt.FileName, 4) = ".zip" Or Right(objAtt.FileName, 5) = ".xlsm" Then
      objAtt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
      
      appxl.Workbooks.Open strFile '**
      appxl.Visible = False '**
      Set A = appxl.Range("H26") '**
      Debug.Print A '**
      appxl.Workbooks.Close '**
      
      fso.DeleteFile strFile
      
      End If
   Next
   
   Set appxl = Nothing
   Set fldTemp = Nothing
   Set fso = Nothing
End Sub
 
G

Guest

Guest
Re : Macro : Lire une donnée d'une PJ pour l'intégrer dans un texte email.

Bonjour,

Rajout d'une variable string de niveau module TextCell.
Déplacement de l'appel à copyAttachments.

Code:
[COLOR=blue]Dim[/COLOR] [COLOR=red][B]TextCell[/B][/COLOR] [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
[COLOR=blue]Sub[/COLOR] Repondre_email_avec_PJ()
[COLOR=blue]Dim[/COLOR] myItem [COLOR=blue]As[/COLOR] Outlook.MailItem
[COLOR=blue]Dim[/COLOR] myAttachments [COLOR=blue]As[/COLOR] Outlook.Attachments
[COLOR=blue]Dim[/COLOR] i [COLOR=blue]As[/COLOR] [COLOR=blue]Long[/COLOR]
[COLOR=blue]Dim[/COLOR] Att [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
[COLOR=blue]Dim[/COLOR] ol [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] Outlook.Application
[COLOR=blue]Dim[/COLOR] olMail [COLOR=blue]As[/COLOR] MailItem
[COLOR=blue]Dim[/COLOR] CurrFile [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
[COLOR=blue]Dim[/COLOR] Pj [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
[COLOR=blue]Dim[/COLOR] OLinbox [COLOR=blue]As[/COLOR] Outlook.MAPIFolder [COLOR=green]'***[/COLOR]
[COLOR=blue]Dim[/COLOR] myItems [COLOR=blue]As[/COLOR] Outlook.Selection [COLOR=green]'**[/COLOR]
[COLOR=blue]On[/COLOR] [COLOR=blue]Error[/COLOR] [COLOR=blue]Resume[/COLOR] [COLOR=blue]Next[/COLOR]
[COLOR=blue]Select[/COLOR] [COLOR=blue]Case[/COLOR] TypeName(Application.ActiveWindow)
[COLOR=blue]Case[/COLOR] [I]"Explorer"[/I]
[COLOR=blue]Set[/COLOR] myItem = ActiveExplorer.Selection.Item(1)
[COLOR=blue]Case[/COLOR] [I]"Inspector"[/I]
[COLOR=blue]Set[/COLOR] myItem = ActiveInspector.CurrentItem
[COLOR=blue]Case[/COLOR] [COLOR=blue]Else[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Select[/COLOR]
[COLOR=blue]On[/COLOR] [COLOR=blue]Error[/COLOR] [COLOR=blue]GoTo[/COLOR] 0
[COLOR=blue]If[/COLOR] myItem [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR]
[COLOR=blue]GoTo[/COLOR] ExitProc
[COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
 
[COLOR=blue]Set[/COLOR] olApp = [COLOR=blue]New[/COLOR] Outlook.Application [COLOR=green]'***[/COLOR]
[COLOR=blue]Set[/COLOR] olSpace = olApp.GetNamespace([I]"MAPI"[/I]) [COLOR=green]'*[/COLOR]
[COLOR=blue]Set[/COLOR] OLinbox = olSpace.GetDefaultFolder(olFolderInbox) [COLOR=green]'***[/COLOR]
[COLOR=blue]Set[/COLOR] myDestFolder = OLinbox.Folders([I]"3- TRAITER"[/I]) [COLOR=green]'**[/COLOR]
[COLOR=blue]Set[/COLOR] myItems = ol.ActiveExplorer.Selection [COLOR=green]'***[/COLOR]
 
[COLOR=blue]Set[/COLOR] myAttachments = myItem.Attachments
 
[COLOR=blue]Set[/COLOR] ol = [COLOR=blue]New[/COLOR] Outlook.Application
[COLOR=blue]Set[/COLOR] outExplorer = Application.ActiveExplorer
[COLOR=blue]Set[/COLOR] olMail = outExplorer.Selection.Item(1).ReplyAll
strLink = [I][url=http://Site.net]site.net[/url][/I]
 
[I]'Appel de CopyAttachments avant la constitution du Body 'initialiser la  variable TextCell[/I]
 
[I]CopyAttachments myItem, olMail[/I]
 
[I][COLOR=blue]With[/COLOR] olMail[/I]
 
[I].Subject = [I]"Re:"[/I] & myItem.Subject[/I]
[I].Body = [I]"Bonjour,"[/I] & vbCrLf & vbCrLf & [I]"Votre fiche "[/I] & [SIZE=3][COLOR=red]TextCell[/COLOR][/SIZE] & [I]" est actuellement disponible."[/I] & vbCrLf & A & vbCrLf & [I]"Cordialement,"[/I] & vbCrLf & vbCrLf _[/I]
[I]& vbCrLf & vbCrLf & vbCrLf _[/I]
[I]& [I]"Retrouvez tout sur le site"[/I] & [I]" "[/I] & strLink & vbCrLf _[/I]
[I]& [I]"---------------------------------------------------------------------------------------------------------------------"[/I] & vbCrLf & vbCrLf & myItem.Body[/I]
[I][COLOR=blue]End[/COLOR] [COLOR=blue]With[/COLOR][/I]
[I]olMail.Display[/I]
[I][COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] myItem [COLOR=blue]In[/COLOR] myItems[/I]
[I]myItem.Move myDestFolder[/I]
[I][COLOR=blue]Next[/COLOR] myItem[/I]
 
[I]ExitProc:[/I]
[I][COLOR=blue]Set[/COLOR] myAttachments = [COLOR=blue]Nothing[/COLOR][/I]
[I][COLOR=blue]Set[/COLOR] myItem = [COLOR=blue]Nothing[/COLOR][/I]
[I][COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/I]
[I][COLOR=blue]Sub[/COLOR] CopyAttachments(objSourceItem, objTargetItem)[/I]
 
[I]  [COLOR=blue]Set[/COLOR] appxl = CreateObject([I]"Excel.application"[/I]) [COLOR=green]'**[/COLOR][/I]
 
[I]  [COLOR=blue]Set[/COLOR] fso = CreateObject([I]"Scripting.FileSystemObject"[/I])[/I]
[I]  [COLOR=blue]Set[/COLOR] fldTemp = fso.GetSpecialFolder(2) [COLOR=green]' TemporaryFolder[/COLOR][/I]
[I]  strPath = fldTemp.Path & [I]"\"[/I][/I]
[I]  [COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] objAtt [COLOR=blue]In[/COLOR] objSourceItem.Attachments[/I]
[I]     strFile = strPath & objAtt.Filename[/I]
 
[I]     [COLOR=blue]If[/COLOR] Right(objAtt.Filename, 5) = [I]".xlsx"[/I] [COLOR=blue]Or[/COLOR] Right(objAtt.Filename, 4) = [I]".xls"[/I] [COLOR=blue]Or[/COLOR] Right(objAtt.Filename, 4) = [I]".zip"[/I] [COLOR=blue]Or[/COLOR] Right(objAtt.Filename, 5) = [I]".xlsm"[/I] [COLOR=blue]Then[/COLOR][/I]
[I]     objAtt.SaveAsFile strFile[/I]
[I]     objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName[/I]
 
[I]     appxl.Workbooks.[COLOR=blue]Open[/COLOR] strFile [COLOR=green]'**[/COLOR][/I]
[I]     appxl.Visible = [COLOR=blue]False[/COLOR] [COLOR=green]'**[/COLOR][/I]
      [SIZE=3][COLOR=red]TextCell[/COLOR][/SIZE] = appxl.Range([I]"H26"[/I]).Text [COLOR=green]'**[/COLOR]
      [COLOR=blue]Debug[/COLOR].[COLOR=blue]Print[/COLOR] A [COLOR=green]'**[/COLOR]
      appxl.Workbooks.[COLOR=blue]Close[/COLOR] [COLOR=green]'**[/COLOR]
 
      fso.DeleteFile strFile
 
      [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
   [COLOR=blue]Next[/COLOR]
 
   [COLOR=blue]Set[/COLOR] appxl = [COLOR=blue]Nothing[/COLOR]
   [COLOR=blue]Set[/COLOR] fldTemp = [COLOR=blue]Nothing[/COLOR]
   [COLOR=blue]Set[/COLOR] fso = [COLOR=blue]Nothing[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR]
 

Discussions similaires

Membres actuellement en ligne

Aucun membre en ligne actuellement.

Statistiques des forums

Discussions
314 628
Messages
2 111 337
Membres
111 104
dernier inscrit
JEMADA