VBA Outlook Object has been removed after changing Application_ItemSend

I’m writing Outllok macro that moves sent email to specyfic folders.
Sub is working fine and it is doing the job however after Sub I’m receiving error that “Operation can’t be done, because object has been removed”.
I think that the Outlook is trying to move email to Sent email folder but there is none.
But that is my quess – maybe You can help to remove this prompt?

Code for Sub:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Private Sub Application_ItemSend(ByVal objItem As Object, Cancel As Boolean)
Dim mail As MailItem
Dim szukane As String
Dim linijek As Integer
Dim adres As String
Dim MyArr() As String
Dim folder As String
Dim myDestFolder As Outlook.folder
Dim myDestFolder1 As Outlook.folder
Dim folderGlowny As Outlook.folder
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.folder
Dim myOutbox As Outlook.folder
Dim myItems As Outlook.Items
Dim myItem As Object
adres = "C:Skryptyadresy.txt"
Const ForAppending = 8
Set Fso = CreateObject("Scripting.FileSystemObject")
Set theFile = Fso.OpenTextFile(adres, ForAppending, Create:=True)
linijek = theFile.Line
Set Fso = Nothing
ReDim Preserve MyArr(linijek, 2)
Dim FileNum As Integer
Dim DataLine As String
Dim i As Integer
i = 1
FileNum = FreeFile()
Open adres For Input As #FileNum
While Not EOF(FileNum)
Line Input #FileNum, DataLine
MyArr(i, 1) = Split(DataLine, ";")(0)
MyArr(i, 2) = Split(DataLine, ";")(1)
i = i + 1
Wend
If TypeName(objItem) = "MailItem" Then
Set mail = objItem
Dim odbiorca As Recipient
Set odbiorca = mail.Recipients(1)
For i = 1 To linijek
If MyArr(i, 1) = odbiorca.Address Then
folder = MyArr(i, 2)
Exit For
End If
Next
End If
' przenoszenie maila do folderu
If folder <> "Nothing" Then
Dim znaleziono As Boolean
znaleziono = False
Set myNameSpace = Application.GetNamespace("MAPI")
Set myDestFolder = myNameSpace.Folders(1)
Set myOutbox = myNameSpace.GetDefaultFolder(olFolderSentMail)
For Each myDestFolder1 In myDestFolder.Folders
If myDestFolder1.Name = "Korespondencja" Then
Set folderGlowny = myDestFolder1
Exit For
End If
Next
'Szukanie w głównych folderach
For Each myDestFolder1 In folderGlowny.Folders
If meDestFolder1 = folder Then
'przenieść maila tutaj
mail.Move meDestFolder1
znaleziono = True
Exit For
End If
Next
'Jeżeli w głównych brak tego folderu to przeglądamy podfoldery
Dim podFolder As Outlook.folder
For Each myDestFolder1 In folderGlowny.Folders
If myDestFolder1.Folders.Count > 0 Then
For Each podFolder In myDestFolder1.Folders
If podFolder.Name = folder Then
mail.Move podFolder
znaleziono = True
Exit For
End If
Next
End If
If znaleziono Then
Exit For
End If
Next
If Not znaleziono Then
MsgBox ("Nie znaleziono folderu " & folder)
End If
End If
</code>
<code>Private Sub Application_ItemSend(ByVal objItem As Object, Cancel As Boolean) Dim mail As MailItem Dim szukane As String Dim linijek As Integer Dim adres As String Dim MyArr() As String Dim folder As String Dim myDestFolder As Outlook.folder Dim myDestFolder1 As Outlook.folder Dim folderGlowny As Outlook.folder Dim myNameSpace As Outlook.NameSpace Dim myInbox As Outlook.folder Dim myOutbox As Outlook.folder Dim myItems As Outlook.Items Dim myItem As Object adres = "C:Skryptyadresy.txt" Const ForAppending = 8 Set Fso = CreateObject("Scripting.FileSystemObject") Set theFile = Fso.OpenTextFile(adres, ForAppending, Create:=True) linijek = theFile.Line Set Fso = Nothing ReDim Preserve MyArr(linijek, 2) Dim FileNum As Integer Dim DataLine As String Dim i As Integer i = 1 FileNum = FreeFile() Open adres For Input As #FileNum While Not EOF(FileNum) Line Input #FileNum, DataLine MyArr(i, 1) = Split(DataLine, ";")(0) MyArr(i, 2) = Split(DataLine, ";")(1) i = i + 1 Wend If TypeName(objItem) = "MailItem" Then Set mail = objItem Dim odbiorca As Recipient Set odbiorca = mail.Recipients(1) For i = 1 To linijek If MyArr(i, 1) = odbiorca.Address Then folder = MyArr(i, 2) Exit For End If Next End If ' przenoszenie maila do folderu If folder <> "Nothing" Then Dim znaleziono As Boolean znaleziono = False Set myNameSpace = Application.GetNamespace("MAPI") Set myDestFolder = myNameSpace.Folders(1) Set myOutbox = myNameSpace.GetDefaultFolder(olFolderSentMail) For Each myDestFolder1 In myDestFolder.Folders If myDestFolder1.Name = "Korespondencja" Then Set folderGlowny = myDestFolder1 Exit For End If Next 'Szukanie w głównych folderach For Each myDestFolder1 In folderGlowny.Folders If meDestFolder1 = folder Then 'przenieść maila tutaj mail.Move meDestFolder1 znaleziono = True Exit For End If Next 'Jeżeli w głównych brak tego folderu to przeglądamy podfoldery Dim podFolder As Outlook.folder For Each myDestFolder1 In folderGlowny.Folders If myDestFolder1.Folders.Count > 0 Then For Each podFolder In myDestFolder1.Folders If podFolder.Name = folder Then mail.Move podFolder znaleziono = True Exit For End If Next End If If znaleziono Then Exit For End If Next If Not znaleziono Then MsgBox ("Nie znaleziono folderu " & folder) End If End If </code>
Private Sub Application_ItemSend(ByVal objItem As Object, Cancel As Boolean)
Dim mail As MailItem
Dim szukane As String
Dim linijek As Integer
Dim adres As String
Dim MyArr() As String
Dim folder As String
Dim myDestFolder As Outlook.folder
Dim myDestFolder1 As Outlook.folder
Dim folderGlowny As Outlook.folder
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.folder
Dim myOutbox As Outlook.folder
Dim myItems As Outlook.Items
Dim myItem As Object

adres = "C:Skryptyadresy.txt"


Const ForAppending = 8
Set Fso = CreateObject("Scripting.FileSystemObject")
Set theFile = Fso.OpenTextFile(adres, ForAppending, Create:=True)
linijek = theFile.Line
Set Fso = Nothing

ReDim Preserve MyArr(linijek, 2)
Dim FileNum As Integer
Dim DataLine As String
Dim i As Integer
i = 1
FileNum = FreeFile()
Open adres For Input As #FileNum
While Not EOF(FileNum)
    Line Input #FileNum, DataLine
    MyArr(i, 1) = Split(DataLine, ";")(0)
    MyArr(i, 2) = Split(DataLine, ";")(1)
    i = i + 1
Wend
        
If TypeName(objItem) = "MailItem" Then
    Set mail = objItem
    Dim odbiorca As Recipient

    Set odbiorca = mail.Recipients(1)
   
    For i = 1 To linijek
        If MyArr(i, 1) = odbiorca.Address Then
            folder = MyArr(i, 2)
            Exit For
        End If
    Next
End If
' przenoszenie maila do folderu
If folder <> "Nothing" Then
    Dim znaleziono As Boolean
    znaleziono = False
    Set myNameSpace = Application.GetNamespace("MAPI")
    Set myDestFolder = myNameSpace.Folders(1)
    Set myOutbox = myNameSpace.GetDefaultFolder(olFolderSentMail)
    
    For Each myDestFolder1 In myDestFolder.Folders
        If myDestFolder1.Name = "Korespondencja" Then
            Set folderGlowny = myDestFolder1
            Exit For
        End If
    Next
    'Szukanie w głównych folderach
    For Each myDestFolder1 In folderGlowny.Folders
        If meDestFolder1 = folder Then
            'przenieść maila tutaj
            mail.Move meDestFolder1
            znaleziono = True
            Exit For
        End If
    Next
    'Jeżeli w głównych brak tego folderu to przeglądamy podfoldery
    Dim podFolder As Outlook.folder
    
    
    For Each myDestFolder1 In folderGlowny.Folders
        If myDestFolder1.Folders.Count > 0 Then
            For Each podFolder In myDestFolder1.Folders
                If podFolder.Name = folder Then
                    mail.Move podFolder
                    znaleziono = True
                    Exit For
                End If
            Next
        End If
        If znaleziono Then
            Exit For
        End If
    Next
    If Not znaleziono Then
        MsgBox ("Nie znaleziono folderu " & folder)
    End If
End If

End Sub

5

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật