首页 > 解决方案 > SendUsingAccount SendAs 权限但未在索引中找到

问题描述

我需要能够从不同的电子邮件地址从 VBA 发送电子邮件。我有权从该地址发送,并且可以从 Outlook 消息窗口手动选择它。但是,当我运行以下代码时,它没有索引。显示的只是我的电子邮件地址。

Sub Which_Account_Number()
'Don't forget to set a reference to Outlook in the VBA editor
    Dim OutApp As Object
    Dim I As Long

    Set OutApp = CreateObject("Outlook.Application")

    For I = 1 To OutApp.Session.Accounts.Count
        MsgBox OutApp.Session.Accounts.Item(I) & " : This is account number " & I
    Next I
End Sub

有没有办法在通话中使用实际的电子邮件地址?这是我要完成的测试代码:

Sub SendMessagesTest()

   Dim objOutlook As Object ' Outlook.Application
   Dim objOutlookMsg As Object ' Outlook.MailItem
   Dim objOutlookRecip As Object ' Outlook.Recipient

    ' Create the Outlook session.
   Set objOutlook = CreateObject("Outlook.Application")
   objOutlook.Session.Logon

   ' Create the message.
   Set objOutlookMsg = objOutlook.CreateItem(0)   '0 = olMailItem              

    With objOutlookMsg

         ' Set the Subject & Body of the message.
         .Subject = "Test Subject"
         .Body = "Test Body"
         '.BodyFormat = 3   '3 = olFormatRichText  (Late Binding)

        'Change Item(1)to another number to use another account
       Set .SendUsingAccount = "TestUser@test.com" 'objOutlook.Session.Accounts.Item(2)  ' (Late Binding)

       .Display

   End With

    Set objOutlook = Nothing
    Set objOutlookMsg = Nothing
    Set objOutlookRecip = Nothing
    Exit Sub

End Sub

当我运行它时,我收到错误“需要对象”。

我不能使用这种类型的代码,因为我没有用于电子邮件地址的索引号:

Set .SendUsingAccount = objOutlook.Session.Accounts.Item(1) 

编辑:这是我用来将约会项目添加到与我共享的另一个用户的日历的代码。注意:我对尝试发送为的邮箱拥有发布编辑权限。

Sub CreateCalendarApptx()
    Dim objApp As Object
    Dim objNS As Object
    Dim objFolder As Object
    Dim objRecip As Object
    Dim objAppt As Object
    Dim objMsg As Object
    Const olMailItem = 0
    Const olFolderCalendar = 9
    Dim strName As String

    Set objApp = CreateObject("Outlook.Application")
    Set objNS = objApp.getNamespace("MAPI")
    Set objMsg = objApp.CreateItem(olMailItem)

   strName = "OtherUser@Test.com"
    'Select Calendar on which to place the appointment
    'The Calendar can either be set with the name of the calendar or the Folder ID
    If Left(strName, 3) = "ID:" Then
        'Strip out the ID: identifier and leave just the ID
        strName = Mid(strName, 5, Len(strName))
        Set objFolder = objNS.GetFolderFromID(strName)
    Else
        Set objRecip = objMsg.Recipients.Add(strName)
        objRecip.Resolve
        If objRecip.Resolved Then
            Set objFolder = objNS.GetSharedDefaultFolder(objRecip, olFolderCalendar)
        End If
    End If

    Set objAppt = objFolder.Items.Add
    objAppt.Subject = "Test"
    objAppt.Display

   Set objApp = Nothing
    Set objNS = Nothing
    Set objFolder = Nothing
    Set objMsg = Nothing
    Set objRecip = Nothing
    Set objAppt = Nothing

End Sub

编辑 2:我之前添加了另一条评论,但董事会似乎不喜欢它,因为我附上了一张图片。结果是,当我从 Outlook 界面发送一封电子邮件,在 From: 字段中使用不同的名称时,它发送成功。但是,当我将鼠标悬停在它上面时,我看到“发件人:OtherUser@test.com 使用帐户发送:Me@test.com”如果是这种情况,VBA 中的 SendUsingAccount 将是我的电子邮件地址,并且应该有另一个属性将是 From: 字段。

标签: vbams-accessoutlook

解决方案


更改您的声明:

Set .SendUsingAccount = objOutlook.Session.Accounts.Item(1) 

至:

Set .SendUsingAccount = objOutlook.Session.Accounts.Item("Testuser@test.com") 

推荐阅读