|
|
Subject:
Simple Visual Basic Macro for Outlook
Category: Computers > Programming Asked by: marcfestnws-ga List Price: $20.00 |
Posted:
03 Jun 2005 11:23 PDT
Expires: 03 Jul 2005 11:23 PDT Question ID: 529007 |
I want to create a keyboard-shortcut-triggered macro for Outlook that does the following: Move one or several highlighted messages in my inbox to a folder called "archived inbox" when pressing CTRL-Q. Thank you. Marc |
|
There is no answer at this time. |
|
Subject:
Re: Simple Visual Basic Macro for Outlook
From: manuka-ga on 07 Jun 2005 01:54 PDT |
Hi marcfestnws, Here's something that'll get you pretty close. Can you live with Alt-M rather than Ctrl-Q? In Outlook, hit Alt-F11 to open the VBA editor. Create a new module and drop this code in: Public Sub MoveToArchive() Dim Inbox As MAPIFolder, Archive As MAPIFolder, Msg As MailItem Set Inbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) Set Archive = Inbox.Folders("Archived Inbox") For Each Msg In ActiveExplorer.Selection Msg.Move Archive Next Msg End Sub You'll need to modify the line beginning "Set Archive = " to conform to where you've put the Archived Inbox folder. In this example it's a folder in the Inbox. If it's a top-level folder, use Set Archive = Application.GetNamespace("MAPI").Folders("Archived Inbox") and if it's several levels down, use (for example) Set Archive = Inbox.Folders("Old Stuff").Folders("Mail").Folders("Archived Inbox") You can step through it in the debugger to make sure it's working correctly. Now you need to put it on your toolbar. Save your VBA project and close the VBA editor. In Outlook, select Tools and then Customize. Select the Commands tab in the dialog box and select "Macros" in the Categories box. You should see something like "Project1.MoveToArchive" in the Commands box. Drag it somewhere on your menu bar with all the other buttons. You'll get a horrible button displaying the default icon and the full name including the project name. You can use the "Modify Selection" button to fix the name and to change the icon (or set it to display as text only if you don't want any icon; but you can't set it to default because we need the text). Note that you can modify the details at any future time by opening the Customize dialog, selecting "Macros" in Categories and the macro item in Commands, and then (without closing the dialog box) clicking on the button in the toolbar to which you have assigned the macro. Now here comes the tricky part. Since Outlook, unlike basically every other Office application, doesn't let you assign your own keyboard shortcuts, all we can do is assign a menu accelerator by adding a & in the name. I've tested it using the name of "&Move To Archive" (the accelerator key is whatever follows the & symbol, so in this case the shortcut is Alt-M; make sure you don't use one that conflicts with one of the top-level menus). This is all under the Modify Selection popup menu. Information on this method of getting keyboard shortcuts was found by a Google search on 'assign "keyboard shortcuts" Outlook'. The rest was done by experimentation. Let us know how it goes! |
Subject:
Re: Simple Visual Basic Macro for Outlook
From: marcfestnws-ga on 07 Jun 2005 07:26 PDT |
Hello manuka - Thank you for trying to help. When I try to debug the code I get the following message: "The macros in this project are disabled. Please refer to thge online help or documentation of the host application to determine how to enable macros." How do I enable the macros? Also, the "archived inbox" folder seems to be inside a folder called "Mailbox - Marc Fest", so I used this line: Set Archive = Inbox.Folders("Mailbox - Marc Fest").Folders("archived inbox") Thanks again for helping with this. Marc. |
Subject:
Re: Simple Visual Basic Macro for Outlook
From: manuka-ga on 08 Jun 2005 02:11 PDT |
Damn! Stupid mental note filing system - I meant to tell you about the security bit but obviously forgot. You'll need to change your security settings in Outlook: go to Tools > Macro > Security and change the setting to Medium. (Then close Outlook down and restart it.) Then the first time in each session you run the macro you'll get a dialog box asking you whether to allow it to run, whereas on the defult setting it's disabled. (Don't change the security setting to Low as this wil allow any code to run without informing you and make you susceptible to spreading viruses.) On the folder location, what you have is correct if the "Mailbox - Marc Fest" folder is a folder inside your main Inbox folder. Is that the case? Cheers, manuka-ga |
Subject:
Re: Simple Visual Basic Macro for Outlook
From: estrangler-ga on 08 Jun 2005 20:52 PDT |
You may need a certificate if running XP w/SP2. To create one; From the Start Menu, in [Microsoft Office / Microsoft Office Tools] click [Digital Certificate for VBA Projects]. Create a dummy certificate for your own use and save it. From within the [Tools] menu inside of VBA, select [Digital Signiture] and reference the certificate you made. Save your project and reload the application, Outlook in this case. |
Subject:
Re: Simple Visual Basic Macro for Outlook
From: marcfestnws-ga on 09 Jun 2005 14:23 PDT |
Creating the certificate solved the problems with the disabled macro. Now there's just a VBA run-time error left. You can see the error message, the offending line in the script, and my Outlook InBox structure at www.2q.com/ga . Can you tell what's wrong? Thank you! Best, Marc |
Subject:
Re: Simple Visual Basic Macro for Outlook
From: marcfestnws-ga on 10 Jun 2005 12:28 PDT |
Manuka - here's the code that worked out for me eventually: Sub MoveToArchive() Dim myFolders As Outlook.Folders Dim Inbox As MAPIFolder, Archive As MAPIFolder, Msg As MailItem Set myOutlook = Outlook.Application Set myNameSpace = myOutlook.GetNamespace("MAPI") Set myFolders = myNameSpace.Folders Set myNameSpace = myOutlook.GetNamespace("MAPI") Set Inbox = Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) Set Archive = myFolders("Mailbox - Marc Fest").Folders("archived inbox") For Each Msg In ActiveExplorer.Selection Msg.Move Archive Next Msg End Sub |
Subject:
Re: Simple Visual Basic Macro for Outlook
From: markus4star-ga on 14 Jul 2005 14:25 PDT |
Thanks to all the comments above! I've got this macro working now and it's a timesaver. Thanks again! |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |