Saturday, December 29, 2012

outlook邮件自动转发到Gmail

Private Sub Application_NewMail()
 Dim myItem As Outlook.MailItem
 Set myItem = Application.ActiveExplorer.Selection.Item(1).Forward
 myItem.Recipients.Add "xxxxxxg@gmail.com"
 myItem.Send
 Set myItem = Nothing
End Sub

上面这个的问题是转发选中的邮件而不是新接受的
更新之后的版本:
Public WithEvents myOlItems As Outlook.Items
Public Sub Application_Startup()
' Reference the items in the Inbox. Because myOlItems is declared ' "WithEvents" the ItemAdd event will fire below.
Set myOlItems = Outlook.Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub myOlItems_ItemAdd(ByVal Item As Object)
If TypeName(Item) = "MailItem" Then
    Set myForward = Item.Forward
    myForward.Recipients.Add "xxxx@gmail.com"
    myForward.Send
End If
End Sub

No comments:

Post a Comment