gpt4 book ai didi

vba - 使用vba访问outlook中的另一个邮箱

转载 作者:行者123 更新时间:2023-12-04 20:27:07 24 4
gpt4 key购买 nike

我的 Outlook 中有两个邮箱。

一个是我的,当我登录我的电脑时它会自动登录,另一个是用于邮件退回。

我确实需要访问邮件帐户的收件箱,但我似乎做不到。

而且我没办法让邮件账号的邮箱成为我的默认邮箱

这是我目前的代码:

Public Sub GetMails()

Dim ns As NameSpace
Dim myRecipient As Outlook.Recipient
Dim aFolder As Outlook.Folders

Set ns = GetNamespace("MAPI")

Set myRecipient = ns.CreateRecipient("mail@mail.pt")
myRecipient.Resolve
If myRecipient.Resolved Then
MsgBox ("Resolved")
Set aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)
Else
MsgBox ("Failed")
End If

End Sub

我遇到的问题是

设置 aFolder = ns.GetSharedDefaultFolder(myRecipient, olFolderInbox)

我得到了 Resolved msgbox,所以我知道这是有效的,但在那之后我得到了一个错误:

Run-Time Error

这并没有说明错误本身。

有人可以帮我吗?谢谢

最佳答案

如果您要访问的文件夹不是 Exchange 文件夹,您将需要找到它,如果它是 Exchange 文件夹,请尝试登录到命名空间。

登录到命名空间

  Set oNS = oApp.GetNamespace("MAPI")
oNS.Logon

查找文件夹据我所知,这段代码来自 Sue Mosher。

Public Function GetFolder(strFolderPath As String) As Object 'MAPIFolder
' strFolderPath needs to be something like
' "Public Folders\All Public Folders\Company\Sales" or
' "Personal Folders\Inbox\My Folder" ''

Dim apOL As Object 'Outlook.Application '
Dim objNS As Object 'Outlook.NameSpace '
Dim colFolders As Object 'Outlook.Folders '
Dim objFolder As Object 'Outlook.MAPIFolder '
Dim arrFolders() As String
Dim I As Long

On Error GoTo TrapError

strFolderPath = Replace(strFolderPath, "/", "\")
arrFolders() = Split(strFolderPath, "\")

Set apOL = CreateObject("Outlook.Application")
Set objNS = apOL.GetNamespace("MAPI")


On Error Resume Next

Set objFolder = objNS.Folders.Item(arrFolders(0))

If Not objFolder Is Nothing Then
For I = 1 To UBound(arrFolders)
Set colFolders = objFolder.Folders
Set objFolder = Nothing
Set objFolder = colFolders.Item(arrFolders(I))

If objFolder Is Nothing Then
Exit For
End If
Next
End If

Set GetFolder = objFolder
Set colFolders = Nothing
Set objNS = Nothing
Set apOL = Nothing


End Function

关于vba - 使用vba访问outlook中的另一个邮箱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/635558/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com