Outlook account kiezen VBA Excel

Anoniem
2023-10-25T18:32:37+00:00

Ik heb een lijst in Excel met documenten (PDF) die ik wil verzenden naar mailadressen die eveneens in die lijst staan.

Onderstaande VBA code werkt prima, maar ik heb 2 accounts in Outlook en ik wil er eentje kunnen specificeren.

Ik heb al wat geprobeerd met Set .SendUsingAccount, maar ik kom er niet uit.

Hoe en waar doe ik dat?

Sub Email_Notification()

Dim OutlookApp  As Object 

Dim OutlookMail As Object 

Dim ToName      As String 

Dim CCName      As String 

Dim DocPath As String 

Dim DocName As String 

Dim x           As Long 

Dim LR          As Long 

Application.ScreenUpdating = False 

LR = Cells(Rows.Count, 1).End(xlUp).Row 

For x = 2 To LR 'regel 1 bevat kolomkoppen

    ToName = Range("D" & x).Value 'mailadressen

    DocPath = Range("K" & x).Value  'pad naar documenten

    DocName = Range("L" & x).Value 'documentnaam

If Range("M" & x).Value <> "Yes" Then

            Set OutlookApp = CreateObject("Outlook.application") 

            Set OutlookMailitem = OutlookApp.CreateItem(0) 

            With OutlookMailitem 

                .Display 

                .To = ToName 

                .Subject = "Concert 2024 " & DocName 

                .HTMLBody = Body & Attachment 

                .Body = "Gelieve bijgaand uw kaartjes aan te treffen." & vbNewLine & vbCrLf & "Met vriendelijke groet," & vbCrLf & "De commissie" 

                .Attachments.Add DocPath & "\" & DocName & ".pdf" 

                .send 

            End With 

            Set OutlookMail = Nothing 

            Set OutlookApp = Nothing 

            Range("M" & x).Value = "Yes" 

        Else 

        End If 

Next x 

Application.ScreenUpdating = True 

End Sub

Microsoft 365 en Office | Excel | Voor thuisgebruik | Windows

Vergrendelde vraag. Deze vraag is gemigreerd vanuit de Microsoft Ondersteuning-community. U kunt met een stem aangeven of de inhoud nuttig is, maar u kunt geen opmerkingen of antwoorden toevoegen of de vraag volgen.

0 opmerkingen Geen opmerkingen
Antwoord geaccepteerd door vraagauteur
Anoniem
2023-10-30T11:54:07+00:00

I got it to work.

Essential is that in Excel VBA editor at Tools -> References -> Microsoft Outlook 16.0 Object Library is ticked!

Sub StuurKaartjes()

Dim ol As Outlook.Application

Dim mi As Outlook.MailItem

Dim ToName As String

Dim DocPath As String

Dim DocName As String

Dim x As Long

Dim LR As Long

Application.ScreenUpdating = False 

LR = Cells(Rows.Count, 1).End(xlUp).Row 

For x = 2 To LR 'regel 1 bevat kolomkoppen 

If Range("M" & x).Value <> "Yes" Then

    ToName = Range("D" & x).Value 'mailadressen 

    DocPath = Range("K" & x).Value  'pad naar documenten 

    DocName = Range("L" & x).Value 'documentnaam 

            Set ol = New Outlook.Application 

            Set mi = ol.CreateItem(olMailItem) 

            mi.Display 

            mi.Subject = "Test" 

            mi.To = ToName 

            mi.Body = "Testing 123" 

            mi.Attachments.Add DocPath & "\" & DocName & ".pdf" 

            mi.SendUsingAccount = ol.Session.accounts("******@test.nl") 

            mi.Send 

            Range("M" & x).Value = "Yes" 

Else

End If

Next x 

            Set ol = Nothing 

            Set mi = Nothing 

Application.ScreenUpdating = True 

End Sub

To be sure there are different accounts present, you can check it by running this code in VBA editor.

Option Explicit

Sub ListAccountNames()

Dim ol As Outlook.Application

Dim ac As Outlook.account

Set ol = New Outlook.Application

For Each ac In ol.Session.accounts

Debug.Print ac.DisplayName

Next ac

End Sub

After running it in the pane "Direct" the results are shown.

Was dit antwoord nuttig?

0 opmerkingen Geen opmerkingen

6 extra antwoorden

Sorteren op: Meest nuttig
  1. Verwijderd

    Dit antwoord is verwijderd vanwege een schending van onze gedragscode. Het antwoord is handmatig gerapporteerd of geïdentificeerd via geautomatiseerde detectie voordat er actie werd ondernomen. Raadpleeg onze Gedragscode voor meer informatie.


    Opmerkingen zijn uitgeschakeld. Meer informatie

  2. Anoniem
    2023-10-27T19:36:40+00:00

    Ik heb zoiets gehad/opgelost in Access VBA, maar het principe blijft hetzelfde. Mijn code is:

    Private Sub mail_Click() 
    
    If IsNull(Me.E_MAIL_ADRES) Then Exit Sub 
    
    Dim objOutlook As Outlook.Application 
    
    Dim objEmail As Outlook.MailItem 
    
    Dim oOutlookAccount As Outlook.Account 
    
    Set objOutlook = CreateObject("Outlook.Application") 
    
    Set objEmail = objOutlook.CreateItem(olMailItem) 
    
    For Each oOutlookAccount In objOutlook.Session.Accounts 
    
        If oOutlookAccount.DisplayName = "******@yyy.zzz" Then 
    
            objEmail.SendUsingAccount = oOutlookAccount 
    
        End If 
    
    Next 
    
    objEmail.To = Me.E_MAIL_ADRES 
    
    objEmail.BodyFormat = olFormatHTML 
    
    objEmail.HTMLBody = "<HTML><BODY><FONT SIZE=2 FACE='Verdana'>" & "Hallo " & Me.Voornaam & ",<br><br>" & _ 
    
        "Met vriendelijke groeten,<br><br>Namens het bestuur van xxxxxx""<br>" & _ 
    
        "Peter<br>Secretaris" & _ 
    
        "</FONT></BODY></HTML>" 
    
    objEmail.Display 
    
    Set objOutlook = Nothing 
    
    Set objEmail = Nothing 
    
    Set oOutlookAccount = Nothing 
    
    End Sub
    

    Vervang ******@yyy.zzz door het gewenste e-mailadres.

    Was dit antwoord nuttig?

    0 opmerkingen Geen opmerkingen