How do I eliminate the "Enable Macro" selection?

Status
Not open for further replies.
K

Keith

I know this question hass been asked before but I was unable to make it work.

In order to make up for Outlook's inadequate handling of IMAP, I put

together a simple macro that would move messages to a folder "Trash" when

invoked. (The delete key deletes emails forever - thank you Microsoft!)

All I want to do is to enable the macro without having to manually enable it

each time I start outlook and without setting my security settings to enable

all macros.

I would appreciate any help.

Here is the code:

Sub MoveToTrash()

On Error Resume Next

Dim objFolder As MAPIFolder

Dim objNS As NameSpace, objItem As MailItem

Set objNS = Application.GetNamespace("MAPI")

Set objFolder = objNS.Folders("KJB").Folders("Trash")

If objFolder Is Nothing Then

MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation,

"INVALID FOLDER"

End If

If Application.ActiveExplorer.Selection.Count = 0 Then

Exit Sub

End If

For Each objItem In Application.ActiveExplorer.Selection

If objFolder.DefaultItemType = olMailItem Then

If objItem.Class = olMail Then

objItem.UnRead = False

objItem.Move objFolder

End If

End If

Next

Set objItem = Nothing

Set objFolder = Nothing

Set objNS = Nothing

End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Either use the settings and enable macros, or sign the VBA project; for that

search for 'SelfCert.exe'.

Best regards

Michael Bauer

Am Tue, 15 Sep 2009 23:15:01 -0700 schrieb Keith:


> I know this question hass been asked before but I was unable to make it


work.
> In order to make up for Outlook's inadequate handling of IMAP, I put
> together a simple macro that would move messages to a folder "Trash" when
> invoked. (The delete key deletes emails forever - thank you Microsoft!)

> All I want to do is to enable the macro without having to manually enable


it
> each time I start outlook and without setting my security settings to


enable
> all macros.

> I would appreciate any help.

> Here is the code:

> Sub MoveToTrash()
> On Error Resume Next
> Dim objFolder As MAPIFolder
> Dim objNS As NameSpace, objItem As MailItem

> Set objNS = Application.GetNamespace("MAPI")
> Set objFolder = objNS.Folders("KJB").Folders("Trash")

> If objFolder Is Nothing Then
> MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation,
> "INVALID FOLDER"
> End If

> If Application.ActiveExplorer.Selection.Count = 0 Then
> Exit Sub
> End If

> For Each objItem In Application.ActiveExplorer.Selection
> If objFolder.DefaultItemType = olMailItem Then
> If objItem.Class = olMail Then
> objItem.UnRead = False
> objItem.Move objFolder
> End If
> End If
> Next

> Set objItem = Nothing
> Set objFolder = Nothing
> Set objNS = Nothing
> End Sub
 
K

Keith

Thanks a lot. I didnt find it by that name. Its under MS office and tools

as "Digital Certificate for VBA Projects.lnk". Then i did a Google search

and found this great writeup on how to do it, though its pretty

self-explanatory: http://www.howto-outlook.com/howto/selfcert.htm

Quick follow-up. Is there a way to make the macro run when I hit the delete

key? Right now I have an icon for it on my toolbar but sometimes I hit

delete and then a message is gone forever.

Also, I lifted the code and modified it from a macro someone else wrote so

its probably lousy code on my part. Its quite slow - any ideas on how to

make it faster? I am not a VBA expert - as you can no doubt tell!

Thanks again Michael.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Outlook can't tell you what has been pressed on the keyboard. For that you'd

have to use a lot of Win32 API code. You might rather want to stick with the

toolbar button.

But as far as I know the DEL key doesn't delete messages, it just marks them

as deleted. Just click Edit/Recover (or similar) , and the messages are

back.

Best regards

Michael Bauer

Am Wed, 16 Sep 2009 14:50:02 -0700 schrieb Keith:


> Thanks a lot. I didnt find it by that name. Its under MS office and


tools
> as "Digital Certificate for VBA Projects.lnk". Then i did a Google search
> and found this great writeup on how to do it, though its pretty
> self-explanatory: http://www.howto-outlook.com/howto/selfcert.htm

> Quick follow-up. Is there a way to make the macro run when I hit the


delete
> key? Right now I have an icon for it on my toolbar but sometimes I hit
> delete and then a message is gone forever.

> Also, I lifted the code and modified it from a macro someone else wrote so
> its probably lousy code on my part. Its quite slow - any ideas on how to
> make it faster? I am not a VBA expert - as you can no doubt tell!

> Thanks again Michael.
 
K

Keith

Thanks Michael,

No, the emails are gone. On other platforms like my iPhone then you can

tell it to move deleted items to a selected folder like "Trash" but Outllook

just throws them in the bit-bucket. Its really poor design but I think

microsoft really only care about exchenge support and they do the bare

minimum to support POP and IMAP. There are other posts about how to write

vba to do this.

Incidentally, can you see if its possible to make this code run faster?

don't know enoutgh about VBA to speed it up myself.

Keith
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
#1: I'd say it's no IMAP account then; or you have a filter in place that

hides those messages that are flagged as deleted. A lot of people here ask

for how to delete messages from an IMAP account immediately just by hitting

the DEL key, but Outlook doesn't support that.

#2: no

Best regards

Michael Bauer

Am Thu, 17 Sep 2009 19:54:01 -0700 schrieb Keith:


> Thanks Michael,

> No, the emails are gone. On other platforms like my iPhone then you can
> tell it to move deleted items to a selected folder like "Trash" but


Outllook
> just throws them in the bit-bucket. Its really poor design but I think
> microsoft really only care about exchenge support and they do the bare
> minimum to support POP and IMAP. There are other posts about how to


write
> vba to do this.

> Incidentally, can you see if its possible to make this code run faster?
> don't know enoutgh about VBA to speed it up myself.

> Keith
 
J

JP

You've declared objItem as a MailItem type, but then you check if the

class is olMail, isn't that redundant? Also, regardless of whether the

Trash folder holds non-MailItems, you can skip the DefaultItemType

check. If it does matter, you're already limiting the For loop to mail

items anyway, and if it doesn't matter, the check is pointless.

You might also consider using the Restrict Method on the Items

Collection and returning a filtered subset of items. It can make your

code faster if the folder contains a large number of non-MailItems.

http://msdn.microsoft.com/en-us/library/aa210275(office.11).aspx

--JP

On Sep 17, 10:54 pm, Keith <Ke...> wrote:
> Thanks Michael,

> No, the emails are gone.  On other platforms like my iPhone then you can
> tell it to move deleted items to a selected folder like "Trash" but Outllook
> just throws them in the bit-bucket.  Its really poor design but I think
> microsoft really only care about exchenge support and they do the bare
> minimum to support POP and IMAP.   There are other posts about how to write
> vba to do this.

> Incidentally, can you see if its possible to make this code run faster?  
> don't know enoutgh about VBA to speed it up myself.

> Keith
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
Skipping the DefaultItemType is a good catch; the check is useless not

because of the declaration of the object variable but because it will return

the same value in any loop.

The Restrict method doesn't help as he loops through a collection of

selected items.

Best regards

Michael Bauer

Am Fri, 18 Sep 2009 07:14:22 -0700 (PDT) schrieb JP:


> You've declared objItem as a MailItem type, but then you check if the
> class is olMail, isn't that redundant? Also, regardless of whether the
> Trash folder holds non-MailItems, you can skip the DefaultItemType
> check. If it does matter, you're already limiting the For loop to mail
> items anyway, and if it doesn't matter, the check is pointless.

> You might also consider using the Restrict Method on the Items
> Collection and returning a filtered subset of items. It can make your
> code faster if the folder contains a large number of non-MailItems.

> http://msdn.microsoft.com/en-us/library/aa210275(office.11).aspx

> --JP

> On Sep 17, 10:54 pm, Keith <Ke...> wrote:
> > Thanks Michael,
>

>> No, the emails are gone.  On other platforms like my iPhone then you can
> > tell it to move deleted items to a selected folder like "Trash" but


Outllook
> > just throws them in the bit-bucket.  Its really poor design but I think
> > microsoft really only care about exchenge support and they do the bare
> > minimum to support POP and IMAP.   There are other posts about how to


write
> > vba to do this.
>

>> Incidentally, can you see if its possible to make this code run faster?  
> > don't know enoutgh about VBA to speed it up myself.
>

>> Keith
 
K

Keith

Yep it's definitely IMAP and the messages are definitely deleted from the

server. The account shows as "IMAP/SMTP" in the account settings. Deleted

emails from Outlook disappear from my view on the iPhone too. If I delete

them from Outlook they are gone forever. If I delete them from my iPhone

they go to Trash. I understand though that the function of putting deleted

items in a designated folder such as Trash is not part of the IMAP spec but

that most IMAP platforms (except Outlook) support it. Outlook does however

let me designate a sent items folder.

I used to use POP but when I got my iPhone, I switched to IMAP to access my

emails because I wanted to synchronize emails between the two platforms.

My email account is with GoDaddy so I use: IMAP.secureserver.net and

SMTPOUT.secureserver.net. Godaddy uses "Trash" for deleted items.

Ive asked about IMAP support in Outlook General section and the result seems

to be to write a VBA macro because this delete -move to trash function is not

supported.

Thanks so much for your help.

Keith
 
K

Keith

Thanks you so much Michael and JP

I made the suggested change (At least I think I did " it still works anyway!)

Your input lead me to ask some questions which I have added as comments. I

really really appreciate your help.

So here is the new code:

Sub MoveToTrash()

On Error Resume Next

Dim objFolder As MAPIFolder

Dim objNS As NameSpace

Dim objItem As MailItem

Set objNS = Application.GetNamespace("MAPI")

Set objFolder = objNS.Folders("KJB").Folders("Trash")

' is this redundant since the folder name is hard coded and doesnt change??

If objFolder Is Nothing Then

MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation,

"INVALID FOLDER"

End If

' Is this test needed since it appears the FOR loop below takes care of

this test

If Application.ActiveExplorer.Selection.Count = 0 Then

Exit Sub

End If

For Each objItem In Application.ActiveExplorer.Selection

objItem.UnRead = False

objItem.Move objFolder

Next

' Do these items need to be set to nothing. Wont they be removed from the

heap or stack when the sub() ends?

Set objItem = Nothing

Set objFolder = Nothing

Set objNS = Nothing

End Sub
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
#1: I'd keep that in place. In a few months you don't remember all the

details and might decide to rename the folder. Then the prompt will remind

you to update the code. Skipping that one wouldn't save you more than a

millisecond, if any.

#2: It's not necessary in your code, but again the saved time is not worth

worrying about.

#3: VB(A) automatically sets the variables to Nothing as soon as you leave

the function.

So, actually you can delete all the mentioned lines of code. But time could

be saved only within the loop. And there's nothing you can do except maybe

to use a variable set to the Selection object:

Dim Sel as Outlook.Selection

Set Sel=Application.ActiveExplorer.Selection

For Each objItem in Sel

> ...

Next

Best regards

Michael Bauer

Am Fri, 18 Sep 2009 19:19:01 -0700 schrieb Keith:


> Thanks you so much Michael and JP

> I made the suggested change (At least I think I did " it still works


anyway!)

> Your input lead me to ask some questions which I have added as comments.


I
> really really appreciate your help.

> So here is the new code:

> Sub MoveToTrash()
> On Error Resume Next
> Dim objFolder As MAPIFolder
> Dim objNS As NameSpace
> Dim objItem As MailItem

> Set objNS = Application.GetNamespace("MAPI")
> Set objFolder = objNS.Folders("KJB").Folders("Trash")

> ' is this redundant since the folder name is hard coded and doesnt


change??
> If objFolder Is Nothing Then
> MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation,
> "INVALID FOLDER"
> End If

> ' Is this test needed since it appears the FOR loop below takes care of
> this test
> If Application.ActiveExplorer.Selection.Count = 0 Then
> Exit Sub
> End If

> For Each objItem In Application.ActiveExplorer.Selection
> objItem.UnRead = False
> objItem.Move objFolder
> Next

> ' Do these items need to be set to nothing. Wont they be removed from


the
> heap or stack when the sub() ends?
> Set objItem = Nothing
> Set objFolder = Nothing
> Set objNS = Nothing
> End Sub
 
K

Keith

I made all the changes Michel, thanks a lot.

Actually I think the slow speed is in part because of the way I invoke the

macro.

I select on item then invoke the macro by pressing my macro button, then I

repeat for the next item, etc.

A better method would be to select multiple items first and then invoke the

macro once to move the multiple selected items at once.

For completeness, here is my final code:

Sub MoveToTrash()

On Error Resume Next

Dim objNS As NameSpace

Dim objFolder As MAPIFolder

Dim objItem As MailItem

Dim Sel As Outlook.Selection

' "Trash" folder in user namespace

Set objNS = Application.GetNamespace("MAPI")

Set objFolder = objNS.Folders("KJB").Folders("Trash")

' Warn if "Trash" folder doesnt exist

If objFolder Is Nothing Then

MsgBox "This folder doesn't exist!", vbOKOnly + vbExclamation,

"INVALID FOLDER"

End If

' User selected emails to move to trash

Set Sel = Application.ActiveExplorer.Selection

' For each mail item in user selection, move it to "Trash" folder

For Each objItem In Sel

objItem.UnRead = False

objItem.Move objFolder

Next

End Sub
 
K

Keith

Can this code be compiled to make it run faster or is the VBA code interpreted?

I guess the speed things is just my observation. If I hit the delete key,

its very fast, if I hit my macro button it might take a second to run and

complete the move so it seems slow.
 

Michael Bauer

Senior Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server
I think moving an IMAP message is what takes most of the time. There's

nothing you can do to speed it up.

Best regards

Michael Bauer

Am Sun, 20 Sep 2009 11:10:01 -0700 schrieb Keith:


> Can this code be compiled to make it run faster or is the VBA code


interpreted?

> I guess the speed things is just my observation. If I hit the delete key,
> its very fast, if I hit my macro button it might take a second to run and
> complete the move so it seems slow.
 
J

JP

Don't worry, it happens to all of us :) The code I've written to move

or delete multiple emails always runs slower than the Del key.

--JP

On Sep 20, 2:10 pm, Keith <Ke...> wrote:
> Can this code be compiled to make it run faster or is the VBA code interpreted?

> I guess the speed things is just my observation.  If I hit the delete key,
> its very fast, if I hit my macro button it might take a second to run and
> complete the move so it seems slow.
 
K

Ken Slovak - [MVP - Outlook]

VBA code will always be slower than Extended MAPI code of course.

"JP" <jp2112@earthlink.net> wrote in message

news:7671b282-2079-449a-a1fb-cb5137fd0bd9@g23g2000vbr.googlegroups.com...

Don't worry, it happens to all of us :) The code I've written to move

or delete multiple emails always runs slower than the Del key.

--JP
 
K

Keith

Well that's reassuring JP - I was about to talk to my therapist about why

Outlook is making me feel a victim. Guess I can cancel that appointment now

:)
 
Status
Not open for further replies.
Thread starter Similar threads Forum Replies Date
C Eliminate need for "Enable Macro" selection Outlook VBA and Custom Forms 7
N Eliminate the +1 before all numbers Using Outlook 1
M Eliminate bug that closes my Outlook Using Outlook 3
M Eliminate trail verson of MSOutlook and go back to my original Outlook Express. Using Outlook 2
S How do I eliminate multiple in boxes? Using Outlook 3
R how do I eliminate allow access dialog box using VBAproject in Out Outlook VBA and Custom Forms 1
Mark Foley Cannot enable add-in in outlook 2010 Using Outlook 0
Peter H Williams Enable script containing VBA Outlook VBA and Custom Forms 12
A How to enable outlook room finder (outlook connector (mapi)) user ? Using Outlook 3
Diane Poremsky Mail-enable Exchange Public Folders New Slipstick.com Articles 0
soadfan Enable all rules from toolbar button? Outlook VBA and Custom Forms 3
R Enable Office 365 Exchange Online Archive New Slipstick.com Articles 0
R Enable Office 365 Exchange Online Archive Using Outlook 6
G VBA code to enable a rule based on time of day for a IMAP mail account Outlook VBA and Custom Forms 14
K Macro won't consistently re-enable Outlook VBA and Custom Forms 2
Sarge USMC how do I enable hyperlinks in Outlook 2013 Using Outlook 1
A how to install or enable categories in OL 2013 for calendar and contacts Using Outlook 5
D How do I mail-enable a Public Folder Sub-folder in exchnage 2007 using shell ? Exchange Server Administration 0
H enable textboxes and buttons from received email Using Outlook 1
D How To Enable Undo in Outlook 2007 Tasks Using Outlook 2
S Outlook 2010: How do you enable Contacts' Activities to search archive folders Using Outlook 6
J OL2003 Macro disables itself - looking for way to automate 'enable' Using Outlook 2
C Enable or install Digital ID for Exchange Users on SBS 2008 Exchange Server Administration 2
M SRV records to enable auto discover for Outlook Anywhere Using Outlook 8
S vb.net exchange 2010 problem with enable-mailbox command Exchange Server Administration 2
P Exchange 2010 - Enable Spam filtering on Hub Transport Server Exchange Server Administration 3
Z Patch / hotfix enable Outlook 2007 to work with Exchange 2010 Archiving Exchange Server Administration 2
I Outlook 2007 Macro/VBE disables How to enable Using Outlook 5
C Enable EmailAddressPolicy via Exchange Shell Exchange Server Administration 2
C How to allow admins to enable ActiveSync and edit address book information in EMC Exchange Server Administration 3
W Outlook was in use; when tried to open today it would not open; pop up with info about e-mails with macros; prompted to enable macros, when doing so; Using Outlook 3
J cannot enable embedded links within received e mails, message sysy this function is disabled please contact your system administrator Using Outlook 1
G How to enable Outlook Anywhere for domain joined clients, enable Exchange ActiveSync, but disable OA Exchange Server Administration 1
M OWA (2003,2007,2010) How to enable or Disable efficiently in Bulk range Like Database/Storage Group/Server Exchange Server Administration 3
Z How to enable Content Filtering for Inbound and Outbound Emails on Exchange 2007? Exchange Server Administration 4
D OAB Offline Address Book generation server and enable web distribution on the Exchange 2010 Client Access server Exchange Server Administration 6
R How do I re-enable the confirmation dialog? Using Outlook 4
M Enable to access outlook 2003 even in safe mode and is giving an error message that Business Contact Manager has encountered a problem Using Outlook 2
J Re-enable the junk mail notification. Using Outlook 6
M Outlook Anywhere - Enable both NTLM and Basic Authentication Exchange Server Administration 8
C How do I enable a link in an e-mail in Outlook 2007? Using Outlook 4
A how to enable macro for addin Outlook VBA and Custom Forms 3
J Enable Sonicwall Outlook VBA and Custom Forms 1
S How do I enable administrator access in Business Contact Manager? BCM (Business Contact Manager) 1
S How to enable scripts to run in a custom form which is forwarded ? Outlook VBA and Custom Forms 1
J Office Ultimate- My outlook doesn't have BCM, how do I enable? BCM (Business Contact Manager) 1
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
Similar threads


















































Top