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
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 1
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 2
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 Calendar Printing Assistant 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
A Outlook macro to create search folder with mail categories as criteria Outlook VBA and Custom Forms 3
Dave A Run macro on existing appointment when it changes Outlook VBA and Custom Forms 1
V Outlook Macro to show Flagged messages Outlook VBA and Custom Forms 2
O Run macro automatically at sending an email Using Outlook 11
R Retain Original Message When Forwarding With Macro Outlook VBA and Custom Forms 3
C Macro to add multiple recipients to message Outlook VBA and Custom Forms 3
B Reply and replyall macro is not working Outlook VBA and Custom Forms 1
O Macro - paste as plain text Outlook VBA and Custom Forms 2
J Help Please!!! Outlook 2016 - VBA Macro for replying with attachment in meeting invite Outlook VBA and Custom Forms 9
witzker Macro to set contact reminder to next day 9:00 Outlook VBA and Custom Forms 45
M Adding Macro to populate "to" "subject" "body" not deleting email string below. Outlook VBA and Custom Forms 5
E Copying data from e-mail attachement to EXCEL file via macro Outlook VBA and Custom Forms 38
M Macro to add date/time stamp to subject Outlook VBA and Custom Forms 4
R VBA macro - new message Outlook VBA and Custom Forms 3
S Example VBA Macro - To Conditionally Change the From Account and Add a BCC Address on Emails Outlook VBA and Custom Forms 11
S Macro using .SendUsingAccount only works the first time, after starting Outlook Outlook VBA and Custom Forms 4
S VBA Macro - Run-time error '424': object required - Help Please Outlook VBA and Custom Forms 3
B VBA Macro for assigning multiple Categories to an email in my Inbox Outlook VBA and Custom Forms 1

Similar threads

Top