Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    May 2015
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Problem with msgitem.Delete

    I have written some code to save selected emails in a specific filenaming format to a specified folder. I have this running from a userform. I have added a check box and want to include some code that will delete the emails after they have saved if the check box is ticked. I have used the following code:

    If UserForm1.CheckBox1.Value = True Then

    msgItem.Delete

    End If

    However, when I run the macro I get an error stating "Outlook cannot delete this item". Any ideas?

  2. #2
    jwoods
    Guest
    I'm thinking "msgItem" is an array of 0 to n (total count of messages) items, so you may need iterate through it to delete the items.

  3. #3
    New Lounger
    Join Date
    May 2015
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I don't think thats the issue as if the checkbox value is false then the following code runs successfully

    msgItem.Subject = FiledSubject

    changing the email subject line in outlook to the value of FiledSubject

  4. #4
    jwoods
    Guest
    Quote Originally Posted by scoobydoo74 View Post
    I don't think thats the issue as if the checkbox value is false then the following code runs successfully

    msgItem.Subject = FiledSubject

    changing the email subject line in outlook to the value of FiledSubject
    Looks like you're changing the subject for a single email.

    If I understand correctly, your original post seemed to imply deleting multiple emails after they have been saved.

    This code might be helpful...

    http://www.robvanderwoude.com/vbstec...on_outlook.php
    Last edited by jwoods; 2015-05-18 at 15:59.

  5. #5
    New Lounger
    Join Date
    May 2015
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Your absolutely correct. I've just noticed that whilst the code is saving the multiple selected emails it is only changing the subject line on the first of the selected emails. I'll need to dig deeper but struggling as this is my first attempt at coding and using snippets I've picked up from different sources. This is the last piece in the jigsaw and the code will then do exactly what I want

  6. #6
    New Lounger
    Join Date
    May 2015
    Posts
    11
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Changing Email Subject using VBA

    I am using the code below to save multiple selected emails in a standard file naming format in a folder, who's path is selected from a text box (textbox1). Depending on whether a checkbox (checkbox1) is selected or not will determine whether the emails are deleted after running the code. If the the checkbox is not selected then the emails are saved to the folder but not deleted from Outlook. If the checkbox is not selected then I want the email subject in Outlook to be changed in order that I know that I have previously saved the email. The code below pretty much does everything I want except changing the email subject. If I select only one email all works fine. However if I select more than one email then only the subject of the first email gets changed. Any help appreciated.

    Sub SaveIncoming()
    Dim lngC As Long
    Dim msgItem As Outlook.MailItem
    Dim strPath As String
    Dim FiledSubject As String

    On Error Resume Next
    strPath = UserForm1.TextBox1.Value
    On Error GoTo 0
    If strPath = "" Then Exit Sub
    If Right(strPath, 1) <> "\" Then strPath = strPath & "\"

    If TypeName(Application.ActiveWindow) = "Explorer" Then
    ' save selected messages in Explorer window
    If CBool(ActiveExplorer.Selection.Count) Then
    With ActiveExplorer
    For lngC = 1 To .Selection.Count
    If .Selection(lngC).Class = olMail Then
    MsgSaver3 strPath, .Selection(lngC)

    If UserForm1.CheckBox1.Value = True Then

    .Selection(lngC).Delete

    End If

    If UserForm1.CheckBox1.Value = False Then

    FiledSubject = "[Filed" & " " & Date & "]" & " " & .Selection(lngC).Subject

    .Selection(lngC).Subject = FiledSubject

    End If

    End If
    Next lngC
    End With
    End If
    ElseIf Inspectors.Count Then
    ' save active open message
    If ActiveInspector.CurrentItem.Class = olMail Then
    MsgSaver3 strPath, ActiveInspector.CurrentItem
    End If
    End If
    End Sub

    Private Sub MsgSaver3(strPath As String, msgItem As Outlook.MailItem)
    Dim intC As Integer
    Dim intD As Integer
    Dim strMsgSubj As String
    Dim strMsgFrom As String
    strMsgSubj = msgItem.Subject
    strMsgFrom = msgItem.SenderName
    ' Clean out characters from Subject which are not permitted in a file name
    For intC = 1 To Len(strMsgSubj)
    If InStr(1, ":<>""", Mid(strMsgSubj, intC, 1)) > 0 Then
    Mid(strMsgSubj, intC, 1) = "-"
    End If
    Next intC
    For intC = 1 To Len(strMsgSubj)
    If InStr(1, "\/|*?", Mid(strMsgSubj, intC, 1)) > 0 Then
    Mid(strMsgSubj, intC, 1) = "_"
    End If
    Next intC

    ' Clean out characters from Sender Name which are not permitted in a file name
    For intD = 1 To Len(strMsgFrom)
    If InStr(1, ":<>""", Mid(strMsgFrom, intD, 1)) > 0 Then
    Mid(strMsgFrom, intD, 1) = "-"
    End If
    Next intD
    For intD = 1 To Len(strMsgFrom)
    If InStr(1, "\/|*?", Mid(strMsgFrom, intD, 1)) > 0 Then
    Mid(strMsgFrom, intD, 1) = "_"
    End If
    Next intD
    ' add date to file name
    strMsgSubj = Format(msgItem.SentOn, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[From " & strMsgFrom & "]" & " " & strMsgSubj & ".msg"
    msgItem.SaveAs strPath & strMsgSubj
    Set msgItem = Nothing
    UserForm1.Hide
    End Sub

  7. #7
    Super Moderator satrow's Avatar
    Join Date
    Dec 2009
    Location
    Cardiff, UK
    Posts
    4,490
    Thanks
    284
    Thanked 576 Times in 479 Posts
    [Merged threads] #6 appears to contain the code for the macro in the OP.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •