• Privacywetgeving
    Het is bij Helpmij.nl niet toegestaan om persoonsgegevens in een voorbeeld te plaatsen. Alle voorbeelden die persoonsgegevens bevatten zullen zonder opgaaf van reden verwijderd worden. In de vraag zal specifiek vermeld moeten worden dat het om fictieve namen gaat.

Mail VBA dmv gezamelijke mailbox

vrouw

Terugkerende gebruiker
Lid geworden
27 mrt 2010
Berichten
1.534
Hallo,

Ik gebruik een VBA script van Ron de Bruin om mail te versturen vanuit Excel.
Met het CDO script werkt het niet meer dus doe ik het nu met "Mail Range/Selection in the body of the mail" op deze site.

Het probleem is dat de mail nu vanuit de persoonlijke outlook mailbox word verzonden.

is deze aan te passen dat de mail vanuit de gezamelijk mailbox word verzonden?
 
Code:
.SendUsingAccount = Session.Accounts.Item("gezamelijk@provider.nl")
 
Thanks voor je snelle reactie.

Waar ga ik die regel neerzetten?
Bericht automatisch samengevoegd:

Het werkt niet als ik de regel hier (ROOD gearceerd) neerzet.
(oh dat werkt niet maar staat nu onder "With OutMail"
Hij blijft mijn persoonlijke naam invullen in het FROM: veld in outlook.

Code:
Sub Mail_Selection_Range_Outlook_Body()
'For Tips see: https://jkp-ads.com/rdb/win/winmail/Outlook/tips.htm
'Don't forget to copy the function RangetoHTML in the module.
'Working in Excel 2000-2016
    Dim rng As Range
    Dim OutApp As Object
    Dim OutMail As Object

    Set rng = Nothing
    On Error Resume Next
    'Only the visible cells in the selection
    Set rng = Selection.SpecialCells(xlCellTypeVisible)
    'You can also use a fixed range if you want
    Set rng = Sheets("YourSheet").Range("D4:D12").SpecialCells(xlCellTypeVisible)
    On Error GoTo 0

    If rng Is Nothing Then
        MsgBox "The selection is not a range or the sheet is protected" & _
               vbNewLine & "please correct and try again.", vbOKOnly
        Exit Sub
    End If

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
    End With

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

    On Error Resume Next

    With OutMail

.SendUsingAccount = Session.Accounts.Item ("mailadres@test.nl")

        .To = "ron@debruin.nl"
        .CC = ""
        .BCC = ""
        .Subject = "This is the Subject line"
        .HTMLBody = RangetoHTML(rng)
        .Send   'or use .Display
    End With
    On Error GoTo 0

    With Application
        .EnableEvents = True
        .ScreenUpdating = True
    End With

    Set OutMail = Nothing
    Set OutApp = Nothing
End Sub


Function RangetoHTML(rng As Range)
' Changed by Ron de Bruin 28-Oct-2006
' Working in Office 2000-2016
    Dim fso As Object
    Dim ts As Object
    Dim TempFile As String
    Dim TempWB As Workbook

    TempFile = Environ$("temp") & "\" & Format(Now, "dd-mm-yy h-mm-ss") & ".htm"

    'Copy the range and create a new workbook to past the data in
    rng.Copy
    Set TempWB = Workbooks.Add(1)
    With TempWB.Sheets(1)
        .Cells(1).PasteSpecial Paste:=8
        .Cells(1).PasteSpecial xlPasteValues, , False, False
        .Cells(1).PasteSpecial xlPasteFormats, , False, False
        .Cells(1).Select
        Application.CutCopyMode = False
        On Error Resume Next
        .DrawingObjects.Visible = True
        .DrawingObjects.Delete
        On Error GoTo 0
    End With

    'Publish the sheet to a htm file
    With TempWB.PublishObjects.Add( _
         SourceType:=xlSourceRange, _
         Filename:=TempFile, _
         Sheet:=TempWB.Sheets(1).Name, _
         Source:=TempWB.Sheets(1).UsedRange.Address, _
         HtmlType:=xlHtmlStatic)
        .Publish (True)
    End With

    'Read all data from the htm file into RangetoHTML
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set ts = fso.GetFile(TempFile).OpenAsTextStream(1, -2)
    RangetoHTML = ts.readall
    ts.Close
    RangetoHTML = Replace(RangetoHTML, "align=center x:publishsource=", _
                          "align=left x:publishsource=")

    'Close TempWB
    TempWB.Close savechanges:=False

    'Delete the htm file we used in this function
    Kill TempFile

    Set ts = Nothing
    Set fso = Nothing
    Set TempWB = Nothing
End Function
 
Laatst bewerkt:
Er mist nog iets aan de coderegel van @AHulpje
Zet er Set voor.
 
De .SendUsingAccount verwacht een object, niet een string.
Bij deze een voorbeeld met foutcontrole:
Code:
Sub MailViaAccount()
    Dim OutApp As Object
    Dim OutMail As Object
    Dim OutAccount As Object
    Dim MailVia As String
 
    'Gebruik dit account
    MailVia = "edmoor@helpmij.nl"
 
    Set OutApp = CreateObject("Outlook.Application")
 
    'Bepaal het te gebruiken verzender account ---------------------
    For i = 1 To OutApp.Session.Accounts.Count
        If OutApp.Session.Accounts.Item(i) = MailVia Then
            Set OutAccount = OutApp.Session.Accounts.Item(i)
        End If
    Next i
    If OutAccount Is Nothing Then
        MsgBox "Het account " & MailVia & " werd niet gevonden", vbCritical, "Account niet gevonden"
        Exit Sub
    End If
    '----------------------------------------------------------------
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = "iemand@helpmij.nl"
        Set .SendUsingAccount = OutAccount
        .CC = ""
        .BCC = ""
        .Subject = "Dit is het onderwerp"
        .Body = "Bij deze het bestand"
        '.Attachments.Add Bestand
        .Display
    End With
End Sub
 
@edmoor

Hij kan het account niet vinden.

1757065042352.png

Ik plaats dat account (mailadres) toch hier?
1757065112753.png

Terwijl het adres hier wel bij staat op de 2e regel

1757065601587.png
 
Laatst bewerkt:
De code die ik plaatste is goed, dat gebruik ik zelf ook.
Dan bestaat het opgegeven account niet in Outlook.
In MailVia zet je alleen het email adres.
 
Hoe bedoel je dat ie niet in outlook bestaat.

Ik kan `m handmatig gewoon opzoeken
 
Ik bedoel dat de code het opgegeven account niet kan vinden.
Wat de melding al zegt.
 
Handmatig kan ik gewoon een mail versturen vanaf dat account, waarom vind ie `m dan niet met de macro?

Wat betekend dit?

Code:
  'Bepaal het te gebruiken verzender account ---------------------
    For I = 1 To OutApp.Session.Accounts.Count
        If OutApp.Session.Accounts.Item(I) = MailVia Then
            Set OutAccount = OutApp.Session.Accounts.Item(I)
        End If
 
Geen idee, die macro werkt hier al jaren zonder enig probleem.
 
Deze (met early binding, handig voor intellisense) werkt bij mij prima, maar je moet natuurlijk wel bestaande e-mailadressen gebruiken en een verwijzing naar de Microsoft Outlook Object Library:
Code:
Sub Mailmetafzender()
    Dim OutApp As Outlook.Application
    Set OutApp = New Outlook.Application
    Dim OutMail As Outlook.MailItem
    Set OutMail = OutApp.CreateItem(olMailItem)
    With OutMail
        .To = "iemand@hotmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "Dit is het onderwerp"
        .Body = "Dit is de boodschap"
        .SendUsingAccount = Session.Accounts.Item("moetbestaan@hotmail.com")
        .Send
    End With
End Sub
Met late binding kan het zo:
Code:
Sub Mailmetafzender()
    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)
    With OutMail
        .To = "iemand@hotmail.com"
        .CC = ""
        .BCC = ""
        .Subject = "Dit is het onderwerp"
        .Body = "Dit is de boodschap"
        .SendUsingAccount = Session.Accounts.Item("afzender@helpmij.nl")
        .Send
    End With
End Sub
 
Laatst bewerkt:
Met de early binding krijg ik al gelijk dit


1757070215431.png

Maar met de 2e code werkt het wel.

Wat betekend dat dan die late binding?
 
Kijk eens hier:
 
Ahh, ik dacht dat het werkte omdat de mail werd verzonden.

Maar het is nog steeds uit mijn eigen naam en niet vanuit het account dat ik aangeef bij:
.SendUsingAccount = Session.Accounts.Item("ander@account.nl")
 
Code:
 Set .SendUsingAccount = .Session.Accounts.Item("ander@account.nl")
 
Kijk nog maar eens goed wat je fout hebt overgenomen.
 
Ja, daar heb je een punt ;)
 
  • Haha
Waarderingen: HSV
Tweede punt ontbrak inderdaad in mijn code, maar de SET in SET .SendSendUsingAccount is (althans bij mij) niet nodig.
 
Terug
Bovenaan Onderaan