I have a VBA script for Outlook. I am writing to download the attachments and sort them into their own folders. I am trying to call a function to calculate the name of the folder that it should go into based on the domain name but when I introduced the function, the rule is not running. I was able to download the files into a single folder when function was not there.
I have tried examples on changing function and also tried it as a Sub but unable to get them to work.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim EmailAddress As String
Dim FullDomainName As String
Dim DomainName As String
Dim CalculatedFolderName As String
EmailAddress = itm.SenderEmailAddress
DomainName = ""
CalculatedFolderName = ""
If (InStr(EmailAddress, "@") > 0) Then
FullDomainName = Right(EmailAddress, Len(EmailAddress) - InStr(EmailAddress, "@"))
DomainName = Left(FullDomainName, InStr(FullDomainName, ".") - 1)
End If
MsgBox DomainName
CalculatedFolderName = FolderName(DomainName)
saveFolder = "c:\attachment\test\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & DomainName & " " & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Function FolderName(DomainName As String) As String
MsgBox DomainName
FolderName = ""
If (DomainName = "abc") Then FolderName = "A"
ElseIf (DomainName = "xyz") Then FolderName = "B"
Else
FolderName = DomainName
End If
Exit Function
End Function
I was expecting a folder name to return when the function is called.
CalculatedFolderName? I don't see where the return is used.""at the start of your code; you then redefine domainname in an if-statement... did you step through and verify that the if-statement criteria were met?