我需要一个解决方案,将一个VBA脚本添加到Outlook中的规则,该规则在电子邮件主题行中的短语“was”之前搜索单词,然后使用该单词在指定文件夹中保存附件(excel文件)。
我熟悉InStr()方法,但无论出于何种原因,我都无法弄清楚如何使用当前代码。有人有什么想法吗?
这是我在添加这一新行之前的当前工作脚本:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim saveFolder2 As String
Dim attname As String
Dim fso As FileSystemObject
Dim stream As TextStream
saveFolder = "C:\Users\testuser\Desktop\Files\Reports"
saveFolder2 = "\\SERVER\C\Users\Service\Documents"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
attname = objAtt.DisplayName
Call updatelog(attname)
objAtt.SaveAsFile saveFolder2 & "\" & objAtt.DisplayName
Call updatelog(attname)
Set objAtt = Nothing
Next
由于
答案 0 :(得分:2)
这是一个通用函数,它将返回触发词之前的单词。
Function WordBeforeTrigger(sInput As String, sTrigger As String) As String
Dim vaSplit As Variant
Dim i As Long
Dim sReturn As String
vaSplit = Split(sInput, Space(1))
For i = LBound(vaSplit) + 1 To UBound(vaSplit)
If vaSplit(i) = sTrigger Then
sReturn = vaSplit(i - 1)
Exit For
End If
Next i
WordBeforeTrigger = sReturn
End Function
这是如何在立即窗口中使用它
?wordbeforetrigger("Report1 2012 was run on 7/11/2013 at 11:58:35pm","was")
2012
?wordbeforetrigger("I was home","was")
I
?wordbeforetrigger("","was")
?wordbeforetrigger("doesn't contain","was")
这些空白行是空字符串。您需要编码以解释何时找不到该单词并返回空字符串。如果Len(sReturn)= 0或类似的话,您可以修改函数以返回sInput。
您似乎想要传入DisplayName和“was”,它将返回该值。您可以将其存储在字符串变量中,并将其用作附件名称。
使用Instr没有任何问题,但你会注意到我没有。我更喜欢使用Split并导航生成的数组而不是Instr。