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. Anoniem
    2023-10-27T15:39:10+00:00

    Hi Jan,

    Thank you for your contribution.

    Could you be a little more specific where I should put this code and how to incorperate it into my own VBA code?

    I know how to work with subs, but functions are a bit new to me.

    Was dit antwoord nuttig?

    0 opmerkingen Geen opmerkingen
  2. Anoniem
    2023-10-27T09:54:07+00:00

    Probeer deze code

    Function fGetAccount(Naam As String) As Object

    Dim n As Integer

    Dim nAccounts As Integer

    If oOutlook Is Nothing Then fMakeOutlookobject

    On Local Error Resume Next

    nAccounts = oOutlook.Session.Accounts.Count

    If Err.Number > 0 Then

    Set oOutlook = Nothing 
    
    oOutlook = fMakeOutlookobject 
    
    nAccounts = oOutlook.Session.Accounts.Count 
    

    End If

    For n = 1 To nAccounts

    If oOutlook.Session.Accounts.Item(n).DisplayName = Naam Then 
    
      Set fGetAccount = oOutlook.Session.Accounts.Item(n) 
    
      Exit For 
    
    End If 
    

    Next

    End Function

    Set oAccount = oOutlook.Session.Accounts.Item(fGetAccount("Accountnaam"))

    Set M.SendUsingAccount = oAccount

    Was dit antwoord nuttig?

    0 opmerkingen Geen opmerkingen
  3. Anoniem
    2023-10-26T18:28:49+00:00

    Hi Sola,

    Thank you for your hard work.

    I reworked the VBA code to this, but Outlook is still using the standard account.

    Maybe I've made some mistakes, but your original VBA has been translated to Dutch.....

    VBA---

    Sub Email()

     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 
    
     Dim MailAccount As Object ' Object for Outlook's Account 
    
     Dim namespace As Object ' Object for the Outlook namespace 
    

    Application.ScreenUpdating = False

    ' Set up your preferred email account - change "YourAccountName" to the name of your account

     Set OutlookApp = CreateObject("Outlook.Application") 
    
     Set namespace = OutlookApp.GetNamespace("MAPI") 
    
     Set MailAccount = namespace.accounts.Item("kaartjes@????.nl") 
    

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

    For x = 2 To LR 'line 1 contains column headings

         ToName = Range("D" & x).Value  'email addresses' 
    
         DocPath = Range("K" & x).Value  'path to documents 
    
         DocName = Range("L" & x).Value  'document name' 
    

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

             Set OutlookMail = OutlookApp.CreateItem(0) 
    

    With OutlookMail

                 .SendUsingAccount = MailAccount  ' Specify the email account you want to use 
    
                 .Display 
    
                 .To = ToName 
    
                 .Subject = "Concert 2024 " & DocName 
    
                 .HTMLBody = Body & Attachment 
    
                 .Body = "Find your tickets attached." & vbNewLine & vbCrLf & "Best regards," & vbCrLf & "The Commision" 
    
                 .Attachments.Add DocPath & "\" & DocName & ".pdf" 
    
                 .Send 
    
             End With 
    

    Set OutlookMail = Nothing

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

         End If 
    
     Next x 
    

    Application.ScreenUpdating = True

    End Sub


    Was dit antwoord nuttig?

    0 opmerkingen Geen opmerkingen
  4. Anoniem
    2023-10-25T21:48:09+00:00

    Hoi Tony,

    Bedankt dat u contact met ons opneemt,

    Om op te geven welk e-mailaccount u in uw VBA-code wilt gebruiken voor het verzenden van e-mails via Outlook, kunt u de eigenschap 'SendUsingAccount' van het object 'MailItem' gebruiken. U kunt uw code als volgt wijzigen om een specifiek e-mailaccount te gebruiken:

    '''VBA Sub Email_Notification() OutlookApp dimmen als object OutlookMail dimmen als object ToName dimmen als tekenreeks CCName dimmen als tekenreeks DocPath dimmen als tekenreeks DocName dimmen als tekenreeks Dim x zo lang Dim LR zo lang MailAccount dimmen als object ' Object voor Outlook's Account Naamruimte dimmen als object ' Object voor de naamruimte van Outlook

    Application.ScreenUpdating = False

    ' Stel uw gewenste e-mailaccount in - wijzig "YourAccountName" in de naam van uw account OutlookApp instellen = CreateObject("Outlook.Application") Naamruimte instellen = OutlookApp.GetNamespace("MAPI") MailAccount = Namespace.Accounts.Item("YourAccountName") instellen

    LR = cellen(rijen.aantal, 1). Einde(xlUp). Roeien

    Voor x = 2 To LR 'regel 1 bevat kolomkoppen ToName = Bereik("D" & x). Waarde 'e-mailadressen' DocPath = Bereik("K" & x). Waarde 'pad naar documenten DocName = Bereik("L" & x). Waarde 'documentnaam'

    Als Bereik("M" & x). Waarde <> "Ja" dan OutlookMail instellen = OutlookApp.CreateItem(0)

    Met OutlookMail . SendUsingAccount = MailAccount ' Geef het e-mailaccount op dat u wilt gebruiken . Beeldscherm . Naar = ToName . Onderwerp = "Concert 2024 " & DocName . HTMLBody = "Vind uw tickets bijgevoegd." & vbNewLine & vbCrLf & "Met vriendelijke groet," & vbCrLf &; "De Commissie" . Bijlagen.DocPath toevoegen & "" &; DocName & ".pdf" . Verzenden Eindigen met

    OutlookMail instellen = Niets

    Bereik("M" & x). Waarde = "Ja" Einde als Volgende x

    Application.ScreenUpdating = Waar Einde sub

    
    In deze gewijzigde code geeft u eerst het e-mailaccount op dat u wilt gebruiken door de eigenschap 'SendUsingAccount' van het object 'OutlookMail' in te stellen op het 'MailAccount' dat u eerder hebt gedefinieerd. Wijzig "YourAccountName" in de naam van het e-mailaccount dat u wilt gebruiken. Op deze manier kunt u bepalen welk e-mailaccount de VBA-code gebruikt om e-mails via Outlook te verzenden.
    
    Ik hoop dat dit helpt!
    
    Groetjes
    Sola  
    
    *Deze reactie is automatisch vertaald. Hierdoor bevat het wellicht gramaticale fouten of vreemde zinnen.*
    

    Was dit antwoord nuttig?

    0 opmerkingen Geen opmerkingen