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
 
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.
 
?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
 
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
X Custom icon (not from Office 365) for a macro in Outlook Outlook VBA and Custom Forms 1
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
mrrobski68 Issue with Find messages in a conversation macro Outlook VBA and Custom Forms 1
G Creating Macro to scrape emails from calendar invite body Outlook VBA and Custom Forms 6
M Use Macro to change account settings Outlook VBA and Custom Forms 0
J Macro to Reply to Emails w/ Template Outlook VBA and Custom Forms 3
C Outlook - Macro to block senders domain - Macro Fix Outlook VBA and Custom Forms 1
Witzker Outlook 2019 Macro to seach in all contact Folders for marked Email Adress Outlook VBA and Custom Forms 1
S macro error 4605 Outlook VBA and Custom Forms 0
A Macro Mail Alert Using Outlook 4
J Outlook 365 Outlook Macro to Sort emails by column "Received" to view the latest email received Outlook VBA and Custom Forms 0
J Macro to send email as alias Outlook VBA and Custom Forms 0
M Outlook Macro to save as Email with a file name format : Date_Timestamp_Sender initial_Email subject Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro GoTo user defined search folder Outlook VBA and Custom Forms 6
D Outlook 2016 Creating an outlook Macro to select and approve Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to send an Email Template from User Defined Contact Form Outlook VBA and Custom Forms 0
Witzker Outlook 2019 Macro to check Cursor & Focus position Outlook VBA and Custom Forms 8
V Macro to mark email with a Category Outlook VBA and Custom Forms 4
M Outlook 2019 Macro not working Outlook VBA and Custom Forms 0
S Outlook 365 Help me create a Macro to make some received emails into tasks? Outlook VBA and Custom Forms 1
Geldner Send / Receive a particular group via macro or single keypress Using Outlook 1
D Auto Remove [EXTERNAL] from subject - Issue with Macro Using Outlook 21
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

Similar threads

Back
Top