Hello! I need to save attachments on my hard drive from incoming emails , based on subject name that contains both "Wind Power Forecast" and "0906" in the subject name. They are 2 emails, with same Subject name, every single day, and have to download the attachments from both of them. So i have a code, but there is a constant Run-time error 13, Type mismatch in the lines where i point the criteria for mail Subject :
I can't avoid it, so can you help me solve that, please?
VBA Code:
If InStr(outMailItem.Subject, "wind power forecast", vbTextCompare) > 0 And _
InStr(outMailItem.Subject, "0906", vbTextCompare) > 0 Then
I can't avoid it, so can you help me solve that, please?
VBA Code:
Public Sub Save_Attachments()
Dim OutlookOpened As Boolean
Dim outApp As Outlook.Application
Dim outNs As Outlook.NameSpace
Dim outFolder As Outlook.MAPIFolder
Dim outAttachment As Outlook.Attachment
Dim outItem As Object
Dim outMailItem As Outlook.mailitem
Dim inputDate As String, subjectFilter As String
Dim saveInFolder As String
saveInFolder = "C:\Users\BG-TRADE-005\OneDrive - ****.com\Desktop\Schedule\Mail_Temp\Download\"
If Right(saveInFolder, 1) <> "\" Then saveInFolder = saveInFolder & "\"
OutlookOpened = False
On Error Resume Next
Set outApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set outApp = New Outlook.Application
OutlookOpened = True
End If
On Error GoTo 0
If outApp Is Nothing Then
MsgBox "Cannot start Outlook.", vbExclamation
Exit Sub
End If
Set outNs = outApp.GetNamespace("MAPI")
'Set outFolder = outNs.GetDefaultFolder(olFolderInbox).Folders("Test")
Set outFolder = outNs.PickFolder
If Not outFolder Is Nothing Then
For Each outItem In outFolder.Items
If outItem.Class = Outlook.OlObjectClass.olMail Then
Set outMailItem = outItem
If InStr(outMailItem.Subject, "wind power forecast", vbTextCompare) > 0 And _
InStr(outMailItem.Subject, "0906", vbTextCompare) > 0 Then
For Each outAttachment In outMailItem.Attachments
outMailItem.SaveAs saveInFolder & Format(Now, "yyyymmdd hhnnss") & ".msg", olMSG
Next
End If
End If
Next
End If
End Sub