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

Post number 6 has been selected as the best answer.

deuxn

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!
 

deuxn

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
Yes, it's already set to Enable all macros. Thanks.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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.
 

deuxn

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
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.
 

deuxn

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
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!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Ah... I've done that before - mostly when testing macros for others - maybe I never tried running them from outlook.
 

deuxn

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
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.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
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
 

deuxn

Member
Outlook version
Outlook 2013 64 bit
Email Account
Office 365 Exchange
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!
 
Similar threads
Thread starter Title Forum Replies Date
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
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 New Slipstick.com Articles 0
Diane Poremsky Block Macros in Office 2013/2016 New Slipstick.com Articles 0
Diane Poremsky Using Arrays in Outlook macros New Slipstick.com Articles 0
Diane Poremsky Running Outlook Macros on a Schedule New Slipstick.com Articles 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
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
B VBScript doesn't run on Recipient Email Outlook VBA and Custom Forms 2
A Run-time error '430' on certain emails when trying to set "Outlook.mailitem" as "ActiveExplorer.Selection.Item" Outlook VBA and Custom Forms 2
S Outlook Custom Form Scripting only working when clicking on "Run this form" Outlook VBA and Custom Forms 2
Y Outlook 2013 Run A Script Outlook VBA and Custom Forms 4
O Outlook 2016 This rule will only run when you check your email in Outlook.... Using Outlook 4
B run scripts Using Outlook 1
A Run time error 424. object required in outlook 2013 Outlook VBA and Custom Forms 10
Dave A Run macro on existing appointment when it changes Outlook VBA and Custom Forms 1
O Run macro automatically at sending an email Using Outlook 11
P errors appear every time I run SCANPST Using Outlook 3
B Outlook rule run a Script doesn't work Outlook VBA and Custom Forms 1

Similar threads

Top