excel 我怎么能发送一个outlook邀请从共享邮箱在vba

wljmcqd8  于 2023-08-08  发布在  其他
关注(0)|答案(2)|浏览(128)

我一直试图从VBA设置会议邀请,但我无法使用共享邮箱这样做。它的工作完美时,做了个人邮件,但我似乎不能添加共享邮箱,即使我有完全的权限。
我认为问题在于设置outAccount,它只接收我的个人电子邮件,而不是共享的

Sub send_invites(r As Long)
    Dim OutApp As Outlook.Application
    Dim OutMeet As Outlook.AppointmentItem
    Set OutApp = Outlook.Application
    Set OutMeet = OutApp.CreateItem(olAppointmentItem)
    Dim OutAccount As Outlook.Account: Set OutAccount = OutApp.Session.Accounts.Item(1)

    With OutMeet
            .Subject = Cells(r, 1).Value
            .RequiredAttendees = Cells(r, 11).Value
    '       .OptionalAttendees = ""
    
            Dim sDate As Date: sDate = Cells(r, 2).Value + Cells(r, 3).Value
            Dim eDate As Date: eDate = Cells(r, 4).Value + Cells(r, 5).Value
            
            .Start = sDate
            .End = eDate
            
            .Importance = olImportanceHigh
            
            Dim rDate As Date: rDate = Cells(r, 7).Value + Cells(r, 8).Value
            Dim minBstart As Long: minBstart = DateDiff("n", sDate, eDate)
            
            .ReminderMinutesBeforeStart = minBstart
            
            .Categories = Cells(r, 9)
            .Body = Cells(r, 10)
            
            .MeetingStatus = olMeeting
            .Location = "Microsoft Teams"
            
            .SendUsingAccount = OutAccount
            .Send
    End With
    
    Set OutApp = Nothing
    Set OutMeet = Nothing
End Sub

Sub send_invites_click()
        Dim rg As Range: Set rg = shData.Range("A1").CurrentRegion
        Dim i As Long
        For i = 2 To rg.Rows.Count
                Call send_invites(i)
        Next i
End Sub

字符串

2fjabf4q

2fjabf4q1#

SendUsingAccount属性需要在Outlook中配置另一个帐户。请确保您已在Outlook中配置了所需的帐户。
如果您没有在Outlook中配置共享邮箱,则需要使用SentOnBehalfOfName属性。该属性返回或设置一个字符串,该字符串指示邮件消息的预期发件人的显示名称。请注意,在这种情况下,您必须有足够的权限代表其他人发送。

xwbd5t1u

xwbd5t1u2#

我明白了

因此,共享邮箱的方法是通过用户帐户识别正确的文件夹,然后才能创建会议。
SentOnBehalfOfName似乎不是必需品。
对于任何寻求解决这个问题的人,这里是完整的代码:

Sub send_invites_click()
        Dim rg As Range: Set rg = shData.Range("A1").CurrentRegion
        Dim i As Long
        For i = 2 To rg.Rows.Count
                Call send_meetings(i)
        Next i
End Sub

Sub send_meetings(r)

    Dim OutApp As Outlook.Application
    Set OutApp = CreateObject("Outlook.Application")
    
    Dim OutMail As Outlook.MailItem
    Set OutMail = OutApp.CreateItem(olMailItem)
    
    Dim SharedMailboxEmail As String
    SharedMailboxEmail = Range("sharedMail").Value
    
    Set outNameSpace = OutApp.GetNamespace("MAPI")
    Set outSharedName = outNameSpace.CreateRecipient(SharedMailboxEmail)
    Set outCalendarFolder = outNameSpace.GetSharedDefaultFolder(outSharedName, 9) '9=olFolderCalendar
    Set OutMeet = outCalendarFolder.Items.Add(1) '1=olAppointmentItem
    
    Dim Recipients As Recipients
    Set Recipients = OutMail.Recipients
    
    Dim objOutlookRecip As Recipient
    Set objOutlookRecip = Recipients.Add(shData.Cells(r, 11).Value)
    
    Dim i As Long
    For i = 1 To OutApp.Session.Accounts.Count
            If OutApp.Session.Accounts.Item(i) = Range("userMail") Then
                Exit For
            End If
    Next i
    
    Dim OutAccount As Outlook.account
    Set OutAccount = OutApp.Session.Accounts.Item(i)
    
    objOutlookRecip.Type = 1
    
    With OutMeet
            Dim sDate As Date
            sDate = Cells(r, 2).Value + Cells(r, 3).Value
            
            Dim eDate As Date
            eDate = Cells(r, 4).Value + Cells(r, 5).Value
            
            Dim rDate As Date
            rDate = Cells(r, 7).Value + Cells(r, 8).Value
            
            Dim minBstart As Long
            minBstart = DateDiff("n", sDate, eDate)
            
            .Subject = Cells(r, 1).Value
            .RequiredAttendees = Cells(r, 11).Value
            .Start = sDate
            .End = eDate
            .Importance = olImportanceHigh
            .ReminderMinutesBeforeStart = minBstart
            .Categories = Cells(r, 9)
            .Body = Cells(r, 10)
            .MeetingStatus = olMeeting
            .SendUsingAccount = OutAccount
            
            'Resolve each Recipient's name.
            For Each objOutlookRecip In OutMeet.Recipients
                objOutlookRecip.Resolve
            Next
            
            .Send
    End With

    Set OutApp = Nothing
    
End Sub

字符串

相关问题