【发布时间】:2023-03-28 15:34:01
【问题描述】:
我正在编写代码来处理传入的电子邮件。大多数方面工作正常;但是,处理日期给我带来了一些麻烦。我在 Module1 中定义的 EvaluateDate 函数无法正常工作。我运行它时没有错误,只是没有输出。表是一个子字符串数组。传入的电子邮件按行拆分为子字符串。因此,基本上数组的每个索引都是电子邮件中的一行。我正在寻找一个特定的月份,然后为一月份分配“01/”,依此类推。收到的电子邮件是这样的“2011 年 10 月 20 日星期四”,并希望被处理到“2011 年 10 月 20 日”。一切都是字符串类型。任何帮助将不胜感激。如果您需要更多其他代码来确定问题的根源,请告诉我。谢谢。
在工作表 1 代码中,
Public Sub CommandButton1_Click()
Dim olApp As New Outlook.Application
Dim olExp As Outlook.Explorer
Dim olSel As Outlook.Selection
Dim myArray(8) As String
Dim Line As Long, Addr1 As String
Dim Tabl, str As String
Dim index As Integer
Dim I As Integer, x As Integer, N As Integer, j As Integer
Sheets("EditData").Select
Columns("D:D").NumberFormat = "@"
'Selection.NumberFormat = "@"
On Error Resume Next
' Getting the messages selection
Set olApp = Outlook.Application
Set olExp = olApp.ActiveExplorer
Set olSel = olExp.Selection
' Checking if there is at least one message selected
If olSel.Count < 1 Then
MsgBox "No message selected", vbExclamation, "Error"
Exit Sub
End If
With Sheets("EditData")
' Retrieving the first avaible row to put message in
Line = .Range("D65000").End(xlUp).Row + 1
' looping through message
For x = 1 To olSel.Count
DoEvents
Erase myArray
mybody = Replace(olSel.Item(x).body, Chr(13), "")
' Splitting the message body into an array of substrings,
' using the "line feed" characters as separators
mybody = Replace(mybody, Chr(10) & Chr(10), Chr(10))
Tabl = Split(mybody, Chr(10))
For Each Item In Tabl
Item = Replace(Item, Chr(10), "")
Item = Application.Clean(Item)
Next Item
' Looping through these substrings
For I = 0 To UBound(Tabl)
' Date Received Start
If LCase(Left(Tabl(I), 4)) = "sent" Then
m = Module1.EvaluateDate(Tabl)
.Cells(Line, 2) = m
End If
Next I
Next X
End With
End Sub
在模块 1 中,
'Function to determine the month, day, and year in this format mm/dd/yy
Public Function EvaluateDate(Tabl As Variant) As Variant
For I = 0 To UBound(Tabl)
If InStr(1, Tabl(I), "January", 1) > 0 Then
m = "01/"
End If
If InStr(1, Tabl(I), "February", 1) > 0 Then
m = "02/"
End If
If InStr(1, Tabl(I), "March", 1) > 0 Then
m = "03/"
End If
If InStr(1, Tabl(I), "April", 1) > 0 Then
m = "04/"
End If
If InStr(1, Tabl(I), "May", 1) > 0 Then
m = "05/"
End If
If InStr(1, Tabl(I), "June", 1) > 0 Then
m = "06/"
End If
If InStr(1, Tabl(I), "July", 1) > 0 Then
m = "07/"
End If
If InStr(1, Tabl(I), "August", 1) > 0 Then
m = "08/"
End If
If InStr(1, Tabl(I), "September", 1) > 0 Then
m = "09/"
End If
If InStr(1, Tabl(I), "October", 1) > 0 Then
m = "10/"
End If
If InStr(1, Tabl(I), "November", 1) > 0 Then
m = "11/"
End If
If InStr(1, Tabl(I), "December", 1) > 0 Then
m = "12/"
End If
Next I
EvaluateDate = m
End Function
【问题讨论】:
-
请使用适当的格式修正您的代码。
-
vb6、vba、vbscript 还是 vb.net?
标签: arrays string vba date substring