Macro Not Executing

Status
Not open for further replies.

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
I am using Items_ItemAdd(byVal Item As Object) to monitor the sent items folder and when a new item is added a userform loads. The userform has several buttons each with their own macro attached. One of the macros selects the item that has been added to the sent items folder and then saves it in MSG format to a user selected folder on a server. If you create a new message and send it all works fine. However if you have say an incoming email open in a separate window and reply or forward this message, the userform loads but the macro won't execute. The problem seems to be due to the email being open in a window. Any suggestions on how to overcome this proble. I have included the macro below that won't run.

' saves email after sending if option to do so is activated
Sub sendandfile()
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myItem As Outlook.MailItem
Dim myItems As Outlook.Items
Dim lngC As Long
Dim msgItem As Outlook.MailItem
Dim strPath As String

Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderSentMail)
Set myItems = myFolder.Items
myItems.Sort "[SentOn]", True
Set myItem = myItems(1)

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

If TypeName(Application.ActiveWindow) = "Explorer" Then
With myItem
If UserForm4.TextBox3.Value = "NO" Then
.Subject = "[Filed" & " " & Date & "]" & " " & myItem.Subject
.Save
Else
.Subject = myItem.Subject
.Save
End If
End With
' save last sent message
If myItem.Class = olMail Then
On Error GoTo ErrHandler
MsgSaverSAF strPath, myItem
On Error GoTo 0
End If
End If
Exit Sub
ErrHandler:
MsgBox "The selected folder does not exist. Please select a valid folder or browse to a folder.", vbExclamation + vbOKOnly
' go back to the line following the error
Unload UserForm6
UserForm6.Show
End Sub
Private Sub MsgSaverSAF(strPath As String, myItem As Outlook.MailItem)
Dim intC As Integer
Dim intD As Integer
Dim strMsgSubj As String
Dim strMsgTo As String
strMsgSubj = Left(myItem.Subject, 80)
strMsgTo = Left(myItem.To, 25)
' 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(strMsgTo)
If InStr(1, ":<&>""", Mid(strMsgTo, intD, 1)) > 0 Then
Mid(strMsgTo, intD, 1) = "-"
End If
Next intD
For intD = 1 To Len(strMsgTo)
If InStr(1, "\/|*?", Mid(strMsgTo, intD, 1)) > 0 Then
Mid(strMsgTo, intD, 1) = "_"
End If
Next intD
' add date to file name
strMsgSubj = Format(myItem.senton, "yyyy-mm-dd Hh.Nn.Ss") & " " & "[To " & strMsgTo & "]" & " " & strMsgSubj & ".msg"
myItem.SaveAs strPath & strMsgSubj
If UserForm1.TextBox3.Value = "YES" Then
myItem.Delete
End If
Set myItem = Nothing
Unload UserForm6
End Sub
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
It looks like you are grabbing the active open message - use the itemadd macro to assign the newly added message to an object variable then reference that variable.
Do you need this line? Removing this If and working with the copy you assigned to myItem variable earlier should work...
If TypeName(Application.ActiveWindow) = "Explorer" Then

I would probably try to do it this way:
Outside of the macros, declare myItem as outlook.mailitem (and remove the dim that is inside of the macro) then in the itemadd macro, set myItem = item. You won't have to search for the last item added to work with it.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
?This sample code changes the subject of the sent message - update the form code and the changesubject macro with your code that saves the message and it should work...

Thisoutlooksession:
Code:
Option Explicit
Private WithEvents Items As Outlook.Items

Private Sub Application_Startup()
Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")
Set Items = Ns.GetDefaultFolder(olFolderSentMail).Items
End Sub

Private Sub Items_ItemAdd(ByVal Item As Object)
If TypeOf Item Is Outlook.MailItem Then
Set myItem = Item
ChangeSubject
End If

End Sub

Code behind the form
Code:
Private Sub UserForm_Initialize()
    With ComboBox1
    .AddItem "Subject 1"
    .AddItem "Subject 2"
    .AddItem "Subject 3"
    .AddItem "Subject 4"
    .AddItem "Subject 5"
    .AddItem "no change"
    End With
End Sub


Private Sub CommandButton1_Click()
    lstNo = ComboBox1.ListIndex
    Unload Me
End Sub

This is in the module:
Code:
Public lstNo As Long
Public myItem As Outlook.MailItem

Public Sub ChangeSubject()
  UserForm1.Show

Select Case lstNo
    Case -1
         myItem.Subject = myItem.Subject
    Case 0
         myItem.Subject = "Subject 1"
    Case 1
        myItem.Subject = "Subject 2"
    Case 2
       myItem.Subject = "Subject 3"
    Case 3
       myItem.Subject = "Subject 4"
    Case 4
       myItem.Subject = "Subject 5"
    End Select
   
    myItem.Save
End Sub
 

Alan McGowan

Senior Member
Outlook version
Outlook 2013 64 bit
Email Account
Exchange Server
Diane, yes that line of code was the problem. Can't believe I didn't spot it. Anyway changing the block of code that started with that line to the following , solved the problem. Thanks very much

If UserForm4.TextBox3.Value = "NO" Then
myItem.Subject = "[Filed" & " " & Date & "]" & " " & myItem.Subject
myItem.Save
Else
myItem.Subject = myItem.Subject
myItem.Save
End If
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
K Macro Not Executing then send email from Explorer Outlook VBA and Custom Forms 3
V Macro to count flagged messages? Using Outlook 2
sophievldn Looking for a macro that moves completed items from subfolders to other subfolder Outlook VBA and Custom Forms 7
S Outlook Macro for [Date][Subject] Using Outlook 1
E Outlook - Macro - send list of Tasks which are not finished Outlook VBA and Custom Forms 3
E Macro to block senders domain Outlook VBA and Custom Forms 1
D VBA Macro to Print and Save email to network location Outlook VBA and Custom Forms 1
N VBA Macro To Save Emails Outlook VBA and Custom Forms 1
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
witzker Outlook 2019 Macro to answer a mail with attachments Outlook VBA and Custom Forms 2
A Outlook 2016 Macro to Reply, ReplyAll, or Forward(but with composing new email) Outlook VBA and Custom Forms 0
J Macro to Insert a Calendar Outlook VBA and Custom Forms 8
W Macro to Filter Based on Latest Email Outlook VBA and Custom Forms 6
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
D Autosort macro for items in a view Outlook VBA and Custom Forms 2
S HTML to Plain Text Macro - Help Outlook VBA and Custom Forms 1
A Macro to file emails into subfolder based on subject line Outlook VBA and Custom Forms 1
N Help creating a VBA macro with conditional formatting to change the font color of all external emails to red Outlook VBA and Custom Forms 5
S Visual indicator of a certain property or to show a macro toggle Outlook VBA and Custom Forms 2
L Modifying VBA script to delay running macro Outlook VBA and Custom Forms 3
S Macro to extract and modify links from emails Outlook VBA and Custom Forms 3
M Replyall macro with template and auto insert receptens Outlook VBA and Custom Forms 1
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 17
S Macro for Loop through outlook unread emails Outlook VBA and Custom Forms 2
Globalforester ItemAdd Macro - multiple emails Outlook VBA and Custom Forms 3
S Macro to extract email addresses of recipients in current drafted email and put into clipboard Outlook VBA and Custom Forms 2
witzker HowTo start a macro with an Button in OL contact form Outlook VBA and Custom Forms 12
witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
S Macro for other actions - Outlook 2007 Outlook VBA and Custom Forms 23
C Macro to extract sender name & subject line of incoming emails to single txt file Outlook VBA and Custom Forms 3
L Macro/VBA to Reply All, with the original attachments Outlook VBA and Custom Forms 2
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
S Outlook Macro to send auto acknowledge mail only to new mails received to a specific shared inbox Outlook VBA and Custom Forms 0
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
S Macro or plug-in to see if specific person was included in this email Outlook VBA and Custom Forms 4
U Macro for reminders,tasks,calendar Outlook VBA and Custom Forms 4
V macro runs slower on startup than after Outlook VBA and Custom Forms 3
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
A VBA macro for 15 second loop in send and received just for 1 specific mailbox Outlook VBA and Custom Forms 1
G VBA Macro Using Outlook 4
R Help Revising VBA macro to delete email over different time span Outlook VBA and Custom Forms 0
M Outlook macro to automate search and forward process Outlook VBA and Custom Forms 6
R Macro Schedule every day in Outlook Using Outlook 0
L Moving emails with similar subject and find the timings between the emails using outlook VBA macro Outlook VBA and Custom Forms 1
Healy Consultants Macro to remove inside organization distribution list email address when reply to all recepients Outlook VBA and Custom Forms 0
geofferyh Cannot get Macro to SAVE more than one message attachment??? Outlook VBA and Custom Forms 5
N How can I increase/faster outlook VBA Macro Speed ? Using Outlook 2
4 Macro to set the category of Deleted Item? Outlook VBA and Custom Forms 2
D.Moore Folder view settings by VBA macro Outlook VBA and Custom Forms 57

Similar threads

Top