Doorgaan naar hoofdcontent

Excel: Outlook aanroepen en ook daadwerkelijk openen met VBA

Om vanuit Excel met VBA een e-mail te versturen is niet zo ingewikkeld. Code is overal te vinden. Ik toon hier een voorbeeld van de website van Ron de Bruin:

Sub Mail_small_Text_Outlook()
'For Tips see: http://www.rondebruin.nl/win/winmail/Outlook/tips.htm
'Working in Office 2000-2013
    Dim OutApp As Object
    Dim OutMail As Object
    Dim strbody As String

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

    strbody = "Hi there" & vbNewLine & vbNewLine & _
              "This is line 1" & vbNewLine & _
              "This is line 2" & vbNewLine & _
              "This is line 3" & vbNewLine & _
              "This is line 4"

    On Error Resume Next
    With OutMail
        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .Body = strbody
        'You can add a file like this
        '.Attachments.Add ("C:\test.txt")
        .Send   'or use .Display
    End With
    On Error GoTo 0

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub

Als we Display gebruiken wordt wel de Outlook e-mail getoond maar niet Outlook zelf. Als we bijvoorbeeld vanuit Excel een Word document willen openen en Word ook echt willen tonen, kunnen we zoiets gebruiken:

Set wordapp = CreateObject("word.Application")
wordapp.documents.Open "D:\data\hessel.docx"
wordapp.Visible = True

Voor verder Excel tips klik hier.
Een simpel instellen van Visible op True volstaat. Om Outlook ook echt te openen, moeten we veel meer kluchten uithalen. Met onderstaande code lukt dit:

'outlook aanroepen en mailtje maken
    On Error Resume Next
    Set OutApp = GetObject(, "Outlook.Application")
    If Err.Number <> 429 Then
        'onderstaande code maakt Outlook ook echt zichtbaar te maken in combinatie met activate
        With GetObject(, "Outlook.application")
            Set objNSP = .GetNamespace("MAPI").GetDefaultFolder(6).Items.Count
        End With
    End If
    
    
    'als Outlook niet geopend, outlook openen en aanroepen
    If Err.Number = 429 Then
        Set OutApp = CreateObject("Outlook.Application")
        'onderstaande code maakt Outlook ook echt zichtbaar te maken in combinatie met activate
        With CreateObject("Outlook.application")
            Set objNSP = .GetNamespace("MAPI")
            Set objFld = objNSP.GetDefaultFolder(6) ' 6 = olFolderInbox
            Set objExp = .Explorers.Add(objFld, 0) ' 0 = olFolderDisplayNormal
        End With
    End If

    'om Outlook ook echt te tonen
    objExp.Activate
        
    'email item maken
    Set OutMail = OutApp.CreateItem(0)

    'versturen mail
    With OutMail
        .To = "hesseldewalle@gmail.com"
        .CC = ""
        .BCC = ""
        .Subject = ""
        .Body = ""
        'om de email te tonen
        .display
    End With

Reacties

Populaire posts van deze blog

Friesland: nu ruim 1500 Friese familiewapens doorzoekbaar

Inleiding Het totale overzicht is nu ook als PDF te downloaden: rptWapens Al jaren verzamel ik de teksten van grafzerken en andere voorwerpen om ze in een database bijeen te brengen. Op heel veel oude grafzerken, rouwborden of andere voorwerpen in Friesland treffen we naast tekst ook vaak familiewapens aan. Ruim een jaar geleden ben ik gestart met het koppelen van familiewapens aan deze teksten. Met behulp van deze database kunnen we nu steeds meer voorheen onbekende wapens thuisbrengen of andersom, initialen herleiden tot de bijbehorende namen. Een voorbeeld. Zo kennen we uit het boek Fries Zilver nummer 894 met de tekst: TDH RBB 1669 Tjitte D. Wijngaarden geboren 6 october 1874 Daarbij een mannen- en een vrouwenwapen. Mannenwapen blijkt dat van Hoitinga te zijn. De letters blijken vervolgens te herleiden tot Tjepke Douwes Hoitinga en Rienkje Bouwes Bruinsma uit Nijland. Vermoedelijk getrouwd in 1669! Opgelost. Ander voorbeeld. Op een lepel uit het boek Dokk...

Excel: VBA script om wachtwoord te verwijderen

Af en toe krijg ik een vraag om een wachtwoord van een Excel blad te halen. Doodsimpel met VBA. Hier een script dat ik gebruik: Sub WachtwoordCrack()     Dim a As Integer, b As Integer, c As Integer, d As Integer, _     e As Integer, f As Integer, g As Integer, h As Integer, _  I As Integer, j As Integer, k, m As Integer     Dim begin As Date, eind As Date     Dim duur As String     Dim objSheet As Worksheet     begin = TimeValue(Time)     On Error Resume Next     For Each objSheet In Application.Worksheets         For a = 65 To 66: For b = 65 To 66: For c = 65 To 66             For d = 65 To 66: For e = 65 To 66: For f = 65 To 66                 For g = 65 To 66: For h = 65 To 66: For I = 65 To 66                     For j = 65 To 66: For k = 65 To...

Excel: Creating Your Own Tabs With XML And VBA

In order to create you own nice looking tabs you need the Custom UI Editor for Microsoft Office. You can download it for free. How is this different from choosing File , Options , Customize Ribbon and adding a new tab with new groups and then commands to the group? You can also move the new tab to any position. T hen your new tab is always there. Even when you don't open the specific file with the right VBA. In my example the outcome looks like this, a nice tab, placed in front of the Home Tab. You will only get this tab when you start the specific example (which you can download). The XML behind this: <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui">    <ribbon startFromScratch="false">     <tabs>       <tab id="MaxIlze" label="Dossier Overzicht" insertBeforeMso="TabHome">         <group id="customGroup1" label="Adressen verrijken">         ...