0
添付ファイルをOutlookから特定のフォルダに保存しようとすると、添付ファイルが1つ以上ある場合は最初の添付ファイルが特定のフォルダに保存されます。Outlookから添付ファイルを特定のフォルダに保存するには
For Each objAtt In objMsg.Attachments
strFile = strFolderpath & objAtt.Filename
objAtt.SaveAsFile strFile
Next
をしかし:代わりにFor Each
ループを使用する - それはあなたがコレクション内の項目の未知の量で作業している場合は、エラー
Public Sub SaveAttachments()
Dim Folder As Outlook.MAPIFolder
Dim objOL As Outlook.Application
Dim objMsg As Outlook.MailItem 'Object
Dim objAttachments As Outlook.Attachments
Dim objSelection As Outlook.Selection
Dim MailBoxName As String
Dim Pst_Folder_Name As String
Dim Pst_SubFolder_Name As String
Dim val
Dim strFile As String
'Dim oOlAp As Object, oOlns As Object, oOlInb As Object
' Dim oOlItm As Object
Dim i As Long
Dim lngCount As Long
Dim strFolderpath As String
On Error GoTo ErrorHandler
MailBoxName = ActiveSheet.Cells(1, 2).Value
Pst_Folder_Name = ActiveSheet.Cells(2, 2).Value
If ActiveSheet.Cells(2, 3).Value <> "" Then
Pst_SubFolder_Name = ActiveSheet.Cells(2, 3).Text
Set Folder = Outlook.Session.Folders(MailBoxName).Folders(Pst_Folder_Name).Folders(Pst_SubFolder_Name)
Else
Set Folder = Outlook.Session.Folders(MailBoxName).Folders(Pst_Folder_Name)
End If
val = 1
Dim myOutlook As Object: Set myOutlook = CreateObject("Outlook.application")
Dim myNameSpace As Object: Set myNameSpace = myOutlook.GetNamespace("MAPI")
'Dim MailFolder As Object: Set MailFolder = myNameSpace.Folders("Folder")
' Set the Attachment folder.
strFolderpath = "C:\Projects\Savefile\"
'~~> Check if the email actually has an attachment
For Each objMsg In Folder.Items
If objMsg.Attachments.Count <> 0 Then
'''''For each statement
i = objMsg.Attachments.Count
'~~> Download the attachment
For val = 1 To i
Set objAttachments = objMsg.Attachments
strFile = strFolderpath & objAttachments.Item(val).Filename
objAttachments.Item(val).SaveAsFile strFile
val = val + 1
Next
End If
ErrorHandler:
Resume Next
End Sub
あなたはどのようなエラーを見ていますか? – Alex