mail@pastecode.io avatar
a month ago
3.9 kB
    ' eDoc tool
Option Explicit

Sub Send_to_eDoc()
    ' This line makes the UserForm1 visible to the user.
    ' UserForm1 is a graphical interface that allows users to interact with the eDoc tool.
    ' When this subroutine is called, it triggers the display of UserForm1,
    ' enabling users to input or select information directly through the form's controls.
End Sub

Sub ProcessReferences(selectedCategory As String, selectedOptions As String, references As String)
    ' Declaration of variables to interact with Outlook.
    ' This includes creating objects for the application itself, its namespace, folders, and mail items.
    ' These objects are essential for accessing and manipulating Outlook data, such as emails and their properties.
    Dim objApp As Outlook.Application
    Dim objNamespace As Outlook.NameSpace
    Dim objFolder As Outlook.MAPIFolder
    Dim objItem As Outlook.mailItem
    Dim objMailItem As Outlook.mailItem
    Dim arrReferences As Variant
    Dim reference As String

    ' Initializes the Outlook application, obtains the MAPI namespace, and then retrieves the default Inbox folder.
    ' This setup is crucial for accessing emails within the user's Inbox, allowing the macro to interact with emails programmatically.
    Set objApp = New Outlook.Application
    Set objNamespace = objApp.GetNamespace("MAPI")
    Set objFolder = objNamespace.GetDefaultFolder(6) ' 6 corresponds to the olFolderInbox enumeration, targeting the Inbox folder.

    ' Attempts to assign the first selected email in the active Outlook window to objItem.
    ' Error handling is temporarily disabled to avoid runtime errors if no email is selected.
    ' This step is necessary to ensure that the macro operates on a user-selected email.
    On Error Resume Next
    Set objItem = objApp.ActiveExplorer.Selection.item(1)
    On Error GoTo 0

    ' Checks if an email has been selected. If not, the subroutine displays a message box to the user and exits,
    ' preventing further execution. This check is vital to ensure that the macro does not proceed without a selected email,
    ' which would lead to errors.
    If objItem Is Nothing Then
        MsgBox "No email selected. Exiting.", vbExclamation
        Exit Sub
    End If

    ' Splits the input string of references into an array for processing.
    ' This allows the macro to handle multiple references contained within a single string,
    ' separating them based on the newline character. Each reference can then be processed individually in subsequent steps.
    arrReferences = Split(references, vbCrLf)

    ' Iterates over each reference in the array. For each reference, a new mail item is created, addressed,
    ' and populated with information including the recipient's email, the subject composed of the selected category,
    ' options, and reference, and the original email attached as a .msg file. Finally, the new mail item is sent.
    ' This loop enables the macro to process and send multiple emails based on the array of references.
    Dim ref As Variant
    For Each ref In arrReferences
        ' Create new mail item
        Set objMailItem = objApp.CreateItem(0) ' olMailItem = 0
        ' Set the recipient's email address
        objMailItem.To = "cargowise@goodlogisticsgroup.com"

        ' Attach selected email as .msg file
        objItem.SaveAs Environ("TEMP") & "\" & "SelectedEmail.msg"
        objMailItem.Attachments.Add Environ("TEMP") & "\" & "SelectedEmail.msg"
        Kill Environ("TEMP") & "\" & "SelectedEmail.msg"

        ' Set email subject
        objMailItem.subject = "[EdiDocManager " & selectedCategory & " " & selectedOptions & " " & ref & "]"

        ' Send the email
    Next ref

    ' Hide the UserForm
End Sub
Leave a Comment