Когда мое правило запускается, запускается приведенный ниже скрипт, и он работает нормально, но когда приходит несколько писем для этого правила, я получаю
"нижний индекс ошибки выполнения 9сообщение о диапазоне "
, когда я нажимаю" отладка ", выделяет эту строку:
sFileName = varAddress(10)
Вот мой код целиком.
Private Function CreateDir(FldrPath As String)
Dim Elm As Variant
Dim CheckPath As String
CheckPath = ""
For Each Elm In Split(FldrPath, "\")
CheckPath = CheckPath & Elm & "\"
If Len(Dir(CheckPath, vbDirectory)) = 0 Then
MkDir CheckPath
Debug.Print CheckPath & " Folder Created"
End If
Debug.Print CheckPath & " Folder Exist"
Next
End Function
Sub SaveEagleView(itm As Outlook.MailItem)
Dim strSubject As String, strExt As String
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim NextFriday As Date
Dim sFileName As String
Dim varAddress As Variant
Dim City As Variant
Dim fdObj As Object
Dim JobArea As String
Dim JobCity As Variant
Dim myPath As String
Dim myFinalPath As String
Dim objMsg As MailItem
Dim sFileExt As String
Set objMsg = Application.CreateItem(olMailItem)
Dim enviro As String
NextFriday = Date + 8 - Weekday(Date, vbFriday)
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))
On Error Resume Next
'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" Or JobCity = "Jacksonville Beach" Then
JobArea = "Jacksonville"
Else
JobArea = JobCity
End If
For Each objAtt In itm.Attachments
saveFolder = "C:\Users\admin\OneDrive\Documents\EagleView\" & Format$(NextFriday, "yyyy-mm-dd") & "\" & JobArea & "\"
CreateDir saveFolder
If Right(objAtt.FileName, 3) = "PDF" Then
sFileExt = ".pdf"
File = saveFolder & sFileName & sFileExt
objAtt.SaveAsFile File
End If
With objMsg
.To = "Careers@Email.com"
.CC = "CustomerService@Email.com"
.Subject = "New EagleView Needs Uploaded"
.BodyFormat = olFormatPlain
.Body = "A new EagleView has been received for the " & JobArea & " office. The file name is " & sFileName & " and needs to be uploaded. Thanks!"
.Send
End With
Set objMsg = Nothing
On Error Resume Next
Next
Next
Set objAtt = Nothing
End Sub
Спасибо за вашу помощь!
Отредактируйте, чтобы добавить больше информации:
Тема электронного письма такова:
FW: EagleView Report 26103101 - 123 Apple Ln, Город, штат (Premium, $ 40,00, 4014 кв. Футов)
В тексте письма также указывается:
• Идентификатор отчета: 26103101 (Premium, $ 40,00, 4014кв. фут) • Адрес: 123 Apple Ln, город, штат 32174-8768
В переменную мне нужно извлечь адрес улицы, а город, вся другая информация не имеет значения для этого правила