【发布时间】:2015-06-19 20:46:44
【问题描述】:
我正在编写 VBA 代码以从 Outlook 电子邮件中获取文本并将其放入我设置的 Excel 工作表中。我正在使用 Excel 2010。我的电子邮件包含
以下信息:
公司:ABC 公司
上课时间:2013-10-29 至 2014-10-22
我已经设置了一个 For With 循环来遍历电子邮件并在 A 列中插入公司名称,在 B 列中插入第一个日期 (2013-10-29) 和另一个日期 (2014-10-22)在 C 列中。当我运行我的代码时,我收到一条错误消息:运行时错误 5:以下代码行中的过程调用或参数无效:
vText2 = Trim(M.SubMatches(2))
请告诉我我做错了什么。我的部分代码如下。如果我需要提供任何其他信息,请告诉我。
sText = olItem.Body
Set Reg1 = CreateObject("VBScript.RegExp")
For i = 1 To 3
With Reg1
Select Case i
Case 1
.Pattern(Company\s[:]+\s(\w*\s*\w*\s*\w*\s*\w*\s*\w*\s*\w*\s*\w*\s*\w*\s*\w*\s*)\n)"
.Global = False
Case 2
.Pattern = "(Class Period\s*[:]+\s*([\d-\s]*))"
.Global = False
Case 3
.Pattern = "(through+\s*([\d-\s]*))"
.Global = False
End Select
End With
If Reg1.Test(sText) Then
Set M1 = Reg1.Execute(sText)
For Each M In M1
vText = Trim(M.SubMatches(1))
vText2 = Trim(M.SubMatches(2))
vText3 = Trim(M.SubMatches(3))
Next
End If
Next i
xlSheet.Range("A" & rCount) = vText
xlSheet.Range("B" & rCount) = vText2
xlSheet.Range("C" & rCount) = vText3
xlWB.Close 1
【问题讨论】:
-
我想你想写
.Pattern = "(Company而不是.Pattern(Company。此外,\d-\s无效,请使用\d\s-。我也认为你的模式中没有 submatch(3)。
标签: regex vba excel runtime-error