I have emails with pdf attachments I would like to save automatically as they come into my inbox. I have my code mostly written, I have tested that all the variables have the correct value, and they output the correct data; however, I'm not sure how to code the actual saving of the file.
The file will get renamed to the customer's address, which is extracted with my code below:
Sub EagleViewSaveAttachment()
'Define Variables
Dim sFileName As String
Dim varAddress As Variant
Dim City As Variant
Dim fdObj As Object
Dim NextFriday As Date
Dim JobArea As String
Dim JobCity As Variant
Dim myPath As String
Dim objAtt As Outlook.Attachment
Dim myFinalPath As String
'Set Variables
NextFriday = Date + 8 - Weekday(Date, vbFriday)
myPath = "C:\Users\admin\OneDrive\Documents\EagleView\"
Set myfolder = Outlook.ActiveExplorer.CurrentFolder
Set fdObj = CreateObject("Scripting.FileSystemObject")
'Loop through emails in folder
For i = 1 To myfolder.Items.Count
Set myitem = myfolder.Items(i)
msgtext = myitem.Body
'Search for Specific Text
delimitedMessage = Replace(msgtext, "Address: ", "###")
delimitedMessage = Replace(delimitedMessage, ",", "###")
varAddress = Split(delimitedMessage, "###")
'Assign the job address from email to variable
sFileName = varAddress(10)
JobCity = LTrim(varAddress(11))
'Define office area based on job city
If JobCity = "Panama City" Or JobCity = "Mexico Beach" Or JobCity = "Panama City Beach" Or JobCity = "Lynn Haven" Or JobCity = "Port Saint Joe" Then
JobArea = "Panama"
ElseIf JobCity = "Daytona Beach" Or JobCity = "Port Orange" Or JobCity = "Deltona" Or JobCity = "Ormond Beach" Or JobCity = "Deland" Then
JobArea = "Daytona"
ElseIf JobCity = "Orlando" Then
JobArea = "Orlando"
ElseIf JobCity = "Jacksonville" Then
JobAre = "Jacksonville"
Else
JobArea = LTrim(varAddress(11))
End If
'Define Final Path
myFinalPath = myPath + Format$(NextFriday, "yyyy-mm-dd") + "\" + JobArea + "\"
'Check if the path exists, if not create it
If fdObj.FolderExists(myFinalPath) Then
MsgBox "Found it."
Else
fdObj.CreateFolder (myFinalPath)
MsgBox "It has been created."
End If
Next
End Sub
As of right now, what I am unable to do is get it to check if the directory C:\Users\admin\OneDrive\Documents\EagleView\yyyy-mm-dd\JobArea
already exists and to create it if it doesn't already exist.
I'm fairly certain the problem lies in my usage of fdObj.FolderExists(myFinalPath)
as it seems that doesn't accept variables.