简体   繁体   English

使用Excel VBA指向备用.pst收件箱的非默认文件夹

[英]Use Excel VBA to point to a non-default folder of an alternate .pst inbox

I cannot find code that works, and that is entirely operated out of Excel VBA to point to an inbox that is not the default inbox in Outlook. 我找不到有效的代码,并且该代码完全在Excel VBA中无法运行,以指向不是Outlook中默认收件箱的收件箱。 Imagine you have a second inbox, with an alternative email address for special emails. 假设您有第二个收件箱,其中还有一个用于特殊电子邮件的备用电子邮件地址。 It seems like; 这好像是;

Set Inbox = Ns.GetDefaultFolder(olFolderInbox) is the natural location to alter to the appropriate code. Set Inbox = Ns.GetDefaultFolder(olFolderInbox)是更改为适当代码的自然位置。 Some suggestions involved using parent.folder but that does not appear to function. 一些建议涉及使用parent.folder,但似乎没有作用。 Suggestions? 建议?

Assume alternative inbox has the name "New Orders" 假设替代收件箱的名称为“新订单”

I have tried using Set Inbox = Ns.GetDefaultFolder(6).Parent.Folders("New Orders") 我曾尝试使用Set Inbox = Ns.GetDefaultFolder(6).Parent.Folders("New Orders")

That won't do. 那不会。 What you're basically doing is looking for another folder with the same hierarchy as the Inbox folder (on the same account or email) but not another folder in another account. 您基本上要做的是寻找与“ Inbox文件夹具有相同层次结构的另一个文件夹(在同一帐户或电子邮件上),而不是在另一个帐户中的另一个文件夹。

...with an alternative email address for special emails... ...以及用于特殊电子邮件的备用电子邮件地址...

Try using this for the above case (I used Early Binding): 尝试将其用于上述情况(我使用了Early Binding):

Dim oOL As Outlook.Application
Dim oAcc As Outlook.Account
Dim oStore As Outlook.Store
Dim oFolder As Outlook.Folder

Set oOL = GetObject(, "Outlook.Application")

For Each oAcc In oOL.Session.Accounts
  If oAcc.UserName = "User.Name" Then 
  '// Note: you can use other properties, I used this for demo //
    Set oStore = oAcc.DeliveryStore
    Set oFolder = oStore.GetDefaultFolder(olFolderInbox)
    Set oFolder = oFolder.Parent.Folders("New Oders")
  End If
Next

First, you can try running the For Loop to check if you really have 2 accounts. 首先,您可以尝试运行For Loop以检查您是否确实有2个帐户。 Once verified, you can go ahead and play around with it. 验证后,您可以继续尝试。 HTH. HTH。

HTH, thanks for your suggestions. HTH,谢谢您的建议。 I have tried to incorporate this into my code. 我试图将其合并到我的代码中。 Unfortunatly I am left in the same position. 不幸的是,我被留在了相同的位置。 I am not receiving a blank file in my destination folder of 4kb with the proper naming convention 我没有使用正确的命名约定在4kb的目标文件夹中收到空白文件

here is what I have so far..perhaps you can see my error in context. 这是我到目前为止所拥有的..也许您可以在上下文中看到我的错误。

Option Explicit

Sub Get_IOVFs()


Dim outlookInbox            As Outlook.MAPIFolder
Dim Item                    As Object
Dim outlookAttachment       As Outlook.Attachment
Dim attachmentFound         As Boolean
Dim attachmentName          As String
Const saveToFolder          As String = "C:\Users\Wassej03\Documents\IOVFs_Master"
Const attName               As String = "IOVF "
Dim TimeExt                 As String
Dim SavePath                As String
Dim ExtString               As String
Dim Filename                As String
Dim I                       As Integer

Dim oOL As Outlook.Application
Dim oAcc As Outlook.Account
Dim oStore As Outlook.Store
Dim oFolder As Outlook.Folder

Set oOL = GetObject(, "Outlook.Application")

For Each oAcc In oOL.Session.Accounts
  If oAcc.UserName = "ccIOVF@zoetis.com" Then
  '// Note: you can use other properties, I used this for demo //
    Set oStore = oAcc.DeliveryStore
    Set oFolder = oStore.GetDefaultFolder(olFolderInbox)
    Set oFolder = oFolder.Parent.Folders("Diagnostics Orders")
  End If
Next

TimeExt = format(Now, "dd-mmm-yy h-mm")
attachmentName = attName & TimeExt

'Get the inbox from Outlook
Dim NS As Outlook.Namespace
Dim objOwner As Outlook.Recipient

'Move to the alternative email Inbox
Set NS = oOL.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("cciovf@zoetis.com")
    objOwner.Resolve
Set outlookInbox = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)

'Make sure that file extension at the end of this line is correct
SavePath = saveToFolder & "\" & attachmentName & ".xlsm"

'Loop through each email to save its attachment
I = 0
For Each Item In outlookInbox.Items
    For Each outlookAttachment In Item.Attachments
    If LCase(Right(outlookAttachment.Filename, Len(ExtString))) = LCase(ExtString) Then
                Filename = SavePath
                outlookAttachment.SaveAsFile Filename
                I = I + 1
             End If
        Next outlookAttachment
    Next Item


MsgBox "IOVFs were searched and if found are saved to '" & saveToFolder & "'!", vbInformation

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM