Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-15-2024, 07:21 AM
Trio78 Trio78 is offline Outlook VBA macro runs from editor, but not from Outlook ribbon? Windows 11 Outlook VBA macro runs from editor, but not from Outlook ribbon? Office 2021
Novice
Outlook VBA macro runs from editor, but not from Outlook ribbon?
 
Join Date: Mar 2024
Posts: 3
Trio78 is on a distinguished road
Default Outlook VBA macro runs from editor, but not from Outlook ribbon?

I have a macro I made to create a new directory and move the selected message into it. It works fine if I run it from the VBA editor, but when I try to run it from Outlook using a button on the ribbon, nothing happens.



I have Macro Security set to "Enable all macros", at least for testing.

Code:
Sub MakeNewPIFolderAndMoveEmail()
    Dim objOutlook As Outlook.Application
    Dim myItems As Outlook.Items
    Dim fldRoot As Outlook.MAPIFolder
    Dim fldPIs As Outlook.MAPIFolder, fldNew As Outlook.MAPIFolder
    Dim strFolderName As String
    Dim Explorer
    Dim CurrentItem
        
    Set objOutlook = CreateObject("outlook.application")

    ' from the default inbox to the parent which is your mailbox
    Set fldRoot = objOutlook.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent
    ' from the mailbox to a folder at the same level as the Inbox
    Set fldPIs = fldRoot.Folders("PIs")

    strFolderName = InputBox("Enter name for the PI folder", "Folder Name")
    
    If Len(strFolderName) = 0 Then
      Exit Sub
    End If
    
    Set fldNew = fldPIs.Folders.Add(strFolderName)
    Set Explorer = Application.ActiveExplorer
 
    ' Check whether any item is selected in the current folder.
    If Explorer.Selection.Count Then
 
      ' Get the first selected item.
      Set CurrentItem = Explorer.Selection(1)
      If CurrentItem.Class = olMail Then
        CurrentItem.Move fldNew
      End If
    End If
End Sub
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Outlook VBA macro runs from editor, but not from Outlook ribbon? vba Option remove Editor from Ribbon jec1 Word 10 12-08-2021 11:19 PM
Add a slide library to a custom ribbon with RibbonX Editor tompen78 PowerPoint 0 07-07-2021 12:19 AM
Outlook VBA macro runs from editor, but not from Outlook ribbon? Size of Outlook Ribbon Changes When Ribbon Is Pinned man_utd_fan Outlook 5 03-24-2021 03:08 AM
pros & cons of Word as editor in Outlook 2003 bolandg Outlook 0 11-21-2009 08:02 PM
Outlook 2002 locks up with Word as Editor when replying to s Larry S Outlook 6 12-12-2005 07:18 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 03:31 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft