Создание шаблона электронной почты с переменными получателями в VBA - PullRequest
0 голосов
/ 04 июля 2019

Я пытаюсь реализовать код в VBA, который будет отправлять электронную почту различным получателям в зависимости от 3 различных условий.Если условие 1 выполнено, то электронное письмо будет отправлено на электронную почту01, а .CC будет отправлено на электронную почту02, если условие 2 выполнено, то электронное письмо будет отправлено на электронную почту03 и CC email04 и т. Д. Так что, по сути, единственное, что может изменитьсяявляется.В & .CC в зависимости от условия где-то в коде

До сих пор я создал шаблон электронной почты в VBA, который обрабатывает приложение Outlook, используя ниже.

Sub CreatingEmailTemplate()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)


        strbody = "Sample Text" & "<br><br>" & _
                    "Sample TextSample TextSample TextSample TextSample TextSample TextSample Text" & "<br><br>" & _
                    "Sample TextSample Text" & "<br><br><br>" & _
                    "Sample TextSample TextSample TextSample" & "<br>" & _
                    "Sample Text" & "<br>" & _
                    "Sample Text" & "<br>" & _
                    "Sample TextSample TextSample TextSample TextSample Text" & "<br>" & _
                    "Sample Text" & "<br>" & _
                    "Sample Text " & "<br>" & _
                    "Sample TextSample Text" & "<br><br><br>" & _
                    "<b>Sample TextSample TextSample TextSample TextSample Text" & "<br>" & _
                    "Sample TextSample TextSample TextSample TextSample TextSample Text<b>"


    On Error Resume Next

  'Trying to implement a variable condition. depending on what condition is met the recipient of sending emails (To & CC) would change 

    With OutMail
        .To = "SampleEmail01@abc.com"
        .CC = "SampleEmail02@abc.com" 
        .BCC = ""
        .Subject = "Sending email to different recipients"
        .htmlBody = strbody
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        'OutMail.Display True
        .Display  'or use .Send to send automatically
    End With

    On Error GoTo 0
    'MsgBox "Email Send"

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Все работает отлично, и я полагаю, что могу использовать 3 разных модуля для обработки 3 разных условий, но я надеялся, что смогу объединить все в одном коде.Есть идеи?

1 Ответ

0 голосов
/ 04 июля 2019

Вы можете установить to и cc по переменной и вызвать весь метод с ним, как

Sub CreatingEmailTemplate(to_recipients  as String, cc_recipients  as string)

...

With OutMail
    .To = to_recipients  
    .CC = cc_recipients  
    .BCC = ""
    .Subject = "Sending email to different recipients"
    .htmlBody = strbody
    'You can add a file like this
    '.Attachments.Add ("C:\test.txt")
    'OutMail.Display True
    .Display  'or use .Send to send automatically
End With

или

Sub CreatingEmailTemplate()

dim to_recipients  as String
dim cc_recipients  as string

if (condition1=true) then
    to_recipients = "email01"
    cc_recipients = "email02"
elseif (condition2=true) then
    to_recipients = "email03"
    cc_recipients = "email04"
elseif (condition3=true) then
    to_recipients = "email01"
    cc_recipients = "email04"
else

end if

...

With OutMail
    .To = to_recipients  
    .CC = cc_recipients  
    .BCC = ""
    .Subject = "Sending email to different recipients"
    .htmlBody = strbody
    'You can add a file like this
    '.Attachments.Add ("C:\test.txt")
    'OutMail.Display True
    .Display  'or use .Send to send automatically
End With
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...