gpt4 book ai didi

正则表达式错误无法编译 Match.Count

转载 作者:行者123 更新时间:2023-12-01 19:34:55 24 4
gpt4 key购买 nike

我正在编写一个 VBA 脚本用作 outlook 宏。目标是根据主题行中的案例编号自动将电子邮件分类到文件夹中。

首先我尝试使用“类字符串”功能,它可以选择正确的电子邮件,但我需要更大的灵 active ,所以我尝试使用正则表达式。到目前为止,这是我的代码。我卡在了

   If objMatch.Count > 0 Then

尝试编译导致错误:“编译错误:未找到方法或数据成员”

我已经在网上搜索过我做错了什么,但我认为 match.count 应该是有效的。我没有使用 VB 的经验,所以如果有任何具体提示,我将不胜感激。

完整代码:

Option Explicit
Sub FoldalotMacro()
Dim fdr As String
Dim CaseFolders As Folder

Dim strEmail As String
Dim RegEx As RegExp
Dim objMatch As Match
Dim objMatches As MatchCollection
Set RegEx = CreateObject("vbscript.regexp")
With RegEx
.Pattern = ".*(68\d{7})(.{0,20}).*"
.IgnoreCase = True
.Global = False
End With

Dim Sel, Item

' ** Source: Items to be processed
Set Sel = Outlook.Session.Folders("My Name").Folders("Inbox").Folders("caseinbox").Items
' ** Target folder
Set CaseFolders = Outlook.Session.Folders("My Name").Folders("Inbox").Folders("casetest")

For Each Item In Sel
Set objMatch = RegEx.Execute(Item.Subject)
' ** Run Regex against item subject
If objMatch.Count > 0 Then
' ** folder title is the extracted case number
Set fdr = objMatch.Item(0).SubMatches(0)
' ** create the folder if it does not exist
If CaseFolders.Folders(fdr) Is Nothing Then CaseFolders.Folders.Add fdr
End If
Item.Move CaseFolders.Folders(fdr)
Else
' ** alert if no action
Debug.Print "no match found"
End If
End Sub

编辑:我还有几个步骤。新代码:

    ' process manually
Option Explicit
Sub FoldalotMacro()
Dim fdr As String
Dim CaseFolders As Outlook.Folder
Dim Counter As Long
Dim strEmail As String
Dim Sel2 As Outlook.Folder
Dim Item As Object
Dim Sel

Dim RegEx As RegExp
Dim objMatch As Match
Dim objMatches As MatchCollection
Dim submatches As submatches
Set RegEx = New RegExp
With RegEx
.Pattern = ".*(6\d{8})(.{0,20}).*"
.IgnoreCase = True
.Global = False
End With

' Source: Items to be processed
Set Sel2 = Outlook.Session.Folders("My Name").Folders("Inbox").Folders("caseinbox")
Set Sel = Sel2.Items
' Target folder
Set CaseFolders = Outlook.Session.Folders("My Name").Folders("Inbox").Folders("casetest")

For Each Item In Sel
Set objMatches = RegEx.Execute(Item.Subject)
' Run Regex against item subject
If objMatches.Count > 0 Then
fdr = objMatches.Item(0)
If CaseFolders.Folders(fdr) Is Nothing Then CaseFolders.Folders.Add fdr
Item.Move CaseFolders.Folders(fdr)
Else
MsgBox "No match found: " & Item.Subject
End If
Next

End Sub

卡在这部分:

    fdr = objMatches.Item(0)
If CaseFolders.Folders(fdr) Is Nothing Then CaseFolders.Folders.Add fdr

我将 fdr 定义为 String 以使其正常工作,但要搜索文件夹名称,它需要一个对象。我应该为 fdr 定义什么?

最佳答案

Dim RegEx As RegExp
Dim objMatch As Match
Dim objMatches As MatchCollection

如果该代码编译通过,那么您显然已经引用了正则表达式库; Macro Man's answer是正确的,您没有任何理由使用 CreateObject 来创建您可以轻松 New 的类的实例。

可以使用对象浏览器(VBE 中的F2)浏览所有引用的库:

Members of 'Match'

Match 类没有 Count 成员,因此当您键入此内容时(假设您键入了):

If objMatch.Count > 0 Then

您没有注意 IntelliSense 告诉您的内容:

IntelliSense listing members of Match in a dropdown


Execute 方法不返回Match 对象。当然这并不明显,因为 Execute 方法返回一个 Object,它可以是任何东西,对吧?使用 TypeName 你可以找出真相:

Sub DoSomething()

With New RegExp
.Pattern = "\w"
Dim result As Object
Set result = .Execute("foo bar")
Debug.Print TypeName(result)
End With

End Sub

打印 IMatchCollection2 - 显然由 MatchCollection 类型实现的接口(interface):Execute 因此返回一个匹配集合对象。

IMatchCollection hidden interface

所以不是这个:

Set objMatch = RegEx.Execute(Item.Subject)

这样做:

Set objMatches = RegEx.Execute(Item.Subject)

然后迭代该集合中的 Match 对象。

关于正则表达式错误无法编译 Match.Count,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42582509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com