0

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.

6
  • 1
    Do you ever use CalculatedFolderName? I don't see where the return is used. Commented Oct 9, 2019 at 17:50
  • I haven't. I am planning to use that to create a folder if the function works. Commented Oct 9, 2019 at 18:04
  • Both domainname and foldername are to to "" 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? Commented Oct 9, 2019 at 18:08
  • yes. Before I introduced the function, it executed perfectly fine. Commented Oct 9, 2019 at 18:10
  • So the issue is with domainname then and not the function return? Returning something you never use isn't going to break anything (assuming the function isn't failing). Do you have Calculatedfoldername in a watch window or a break point to see that it is empty? Commented Oct 9, 2019 at 18:15

1 Answer 1

0

The fix was done by moving the next statement to the next line.

If (DomainName = "abc") Then 
     FolderName = "A"
ElseIf (DomainName = "xyz") Then 
     FolderName = "B"
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.