当我的规则运行时,下面的脚本运行并且运行良好,但是当通过该规则的多封电子邮件通过时,我得到一个
“运行时错误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报告26103101-123 Apple Ln,City,State(高级,$ 40.00,4014平方英尺)
电子邮件正文中也包含以下内容:
•报告ID:26103101(附加费,$ 40.00,4014平方英尺) •地址:州立苹果市123 Apple Ln 32174-8768
我需要提取到变量中的是街道地址和城市,所有其他信息与该规则无关
答案 0 :(得分:1)
因此,您的两个Replace()
方法的作用是将Address:
和您体内的所有逗号,
替换为###
,因此
• Report ID: 26103101 (Premium, $40.00, 4014 sq ft) • Address: 123 Apple Ln, City, State 32174-8768
成为
• Report ID: 26103101 (Premium### $40.00### 4014 sq ft) • ###123 Apple Ln### City### State 32174-8768
然后被拆分成一个数组,结果为
• Report ID: 26103101 (Premium
$40.00
4014 sq ft) •
123 Apple Ln
City
State 32174-8768
因此,我建议先找到Address:
,然后再除以,
delimitedMessage = Right$(msgtext, Len(msgtext) - InStr(1, msgtext, "Address: ") - 8)
varAddress = Split(delimitedMessage, ", ")
因此这将导致以下变量值:
答案 1 :(得分:0)
扩大我的评论。如果邮件始终遵循相同的格式,则似乎可以更好地解析主题行而不是电子邮件正文,
<something> - <street_address>, <city>, <state> (<somethingelse>)
将地址拆分为一个数组是
FullAddress=Split(Split(Split(myitem.Subject, " - ")(1), "(")(0), ",")
然后您可以访问以下不同的地址部分:
City = FullAddress(1)
对于您的错误,这意味着您正在访问的数组在索引“ 10”处没有项。这意味着您的地址不在您认为的阵列中。遇到此错误时,请单击“调试”按钮,然后可以在VBE的“本地”窗口中检查阵列的内容,以查看该阵列的外观。