Guest User

Untitled

a guest
Jun 19th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr)
  2. Sub ResendSelectedReport()
  3.     ' Works in Outlook 2013/2016
  4.    Dim objOL As Outlook.Application
  5.     Dim currentExplorer As Explorer
  6.     Dim Selection As Selection
  7.     Dim myItem As Object
  8.     Dim reportItem As Outlook.reportItem
  9.     Dim objInsp, oInspector As Outlook.Inspector
  10.     Dim olResendMsg As Object ' Using Object to handle both ReportItem and MailItem
  11.    Dim ItemCollection As New Collection
  12.     Dim i As Integer
  13.     Dim ns As Outlook.NameSpace
  14.     Dim inbox As Outlook.MAPIFolder
  15.     Dim resentFolder As Outlook.MAPIFolder
  16.    
  17.     ' Initialize Outlook application object
  18.    On Error GoTo ErrorHandler
  19.     Set objOL = Outlook.Application
  20.     Set currentExplorer = objOL.ActiveExplorer
  21.     Set Selection = currentExplorer.Selection
  22.     Set ns = objOL.GetNamespace("MAPI")
  23.     Set inbox = ns.GetDefaultFolder(olFolderInbox)
  24.     Set resentFolder = GetFolder(inbox, "ResentToUMA") ' Function to get or create folder
  25.    
  26.     ' Check if the ResentToUMA folder exists; if not, create it
  27.    If resentFolder Is Nothing Then
  28.         Set resentFolder = inbox.Folders.Add("ResentToUMA", olFolderInbox)
  29.     End If
  30.    
  31.     On Error GoTo 0
  32.  
  33.     ' Debugging to check the number of selected items
  34.    Debug.Print "Number of selected items: " & Selection.Count
  35.  
  36.     ' Store the selected items in a collection to avoid selection issues
  37.    For Each myItem In Selection
  38.         ' Check if the selected item is a ReportItem
  39.        If TypeOf myItem Is Outlook.reportItem Then
  40.             ItemCollection.Add myItem
  41.             Debug.Print "Added ReportItem to collection: " & myItem.Subject
  42.         Else
  43.             Debug.Print "Selected item is not a ReportItem."
  44.         End If
  45.     Next myItem
  46.  
  47.     ' Debugging to check the number of items added to the collection
  48.    Debug.Print "Number of items in collection: " & ItemCollection.Count
  49.  
  50.     ' Loop through each item in the collection
  51.    For i = 1 To ItemCollection.Count
  52.         Set reportItem = ItemCollection(i)
  53.  
  54.         ' Display the report item to run the resend command
  55.        reportItem.Display
  56.  
  57.         ' Get the Inspector for the displayed item
  58.        Set objInsp = reportItem.GetInspector
  59.  
  60.         ' Execute the "Send Again" command in Report
  61.        objInsp.CommandBars.ExecuteMso ("SendAgain")
  62.        
  63.         'A new window opens, where you can Edit and Send
  64.        Set objInsp = Application.ActiveWindow
  65.         'Resend finally
  66.        objInsp.CommandBars.ExecuteMso ("SendDefault")
  67.        
  68.         'Close Report
  69.        reportItem.Close olDiscard
  70.         ' Move the resent report item to ResentToUMA folder
  71.        reportItem.Move resentFolder
  72.  
  73.         ' Close the original report item without saving changes
  74.         reportItem.Close olDiscard
  75.  
  76.         Debug.Print "[" & i & "/" & ItemCollection.Count & "] RESENT - " & CStr(reportItem.CreationTime) & " - " & reportItem.Subject & ""
  77.        
  78.         'Sleep 1000
  79.    Next i
  80.  
  81. exitproc:
  82.     ' Clean up
  83.    Set reportItem = Nothing
  84.     Set objInsp = Nothing
  85.     Set olResendMsg = Nothing
  86.  
  87.     Debug.Print "Process completed."
  88.     Exit Sub
  89.  
  90. ErrorHandler:
  91.     Debug.Print "Error: " & Err.Number & " - " & Err.Description
  92.     Resume exitproc
  93. End Sub
  94.  
  95. Function GetFolder(parentFolder As Outlook.MAPIFolder, folderName As String) As Outlook.MAPIFolder
  96.     ' Function to get a subfolder of parentFolder by name; create if it doesn't exist
  97.    Dim subFolder As Outlook.MAPIFolder
  98.     On Error Resume Next
  99.     Set subFolder = parentFolder.Folders(folderName)
  100.     On Error GoTo 0
  101.     If subFolder Is Nothing Then
  102.         Set subFolder = parentFolder.Folders.Add(folderName)
  103.     End If
  104.     Set GetFolder = subFolder
  105. End Function
  106.  
  107.  
  108.  
Advertisement
Add Comment
Please, Sign In to add comment