2011-10-18 36 views
0

試圖獲得一些VBA代碼在一起,基本上能夠從Outlook 2007中的工具欄上的按鈕運行我的規則。以下代碼運行我的Exchange服務器收件箱中的規則,它是空的一切都轉移到我的「個人收件箱」。我只是想更改下面的代碼來閱讀我的個人收件箱,而不是我的Exchange郵箱收件箱。已經在網上搜索,無法找到我的答案,因此我的帖子 -設置VBA讀取個人收件箱

Sub RunAllInboxRules() 
Dim st As Outlook.Store 
Dim myRules As Outlook.Rules 
Dim rl As Outlook.Rule 
Dim count As Integer 
Dim ruleList As String 
'On Error Resume Next 


' get default store (where rules live) 
Set st = Application.Session.DefaultStore 
' get rules 
Set myRules = st.GetRules 

' iterate all the rules 
For Each rl In myRules 
    ' determine if it's an Inbox rule 
    If rl.RuleType = olRuleReceive Then 
     ' if so, run it 
     rl.Execute ShowProgress:=True 
     count = count + 1 
     ruleList = ruleList & vbCrLf & rl.Name 
    End If 
Next 

' tell the user what you did 
ruleList = "These rules were executed against the Inbox: " & vbCrLf & ruleList 
MsgBox ruleList, vbInformation, "Macro: RunAllInboxRules" 

Set rl = Nothing 
Set st = Nothing 
Set myRules = Nothing 
End Sub 

回答

0

試試這個。我在我的機器上測試過。這將登錄到您登錄的郵箱並相應地運行規則

Sub RunAllInboxRules() 
Dim objOL As Outlook.Application 
Dim st As Outlook.Store 
Dim myRules As Outlook.Rules 
Dim rl As Outlook.Rule 
Dim count As Integer 
Dim ruleList As String 
Dim fldInbox As Object 
Dim gnspNameSpace As Outlook.NameSpace 

'On Error Resume Next 

' get default store (where rules live) 

'Logs into Outlook session 
    Set objOL = Outlook.Application 
    Set gnspNameSpace = objOL.GetNamespace("MAPI") 'Outlook Object 
    'Logs into the default Mailbox Inbox 

'set the store to the mailbox 

Set st = gnspNameSpace.GetDefaultFolder(olFolderInbox).Store 

' get rules 

Set myRules = st.GetRules 

' iterate all the rules 
For Each rl In myRules 
    ' determine if it's an Inbox rule 
    If rl.RuleType = olRuleReceive Then 
     ' if so, run it 
     rl.Execute ShowProgress:=True 
     count = count + 1 
     ruleList = ruleList & vbCrLf & rl.Name 
    End If 
Next 

' tell the user what you did 
ruleList = "These rules were executed against the Inbox: " & vbCrLf & ruleList 
MsgBox ruleList, vbInformation, "Macro: RunAllInboxRules" 

Set rl = Nothing 
Set st = Nothing 
Set myRules = Nothing 
End Sub