我正在尝试获取我在MS Outlook中设置的规则的条件的文本值。
我创建了一个名为“ TestRule”的规则,其条件基于消息正文中的文本(必须在其中包含文本“ zzz”),并且还基于消息标题中的文本(其必须具有文本“ aaa”) “ 在里面)。(当然,此规则可能永远不会运行,这只是为了测试我是否可以读取该规则中的条件。)已启用该规则和条件。
这是我正在使用的代码
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("TestRule")
Debug.Print olRule.Conditions.Body.Text
Debug.Print olRule.Conditions.MessageHeader.Text
但是,两条debug.print行均给出“类型不匹配”错误。
如何读取条件的当前值?
(我已经检查,并仔细检查:有是,一个名为“TestRule”规则)
GD DRC,
我已经为解决您的问题付出了更多的努力,并且我想出了为什么您会遇到类型不匹配的问题。
您可能已在“ body”条件以及“ messageheader”条件中添加了一些关键字?这些单独的条目将添加到数组“ olRule.Conditions.Body.Text”中。
您下面的代码行;
olRule.Conditions.Body.Text
Debug.Print olRule.Conditions.MessageHeader.Text
本质上是在要求'debug.print'方法来打印一个数组,但是它不知道该怎么做。由于它不了解要打印数组的哪一部分。根据您输入的条目数量,每个条目的标识符必定需要一个标识符来指向唯一的数组条目。
所以:
有两种方法可以解决您的编码问题:
在行上设置一个断点:
olRule.Conditions.Body.Text
这样执行就在执行代码行之前停止。打开“本地窗口”(通过“视图” |“本地窗口”),然后打开olRule.Text,您将看到添加的数组条目。
主子程序
Sub TestRule()
Dim olRules As Outlook.Rules
Dim olRule As Outlook.Rule
Set olRules = Application.Session.DefaultStore.GetRules
Set olRule = olRules.Item("TestRule")
Debug.Print TypeName(olRule)
printArray olRule.Conditions.Body.Text
printArray olRule.Conditions.MessageHeader.Text
End Sub
printArray子例程
Private Sub printArray(ByRef pArr As Variant)
Dim readString As Variant
If (IsArray(pArr)) Then 'check if the passed variable is an array
For Each readString In pArr
If TypeName(readString) = "String" Then 'check if the readString is a String variable
Debug.Print readString
End If
Next
End If
End Sub
您还可以将第二个子例程集成到第一个子例程中,但是更好的编码实践是在Function或单独的子例程中执行重复性的任务(然后您也可以将其用于同一模块中的其他方法:-)
相信这会为您提供解决方案!
祝你好运 !
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句