I'm using the following script to save an email attachment to a file. I'm getting a compile error of "Unexpected Next" on line 23. What am I doing wrong?
Function Main()
Set objApp = GetObject(, "Outlook.Application") '#### Find existing instance of Outlook
If Err Then
'WScript.Quit() '#### If Outlook isn't running, quit the script
Set objApp = CreateObject("Outlook.Application") '#### uncomment this & comment out above line to get data even if Outlook isnt running
End If
Set objOutlook = objApp.GetNameSpace("MAPI") '#### Get MAPI namespace
Set objInbox = objOutlook.GetDefaultFolder(6).Items
intA = 0
For Each aMail In objInbox
If aItem.Subject = "test email" Then
Set myattachments = aMail.Attachments
If myattachments.Count > 0 Then
For Each aAttachment In myattachments
Wscript.Echo aMail.Subject & " " & aAttachment.DisplayName
aAttachment.SaveAsFile "C:\" & aAttachment.DisplayName
Next
End If
Next
'set objects to nothing
Set objApp = Nothing
Set objOutlook = Nothing
Set objInboxItems = Nothing
Main = DTSTaskExecResult_Success
End Function
Function Main()
Set objApp = GetObject(, "Outlook.Application") '#### Find existing instance of Outlook
If Err Then
'WScript.Quit() '#### If Outlook isn't running, quit the script
Set objApp = CreateObject("Outlook.Application") '#### uncomment this & comment out above line to get data even if Outlook isnt running
End If
Set objOutlook = objApp.GetNameSpace("MAPI") '#### Get MAPI namespace
Set objInbox = objOutlook.GetDefaultFolder(6).Items
intA = 0
For Each aMail In objInbox
If aItem.Subject = "test email" Then
Set myattachments = aMail.Attachments
If myattachments.Count > 0 Then
For Each aAttachment In myattachments
Wscript.Echo aMail.Subject & " " & aAttachment.DisplayName
aAttachment.SaveAsFile "C:\" & aAttachment.DisplayName
Next
End If
Next
'set objects to nothing
Set objApp = Nothing
Set objOutlook = Nothing
Set objInboxItems = Nothing
Main = DTSTaskExecResult_Success
End Function