Having some trouble with a run-a-script rule (moving mail based on file type)

Post number 3 has been selected as the best answer.

EricSigh

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Hi, sorry if I'm in the wrong place for this, but I've been trying to implement the advice in this article:


Here is the slightly altered code I'm using:

Code:
Sub MoveMail(Item As Outlook.MailItem)


If Item.Attachments.Count > 0 Then


Dim attCount As Long
Dim strFile As String
Dim sFileType As String


attCount = Item.Attachments.Count


For i = attCount To 1 Step -1
      strFile = Item.Attachments.Item(i).FileName
      
      sFileType = LCase$(Right$(strFile, 4))
    
    Select Case sFileType
        Case ".xml"
    ' do something if the file types are found 
    ' this code moves the message
      Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Test"))
    
   ' stop checking if a match is found and exit sub
       GoTo endsub
      End Select
  Next i


End If
 
endsub:


  Set Item = Nothing
 
End Sub



I know the author recommends using a stub-macro in order to get the rule to run, I'm just not quite sure where to implement that. Any help is appreciated!
 

EricSigh

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Sorry I didn't specify, but when I run the rule it just seems like nothing happens.
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Add this macro to the same module as the script then select a message the rule should apply to and run this macro to see if it works.

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
MoveMail objItem

End Sub

make sure the folder you are moving the message to exists - Is there a subfolder called test in the inbox?
 

EricSigh

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Add this macro to the same module as the script then select a message the rule should apply to and run this macro to see if it works.

Code:
Sub RunScript()
Dim objApp As Outlook.Application
Dim objItem As Object ' MailItem
Set objApp = Application
Set objItem = objApp.ActiveExplorer.Selection.Item(1)

'macro name you want to run goes here
MoveMail objItem

End Sub

make sure the folder you are moving the message to exists - Is there a subfolder called test in the inbox?
Thank you! This immediately fixed it haha, sorry I'm not super familiar with VB. Yes, I created a test folder on my computer to try a few things, it will be changed when I implement this on a user's computer. Follow up question, will this also work for a subfolder within a subfolder? And if so what would the syntax be?

Say I have a test 2 folder within my test folder, I've tried

Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Test 2"))

and

Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Test").Folders("Test 2"))


I'll be looking for more info on that on slipstick, but thank you for your help so far!
 

EricSigh

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
Thank you! This immediately fixed it haha, sorry I'm not super familiar with VB. Yes, I created a test folder on my computer to try a few things, it will be changed when I implement this on a user's computer. Follow up question, will this also work for a subfolder within a subfolder? And if so what would the syntax be?

Say I have a test 2 folder within my test folder, I've tried

Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Test 2"))

and

Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Test").Folders("Test 2"))


I'll be looking for more info on that on slipstick, but thank you for your help so far!

Nevermind, I was able to figure it out. Thank you so much for your help!
 

Diane Poremsky

Senior Member
Outlook version
Outlook 2016 32 bit
Email Account
Office 365 Exchange
Item.Move (Session.GetDefaultFolder(olFolderInbox).Folders("Test").Folders("Test 2"))
This is the one (but it has an extra ')' ) if the folders are
Inbox'
-- Test
---- Test 2

the first would be used if there were two folders at the same level under the inbox.
Inbox
-- Test
-- Test 2
 
Similar threads
Thread starter Title Forum Replies Date
B Trying to get old emails from a not working computer but having trouble. Using Outlook 6
M having trouble importing excel data into outlook contacts BCM (Business Contact Manager) 1
N Having Shared Calendar shift to Current day Using Outlook 0
dbjdbj Is it only me having a fun time with Outlook.com "beta" ? Using Outlook 3
O Importing Mbox - anyone out there having experience with this tool... Using Outlook 2
A Outlook 2019 having issues with 'people search' and not making suggestions in email To: etc Using Outlook 12
M I'm Having problems Moving Contacts to a New List Using Outlook 8
O Having rules run on old mails noved to inbox Outlook VBA and Custom Forms 8
V Problem not having Contacts folder after importing emails from Yahoo. Using Outlook 0
J outlook having sort by date or from unable to display sender name. Using Outlook 2
M Having Calander send E-mails Using Outlook 3
B Having Recent Issues with Hotmail Connector? Using Outlook.com accounts in Outlook 17
O Exchange 2003 displaying HTML emails as having attachments Exchange Server Administration 7
T Get rid of having to type verification characters to send mail BCM (Business Contact Manager) 2
A Contact Manager Unable to Initialize...after having tested Office BCM (Business Contact Manager) 1
G Trouble with Office 365/2016 Using Outlook 3
M Trouble using keyboard shortcut for color categories in Outlook 2016 Using Outlook 3
M Trouble downloading Outlook on an Android phone Using Outlook 4
B Trouble Starting Outlook 2010 after Crash Using Outlook 3
U Trouble migrating Contacts (2002) to People (2013) Using Outlook 3
K New to BCM - trouble importing contacts & accounts BCM (Business Contact Manager) 3
makinmyway Trouble Installing BCM Outlook 2013; Endless Install Loop Happens Using Outlook 0
M Trouble Importing folders from Outlook 2007 to Outlook 2013 Using Outlook 12
S trouble with Outlook 2010 saving sent emails Using Outlook 2
B Trouble Locating Tools Menu since There's No Standard Toolbar in Office 2010 Using Outlook 1
T Outlook 2007 - trouble with alphabetizing when titles are used (ie - Mr.) Using Outlook 0
M Outlook 2010 stuck in Work Offline mode; trouble sending email Using Outlook 1
A Trouble importing data to radio buttons Business Contact Manager 2010 (BCM) Using Outlook 2
B Trouble Adding @Outlook.com Email Accounts Using Outlook 3
A emails disappearing from inbox outlook 2010 exchange server 2010 major trouble Exchange Server Administration 6
K Trouble converting the ANSI PST to a Unicode PST Using Outlook 4
V Trouble Running Outlook 2007 and 2010 Using Outlook 4
U Trouble for sharing Calendar Using Outlook 3
S Trouble setting up Outlook Connector to sync with windows live Using Outlook.com accounts in Outlook 3
T Trouble with creating database BCM (Business Contact Manager) 1
K Trouble using SaveAs Outlook VBA and Custom Forms 3
M trouble connection to remote database BCM (Business Contact Manager) 1
D Outlook 2013 Macros only run in VB editor, not in drop down or button Outlook VBA and Custom Forms 10
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
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

Similar threads

Top