Outlook 2013 Macros only run in VB editor, not in drop down or button

Post number 6 has been selected as the best answer.

Status
Not open for further replies.

deuxn

New Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
I've created two macros. Both run when VB Editor is open, but not anywhere else. In the first macro I assigned a button to it, but it doesn't run. The second doesn't run from the macro dropdown. Any thoughts? Using desktop version of Outlook for MS 365 MS, 64-bit. Macro security is set to Enable all macros.

Code #1 - Purpose: To change category of Focus Time appointments
Option Explicit
' Change Insight's Focus Time Appointments
' Source: Change Insight's Focus Time Appointments
Public Sub ChangeInsights()
Dim calFolder As folder
Dim CalItems As Outlook.Items
Dim ResItems As Outlook.Items
Dim sFilter As String
Dim Appt As AppointmentItem 'Object
Dim mystart As Date

mystart = Date

Set calFolder = Session.GetDefaultFolder(olFolderCalendar)
Set CalItems = calFolder.Items

'Sort all of the appointments based on the start time
CalItems.Sort "[Start]"
On Error Resume Next
sFilter = "[Start] >='" & Format(mystart, "m/d/yy") & "' AND [Subject] = Focus Time"
Set ResItems = CalItems.Restrict(sFilter)

'Loop through the items in the collection.
For Each Appt In ResItems
With Appt
.Categories = "Deep Work"
'.ReminderSet = False
'.BusyStatus = olTentative
.Save
End With
Next
Set Appt = Nothing
End Sub

Code #2 - Purpose: To delete email address in the body of a forwarded email.
Sub DeleteTos()
Dim outNS As Outlook.NameSpace
Dim Item As Outlook.MailItem
Dim strTo As String
Dim strSubject As String

strTo = "To:"
strSubject = "Subject"

Set outNS = Application.GetNamespace("MAPI")
Set Item = GetCurrentItem()

Start = InStr(1, Item.body, strTo)
Done = InStr(1, Item.body, strSubject)

Remove = Done - Start

emails = Mid(Item.body, Start, Remove)
With Item
.body = Replace(Item.body, emails, "")
End With

Set outNS = Nothing
End Sub
Function GetCurrentItem() As Object
Dim objApp As Outlook.Application

Set objApp = Application
On Error Resume Next
Select Case TypeName(objApp.ActiveWindow)
Case "Explorer"
Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
Case "Inspector"
Set GetCurrentItem = objApp.ActiveInspector.currentItem
End Select

Set objApp = Nothing
End Function

Thanks!
 
Yes, it's already set to Enable all macros. Thanks.
 
The second one might need an item.save but otherwise both should work from buttons. Did you change the name after creating the button?


I tested both - they work here, so the macros are fine (which you knew, since they work from the editor). do you have them in ThisOutlookSession? If so, put them in a module. It usually doesn't make a difference though.
 
Thanks for the suggestion; I added item.save to the the second macro.

I modified the Display Name of the macro button in in the QAT, but it doesn't make a difference if I add the macro button without the change. When I go to the Developer Tab > Macros dropdown > Project1.ChangeInsights, nothing happens. However when I go to Macros > Macros > Select "ChangeInsights" then run, I get an error message "Sub or Function not defined." Same thing happens with the DeleteTos macro.
 
I figured out the issue. I gave the modules the same names as the macro. Once I changed them both the code worked outside the VB Editor. Thanks!
 
Ah... I've done that before - mostly when testing macros for others - maybe I never tried running them from outlook.
 
A follow up question: when I run the code the line breaks between emails disappear. What change do I need make to the code to maintain the line breaks? Thanks.
 
That is happening in the second macro? I don't see anything in this that would remove line breaks- but if the messages are HTML, it might happen as the item.body is plain text, no HTML code for line breaks. Try using item.htmlbody or use item.BodyFormat = olFormatHTML

Code:
Start = InStr(1, Item.body, strTo)
Done = InStr(1, Item.body, strSubject)

Remove = Done - Start

emails = Mid(Item.body, Start, Remove)
With Item
.body = Replace(Item.body, emails, "")
End With
 
That is happening in the second macro? I don't see anything in this that would remove line breaks- but if the messages are HTML, it might happen as the item.body is plain text, no HTML code for line breaks. Try using item.htmlbody or use item.BodyFormat = olFormatHTML

Code:
Start = InStr(1, Item.body, strTo)
Done = InStr(1, Item.body, strSubject)

Remove = Done - Start

emails = Mid(Item.body, Start, Remove)
With Item
.body = Replace(Item.body, emails, "")
End With
Sorry I didn't specify; yes, it's the second macro. I tried adding "item.htmlbody" however the entire email started to show the HTML code. Oh well. Thanks!
 
I'll test it in the morning -
 
Apologies for the very late reply. I'm still having issues with this code clearing all the formatting. I tried replacing "body" with "HTMLbody." However the entire email become HTML code. I'm trying to remove all the emails in "To:" when forwarding an email. Thanks.
Code:
Sub DeleteTos()
Dim outNS As Outlook.NameSpace
Dim Item As Outlook.MailItem
Dim strTo As String
Dim strSubject As String

    strTo = "To:"
    strSubject = "Subject"
    
    Set outNS = Application.GetNamespace("MAPI")
    Set Item = GetCurrentItem()
      
    Start = InStr(1, Item.Body, strTo)
    Done = InStr(1, Item.Body, strSubject)
    
    Remove = Done - Start
    
    emails = Mid(Item.Body, Start, Remove)
    With Item
         .Body = Replace(Item.Body, emails, "")
         .BodyFormat = olFormatHTML
        .Save
    End With
  
        
Set outNS = Nothing
End Sub
Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
          
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.currentItem
    End Select
      
    Set objApp = Nothing
End Function
 
Is this what you want - the top is in the normal reply - the bottom is after running the macro.

1645722135434.png


The issue with your code was not replacing all .Body with .HTMLBody - and you don't need to set the format.

Code:
Sub DeleteTos()
Dim outNS As Outlook.NameSpace
Dim Item As Outlook.MailItem
Dim strTo As String
Dim strSubject As String

    strTo = "To:"
    strSubject = "Subject"
   
    Set outNS = Application.GetNamespace("MAPI")
    Set Item = GetCurrentItem()
     
    Start = InStr(1, Item.HTMLBody, strTo)
    Done = InStr(1, Item.HTMLBody, strSubject)
   
    Remove = Done - Start
   
    emails = Mid(Item.HTMLBody, Start, Remove)
    With Item
         .HTMLBody = Replace(Item.HTMLBody, emails, "")
    '     .BodyFormat = olFormatHTML
        .Save
    End With
 
       
Set outNS = Nothing
End Sub
Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
         
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.currentItem
    End Select
     
    Set objApp = Nothing
End Function
 
Ah, thank you! I think I missing changing " .Body = Replace( ..." to ".HTMLBody = Replace( ..."
 
That's the one I missed the first time I ran it too. :)
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
V Outlook macros no longer run until VB editor is opened Outlook VBA and Custom Forms 0
V Run multiple Macros or macros from within other macros ? Outlook VBA and Custom Forms 2
P Macros Do Not Run Outlook VBA and Custom Forms 1
S Why do I have to close and reopen Outlook for macros to work? Outlook VBA and Custom Forms 2
S Email Macros to go to a SHARED Outlook mailbox Draft folder...NOT my personal Outlook Draft folder Using Outlook 2
J Want to learn VBA Macros for Outlook. What book can you recommend? Outlook VBA and Custom Forms 2
nathandavies Email Details to Excel & Save as .MSG on one macro - combination of 2 macros Outlook VBA and Custom Forms 3
N Export details to a excel spreadsheet using macros Using Outlook 0
D Outlook macros to create meeting on shared calendar Outlook VBA and Custom Forms 10
N Does a Shared Folder Policy override a Digital Signature Setting for macros? Outlook VBA and Custom Forms 6
A Processing Incoming E-mails with Macros Using Outlook 0
Diane Poremsky Block Macros in Office 2013/2016 Using Outlook 0
Diane Poremsky Using Arrays in Outlook macros Using Outlook 0
Diane Poremsky Running Outlook Macros on a Schedule Using Outlook 0
B Choose commands from Macros is empty Outlook VBA and Custom Forms 3
P Macros in Word 2003 - how to transfer to another Word 2003? Using Outlook 1
N Running multiple macros upon sending Outlook VBA and Custom Forms 6
Britonius Macros for Delegate Issues? Using Outlook 9
S Using Macros in the Outlook Calendar Using Outlook 2
A Outlook 2010 disabled macros Using Outlook 2
R Outlook Macros for Appointments and Tasks Using Outlook 1
M Running macros in tasks sent out as meeting requests in invitees machine Using Outlook 4
L Calendar Macros? Using Outlook 3
S Inserting Dates With Quick Parts (or Macros) Using Outlook 4
L Macros disabled in custom Outlook form Outlook VBA and Custom Forms 1
C Why can't 2003 handle the macros I wrote for 2000 Outlook VBA and Custom Forms 6
M macros of outlook 2007 Outlook VBA and Custom Forms 1
B Macros have been disabled error Message with Custom forms Outlook VBA and Custom Forms 17
C Create macros in Outlook 2007 Outlook VBA and Custom Forms 5
S Need: Date handling in Outlook Macros, either information/documentation Outlook VBA and Custom Forms 1
V Macros suddenly disabled Outlook VBA and Custom Forms 1
N Copying outlook macros between pcs Outlook VBA and Custom Forms 1
D Macros Disabled in Outlook 2007 BCM (Business Contact Manager) 9
C Outlook macros have vanished Outlook VBA and Custom Forms 1
Y How to record macros in Outlook2007? Outlook VBA and Custom Forms 1
L Digital signing macros Outlook VBA and Custom Forms 1
X Run macro automatically when a mail appears in the sent folder Using Outlook 5
J Want to create a button on the nav bar (module add-in) to run code Outlook VBA and Custom Forms 2
J Outlook Rules VBA Run a Script - Multiple Rules Outlook VBA and Custom Forms 0
N Outlook 2021 'Run Script" Rules? Outlook VBA and Custom Forms 4
T Outlook 2010 Errore run-time -2147417851 (80010105) Metodo delete ContactItem non riuscito Outlook VBA and Custom Forms 0
K Run a script rule to auto 'send again' on undeliverable emails? Outlook VBA and Custom Forms 1
G Save attachment run a script rule Outlook VBA and Custom Forms 0
U Outlook 2019 VBA run-time error 424 Outlook VBA and Custom Forms 2
D We're sorry but outlook has run into an error Using Outlook 6
M White square in body of Outlook Messages (O2016 Version 2012 32bit Click To Run) Using Outlook 4
E Having some trouble with a run-a-script rule (moving mail based on file type) Outlook VBA and Custom Forms 5
C Auto Run VBA Code on new email Outlook VBA and Custom Forms 1
Aussie Rules Run a Script on an Incoming Email OK and then the Email reverts Outlook VBA and Custom Forms 0
A Apply Selected Emails to outlook rules and Run Rules Using Outlook 5

Similar threads

Back
Top