2013-08-22 71 views
1

删除invlaid角色,我有以下代码从电子邮件主题

Sub RemoveInvalidChar(myMail As MailItem) 


If myMail.Subject <> vbNullString Then 
     strname = myMail.Subject 
    Else 

End If 
strname = Replace(strname, "&", "and") 

End Sub 

基本上我想用字和使用规则,以取代&字符。似乎没有工作。

回答

0
Sub RemoveInvalidChar(myMail As MailItem) 


If myMail.Subject <> vbNullString Then 
     'set the actual subject to a new subject 

     myMail.Subject= Replace(myMail.Subject, "&", "and") 
     'get in the habit of saving email when you change it via vba 
     'as there will be a lot of cases where you get burned for this 
     myMail.Save 
End If 

End Sub 

如果这不起作用,请添加一个断点并确保实际上是通过规则调用宏。

1

您尚未将myMail.Subject设置为您的新字符串。添加下面的行:

myMail.Subject = strname 

但请确保您发送 ByRef。

+0

似乎并没有做任何事情, – Silentbob

+0

你发送myMail对象的ByRef: '子RemoveInvalidChar(为ByRef myMail作为的MailItem)' – Mark

+0

是的,这就是我'子RemoveInvalidChar(为ByRef myMail作为的MailItem ) 如果myMail.Subject <> vbNullString然后 则strName = myMail.Subject 否则 结束如果 myMail.Subject =则strName 则strName = REPLACE(则strName, “&”, “和”) 完Sub' – Silentbob

0

恩德兰 - 我设法昨天得到答案,但无法发布结果。

我错过了mymail.save

反正我的代码如下,它的工作原理。 Ltrim只是从主题行的前面删除任何前面的空格。

Sub RemoveInvalidChar(myMail As MailItem) 

    Dim strname As String 
    Dim sreplace As String 

    sreplace = "and" 
    strname = LTrim(myMail.Subject) 

    strname = Replace(strname, "&", sreplace) 
    myMail.Subject = strname 
    myMail.Save 

End Sub 
+0

就像我说的,'.Save'是一个非常令人沮丧和相当普遍的问题,用Outlook做这样的事情...... – enderland