gpt4 book ai didi

vba - 错误继续显示下一个不起作用/创建文件夹错误440

转载 作者:行者123 更新时间:2023-12-03 08:45:28 25 4
gpt4 key购买 nike

步骤1:
我要创建一个文件夹,如果失败(因为它可能已经存在),我希望它忽略并继续:

    Sub MakeFolder()

'declare variables
Dim outlookApp As Outlook.Application
Dim NS As Outlook.NameSpace

'set up folder objects
Set outlookApp = New Outlook.Application
Set outlookApp = New Outlook.Application
Set NS = outlookApp.GetNamespace("MAPI")
Set objOwner = NS.CreateRecipient("email@host.com")
objOwner.Resolve
Set outlookInbox = NS.GetSharedDefaultFolder(objOwner, olFolderInbox)

'make a folder, maybe
Dim newFolder
On Error Resume Next
Set newFolder = outlookInbox.Folders.Add("myNewFolder")
On Error GoTo -1
On Error GoTo 0
End Sub

但我仍然得到一个错误:

enter image description here

我怎样才能获得这种耐力?如果该文件夹不存在,它将顺利运行并创建它。

步骤2:
我有一个文件夹列表(大约60个),该列表可能会随着时间而变化。因此,我想运行一个脚本来检查新文件夹,然后创建它们:

     For Each fol In folders
On Error Resume Next
Set newFolder = outlookInbox.Folders.Add(fol)
If Err.Number <> 0 Then
On Error GoTo -1
Else:
Debug.Print fol & " created "
End If
On Error GoTo 0
Next ID

同样在这里,如果无法创建该文件夹,则 outlookInbox.Folders.Add()会引发错误,无论返回的是下一步如何。我能做什么?

编辑

[请参阅第一个评论以获取解决方案]

最佳答案

现在,您修复了IDE,可以使用以下代码

Option Explicit
Public Sub Example()
Dim olNs As Outlook.NameSpace
Set olNs = Application.GetNamespace("MAPI")

Dim Inbox As Outlook.Folder
Set Inbox = olNs.GetDefaultFolder(olFolderInbox)

Dim SubFolder As Outlook.Folder

'// SubFolder Name
Dim FolderName As String
FolderName = "myNewFolder"

'// Check if folder exist else create one
If FolderExists(Inbox, FolderName) = True Then
Debug.Print "Folder Exists"
Set SubFolder = Inbox.Folders(FolderName)
Else
Set SubFolder = Inbox.Folders.Add(FolderName)
End If

End Sub


'// Function - Check folder Exist
Private Function FolderExists(Inbox As Folder, FolderName As String)
Dim Sub_Folder As MAPIFolder

On Error GoTo Exit_Err
Set Sub_Folder = Inbox.Folders(FolderName)

FolderExists = True
Exit Function

Exit_Err:
FolderExists = False

End Function

关于vba - 错误继续显示下一个不起作用/创建文件夹错误440,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55749551/

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