Outlook VBA - MoveTo method does not accept my Folder variable as an object - vba

All,
I wrote some VBA code to move the currently selected email folder in an Outlook session to a folder called "archive 2023". It did not work; I spent ages working out why but could not fix the code. Eventually, I settled it by researching and then coming at the problem from a different angle. Although happy, and learning a lot, I still don't know why my original code did not work. And that bugs me (if you excuse the pun).
I'll start with my original code that did not work - I've gone heavy on the comments.
Sub archive_a_folder()
'firsty create the variable I'll store the current folder in as an object
Dim current_folder As Outlook.Folder
'then put the folder, selected in the active instance of Outlook, into the variable
Set current_folder = Application.ActiveExplorer.CurrentFolder
Debug.Print current_folder.Name 'I put this in to check the above worked - and it did!
'I wrote a little code to find out the EntryID property of the folder called "archive 2023"
'I then put the EntryID (which is a string) into a variable
Dim archiveID As String
archiveID = "xxxx" 'instead of xxxx this is a really long string
'I then create a MAPI namespace so I can use the GetFolderFromID method
Dim ns As Outlook.NameSpace
Set ns = Application.GetNamespace("MAPI")
'I then create an Outlook.Folder variable and put the "archive 2023" folder in there by ...
'... using the GetFolderFromID method using the EntryID
Dim archive_folder As Outlook.Folder
Set archive_folder = ns.GetFolderFromID(archiveID)
Debug.Print archive_folder.Name 'Did this to check the above works and it does!
'So at this point I thought I had two correctly assigned Outlook.Folder object variables ...
'... One assigned with the folder that needs moving and one being the destination folder
'The documentation states the MoveTo method should be used like this...
'... Folder.MoveTo(Folder) with the first Folder (an object) being moved to the second.
current_folder.MoveTo(archive_folder)
'I get an object expected error.
End Sub
Running the code line by line proved everything was working right up to current_folder.MoveTo(archive_folder).
The debugging print outs show that the variables current_folder and archive_folder are correctly assigned. I even printed out the variables' types to ensure they were both of the Folder type and they were (they were actually type FolderMAPI but I think thats OK).
I tried creating a new Folder.Outlook variable and having the below statement:
set new_folder = current_folder.MoveTo(archive_folder)
or
new_folder = current_folder.MoveTo(archive_folder)
but niether worked. (I saw that the MoveTo method returned a Folder so that's why I tried that.
Eventually, after research, I re-wrote is like this and it worked.
Sub archive_folder()
'get the current folder and put it in a Folder variable
Dim current_folder As Outlook.Folder
Set current_folder = Application.ActiveExplorer.CurrentFolder
'get a namespace variable so I can use some of its methods later
Dim ns As Outlook.NameSpace
Set ns = Application.GetNamespace("MAPI")
'create inbox as a Folder variable
Dim inbox As Outlook.Folder
'using a namespace method assign the actual in-box to the inbox variable
'olFolderInbox is an inbuilt referene to the default in box folder
Set inbox = ns.GetDefaultFolder(olFolderInbox)
'create a Foler variable that will be assigned the destination folder
Dim archive_folder As Outlook.Folder
'this seems oddly cumbersome but works!
'take parent of the inbox Folder and look for "archive 2003" beneath it
'assign this to the archive folder variable.
Set archive_folder = inbox.Parent.Folders("archive 2023")
'The using the MoveTo method move the current_folder to the
'archive folder
current_folder.MoveTo archive_folder
'when I check in my Outlook window, its moved!
Exit Sub
End Sub
If I had to guess at what the problem is, its something to do with GetFolderFromID not returning a Folder object with all the properties needed for the MoveTo method to work.
I'm probably thinking too 'real world' mistakenly imagining folders actually being stored in other folders. The system probably just looks as the Parent and Folders properties of all the folders and builds a tree for the GUI. Mayube GetFolderFromID does not return these property values correctly so MoveTo does not think its an object at all this the error. This would seem likely if MoveTo just messed about with some of the parent / folders properties.
If this is the case though, what would be the point of the GetFolderFromID function?
Or maybe I'm being punished for trying to skip learning the basics of a language.
Any help?
Aldus
Edit:
I can't believe I did not clock that I should not have used parentheses for the MoveTo method. ##DmitryStreblechenko saw me right in the comments.
To make me feel better I massively reduced the size of the code...
Sub archive_a_folder()
archiveID = "xxx" `xxx is the EntrhyID of the destination folder
Application.ActiveExplorer.CurrentFolder.MoveTo Application.GetNamespace("MAPI").GetFolderFromID(archiveID)
End Sub
:-)

I want to show this question as answered but want to make clear it was answered in the comments and not by me. In essence, and I can't believe I made this mistake, I used parenthesis when I should not have.
Where I used current_folder.MoveTo(archive_folder) I should have used current_folder.MoveTo archive_folder
I fell for the old trick of assuming my syntax was correct but there was some deeper problem. But no, I just used a couple of brackets!
I used to code many years ago so have enough of an understanding to risk skipping some of the basics when learning a new language but the danger of that is what led to this question.
Oh well, you live and learn.

Related

How can I make my VBA Outlook script more efficient

This is my first question on StackExchange ever :-)
I am Running the following script in MS Outlook VBA
Sub export()
On Error resume Next
Dim Ns As Outlook.NameSpace
Dim eitem
Dim oFile As Object
Dim fso As Object
Set Ns = Application.GetNamespace("MAPI")
Set fso = CreateObject("Scripting.FileSystemObject")
Set oFile = fso.CreateTextFile("C:\Users\chakkalakka\Desktop\mails.txt")
'Code
For Each eitem In Ns.Session.Folders.Item(12).Items
oFile.WriteLine eitem.SenderName & "§" & eitem.SentOnBehalfOfName & "§" & eitem.ReceivedTime
Next
oFile.Close
Set Ns = Nothing
Set fso = Nothing
Set oFile = Nothing
Debug.Print "Completed!"
End Sub
The script in general is working fine and the output is correct. My Problem is: I need to run this inside a folder with > 95000 items and it takes ages.
So my question is: What can I do to improve performance?
Thanks in advance for your help
The most inefficient line of code is the following one:
For Each eitem In Ns.Session.Folders.Item(12).Items
You need to break the chain of property and method calls and declare them on separate lines. So each property or method will be declared on a separate line of code. Thus, you will be able to release underlying COM objects instantly. Set a variable to Nothing in Visual Basic to release the reference to the object.
Iterating through all items in the folder is a time-consuming task. Instead, I'd suggest using the Find/FindNext or Restrict methods of the Items class to deal with items that correspond to your conditions. Read more about these methods in the following articles:
How To: Use Restrict method to retrieve Outlook mail items from a folder
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
Also you may consider using the GetTable method of the Folder class which allows to obtain a Table object that contains items filtered by Filter. If Filter is a blank string or the Filter parameter is omitted, GetTable returns a Table with rows representing all the items in the Folder.

Outlook 2010 scripted rule using VBA

I am trying to create a very simple (because I'm new and learning) scripted rule in Outlook 2010.
The RULE is: If a new mail item comes in from a particular email address, run my script and stop processing rules. The SCRIPT checks the body for a string. If the string is found, it moves the email to destination folder 1, otherwise it moves it to destination folder 2.
Sadly, I can't seem to get the script (code below) to do anything (mail just goes to inbox rather than either folder specified in script). A lot of this was pieced together from online examples, so I don't understand it all, but I figure I'd ask this now while I research the stuff I don't get. Any ideas on how to get this to work as intended?
'Use the MailItem class of item
Public Sub NCRFRule(Item As Outlook.MailItem)
Dim MAPI As NameSpace 'Don't know what this does
Dim dest1, dest2 As Folder 'declare destination folders
Dim newMail As MailItem 'set item type
'Don't know what this does.
Set MAPI = GetNamespace("MAPI")
'Set the destination folders
Set dest1 = MAPI.Folders("Inbox").Folders("NCRFs")
Set dest2 = MAPI.Folders("Inbox").Folders("other's NCRFs")
'Rule if-statement. If text is found, move mail to dest1 folder
If InStr(1, newMail.Body, "Your Required Action") <> 0 Then
newMail.Move dest1
GoTo cutOut:
End If
'If the above If-statement doesn't execute, text wasn't found,
'move mail to other destination folder.
newMail.Move dest2
cutOut:
End Sub
Note: this code is in the "ThisOutlookSession" module.
MAPI.Folders("Inbox")
There is no such folder. Use the GetDefaultFolder method of the Namespace or Store class instead.
Also you may find the Getting Started with VBA in Outlook 2010 article helpful.
Building on What Eugene explained, changing
Set dest1 = MAPI.Folders("Inbox").Folders("NCRFs")
Set dest2 = MAPI.Folders("Inbox").Folders("other's NCRFs")
to
Set dest1 = MAPI.GetDefaultFolder(olFolderInbox).Folders("NCRFs")
Set dest2 = MAPI.GetDefaultFolder(olFolderInbox).Folders("other's NCRFs")
got that part to work. Then I had to remove the line
Dim newMail As MailItem 'set item type
and replace all instances of "newMail" with "Item". Now it works!

How do I make Outlook purge a folder automatically when anything arrives in it?

I hope it's okay to ask this kind of question. Attempting to write the code myself is completely beyond me at the moment.
I need a macro for Outlook 2007 that will permanently delete all content of the Sent Items folder whenever anything arrives in it. Is it possible? How do I set everything up so that the user doesn't ever have to click anything to run it?
I know I'm asking for a fish, and I'm embarrassed, but I really need the thing...
edit:
I've pasted this into the VBA editor, into a new module:
Public Sub EmptySentEmailFolder()
Dim outApp As Outlook.Application
Dim sentFolder As Outlook.MAPIFolder
Dim item As Object
Dim entryID As String
Set outApp = CreateObject("outlook.application")
Set sentFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)
For i = sentFolder.Items.Count To 1 Step -1
sentFolder.Items(i).Delete '' Delete from mail folder
Next
Set item = Nothing
Set sentFolder = Nothing
Set outApp = Nothing
End Sub
It's just a slightly modified version of a piece of code I found somewhere on this site deleting Deleted Items. It does delete the Sent Items folder when I run it. Could you please help me modify it in such a way that it deletes Sent Items whenever anything appears in the folder, and in such a way that the user doesn't have to click anything to run it? I need it to be a completely automated process.
edit 2: Please if you think there's a better tool to achieve this than VBA, don't hesitate to edit the tags and comment.
edit 3: I did something that works sometimes, but sometimes it doesn't. And it's ridiculously complicated. I set a rule that ccs every sent email with an attachment to me. Another rule runs the following code, when an email from me arrives.
Sub Del(item As Outlook.MailItem)
Call EmptySentEmailFolder
End Sub
The thing has three behaviors, and I haven't been able to determine what triggers which behavior. Sometimes the thing does purge the Sent Items folder. Sometimes it does nothing. Sometimes the second rule gives the "operation failed" error message.
The idea of acting whenever something comes from my address is non-optimal for reasons that I'll omit for the sake of brevity. I tried to replace it with reports. I made a rule that sends a delivery report whenever I send an email. Then another rule runs the code upon receipt of the report. However, this has just one behavior: it never does anything.
Both ideas are so complicated that anything could go wrong really, and I'm having trouble debugging them. Both are non-optimal solutions too.
Would this be an acceptable solution? Sorry its late but my copy of Outlook was broken.
When you enter the Outlook VB Editor, the Project Explorer will be on the left. Click Ctrl+R if it isn't. It will look something like this:
+ Project1 (VbaProject.OTM)
or
- Project1 (VbaProject.OTM)
+ Microsoft Office Outlook Objects
+ Forms
+ Modules
"Forms" will be missing if you do not have any user forms. It is possible "Modules" is expanded. Click +s as necessary to get "Microsoft Office Outlook Objects" expanded:
- Project1 (VbaProject.OTM)
- Microsoft Office Outlook Objects
ThisOutlookSession
+ Forms
+ Modules
Click ThisOutlookSession. The module area will turn white unless you have already used this code area. This area is like a module but have additional privileges. Copy this code to that area:
Private Sub Application_MAPILogonComplete()
' This event routine is called automatically when a user has completed log in.
Dim sentFolder As Outlook.MAPIFolder
Dim entryID As String
Dim i As Long
Set sentFolder = CreateObject("Outlook.Application"). _
GetNamespace("MAPI").GetDefaultFolder(olFolderSentMail)
For i = sentFolder.Items.Count To 1 Step -1
sentFolder.Items(i).Delete ' Move to Deleted Items
Next
Set sentFolder = Nothing
End Sub
I have taken your code, tidied it up a little and placed it within an event routine. An event routine is automatically called when the appropriate event occurs. This routine is called when the user has completed their log in. This is not what you requested but it might be an acceptable compromise.
Suggestion 2
I have not tried an ItemAdd event routine on the Sent Items folder before although I have used it with the Inbox. According to my limited testing, deleting the sent item does not interfere with the sending.
This code belongs in "ThisOutlookSession".
Option Explicit
Public WithEvents MyNewItems As Outlook.Items
Private Sub Application_MAPILogonComplete()
Dim NS As NameSpace
Set NS = CreateObject("Outlook.Application").GetNamespace("MAPI")
With NS
Set MyNewItems = NS.GetDefaultFolder(olFolderSentMail).Items
End With
End Sub
Private Sub myNewItems_ItemAdd(ByVal Item As Object)
Debug.Print "--------------------"
Debug.Print "Item added to Sent folder"
Debug.Print "Subject: " & Item.Subject
Item.Delete ' Move to Deleted Items
Debug.Print "Moved to Deleted Items"
End Sub
The Debug.Print statements show you have limited access to the sent item. If you try to access more sensitive properties, you will trigger a warning to the user that a macro is assessing emails.

Adjusting a VB Script to Programmatically Create a Folder triggered by an E-mail

This is my first time asking a question to y'all. I'm a SQL Developer by trade, and am very green when it comes to VB.
I manage a on-line database for my department, Quickbase, and with this website we manage report requisitions. I create a ticket for each one, and that ticket creates an e-mail notifying the dev. responsible for that assignment. We have folders set up for each request that comes in, and it is very laborious and frustrating to manually create said folders.
So I asked and looked around, coming across a script that was able to do what I needed, or so I am told. However, I'm not sure how to customize it to my needs, nor implement it correctly. This is where I need your assistance, fair programming gods of SO, please help me slay this dragon, and all the riches of the realm will be yours*!
Outlook VBA
Sub MakeFile(MyMail As MailItem)
myMailEntryID = MyMail.EntryID
Set outlookNameSpace = Application.GetNamespace(“MAPI”)
Set outlookMail = outlookNameSpace.GetItemFromID(myMailEntryID)
MyArgument = OutlookMail.Subject
Dim sMyCommand = “c:\makefile.bet ” & MyArgument
Shell “cmd /c ” & sMyCommand, vbHide
End Sub
Makefile.bat
#echo off
cls
mkdir %1
The webtsite URL is: www.quickbase.com
The root folder path: h:///ntsp/data/reports - criteria/quickbase docs/[Folder to be created]
*Riches are not monetary, but the feeling of goodness, and completeness only gained by helping a fellow nerd out, oh and it makes the e-peen grow might and strong!
being a fellow nerd I am going to get you started in the right direction. I think we can achieve what you want with VBA alone and do not need to use shell.
First we need to hook an event of when this all should happen. I imagine that is when your inbox gets an email. If I am right here is the start of this.
Please understand 2 important things.
This only works on items coming into your inbox. Thus if you already have a rule moving items to another folder it will not work.
You need to "Test" the email coming in - my example shows a test of the subject. It will only call you special routine IF and ONLY IF the subject has in it "My Test"
To enter the code in the Visual Basic Editor:
On the Tools menu, point to Macro, and then click Visual Basic Editor.
In the Project pane, click to expand the folders, and then double-click the ThisOutlookSession icon.
Type or paste the following code into the Code window.
Dim WithEvents objInboxItems As Outlook.Items
' Run this code to start your rule.
Sub StartRule()
Dim objNameSpace As Outlook.NameSpace
Dim objInboxFolder As Outlook.MAPIFolder
Set objNameSpace = Application.Session
Set objInboxFolder = objNameSpace.GetDefaultFolder(olFolderInbox)
Set objInboxItems = objInboxFolder.Items
End Sub
' Run this code to stop your rule.
Sub StopRule()
Set objInboxItems = Nothing
End Sub
' This code is the actual rule.
Private Sub objInboxItems_ItemAdd(ByVal Item As Object)
If Item.Subject = "My Test" Then
Call checkForFolder
End If
End Sub
Private Sub checkForFolder()
End Sub
On the File menu, click Save VbaProject.OTM.
You can now run the StartRule and StopRule macros to turn the rule on and off.
Quit the Visual Basic Editor.
(You might need to start and stop Outlook to get the variables to "Hook".
Once you get this working and understood, then you can remove the on off switches.
Then you have to decide your test for making a new folder so that we can then test the email and compare it to existing folders and then make a new one etc.

How do I programatically add a reference to a VBA project?

I'm deploying an early bound styled VBA module that needs Scripting.Dictionary and RegExp.
The script, predictably, fails when it runs on another computer.
The user has to go to Tools->Reference in the VBA IDE and add a reference to those two libraries manually to make it work.
Hence lies the problem. Asking the non-technical end user to go to the IDE and manually add references is asking way too much of them.
The other alternative is to rewrite the whole (very long script written by someone else) to use late binding. I rather not take this path if there are other methods.
As an altervative, some people suggest adding a reference programatically like so:
Application.VBE.ActiveVBProject.References.AddFromFile [Path to library]
Is this the correct solution and if so are there any downsides of this strategy?
If not, are there other methods that will to enable the code to remain early bound yet does not require references to be added manually by the user.
Suggestions involving direct calls to the Win32/64 API are also welcome.
Thanks.
In my own limited environment (small # of other people using spreadsheets I develop, relatively standard machine setups), if I create the file and add the references, and then give a copy to someone else, they can open it with no problems and not have to do anything, so keep that in mind with this answer. (I'm wondering why that doesn't work for you.) Also, this was with Excel.
Rather than adding a reference from a file path, you might consider using the GUID property instead.
Here is some code I once used to automatically create references in a newly created workbook. (It's part of a script that would export code, references, and unit tests on worksheets to text for use with Subversion and then later reconstitute the workbook from the text files.) You might find it useful to your situation. (EH and cleanup removed to keep it short...)
'Export refs in existing workbook to text file
Private Sub exportRefs_(srcWbk As Workbook)
Dim fs As FileSystemObject
Set fs = New FileSystemObject
Dim tsout As TextStream
Set tsout = fs.CreateTextFile(fs.BuildPath(getTargetPath_(srcWbk), "refs.refs"))
Dim ref As Reference
For Each ref In Application.ThisWorkbook.VBProject.References
Call tsout.WriteLine(ref.GUID)
Next ref
'<EH + cleanup...>
End Sub
'Add refs to newly created workbook based on previously exported text file
Private Sub importRefs_(wbk As Workbook, path As String)
Dim fs As FileSystemObject
Set fs = New FileSystemObject
Dim tsin As TextStream
Set tsin = fs.OpenTextFile(path)
Dim line As String
Dim ref As Reference
While Not tsin.AtEndOfStream
line = tsin.ReadLine()
Set ref = Nothing
On Error Resume Next
Set ref = wbk.VBProject.References.AddFromGuid(line, 0, 0)
On Error GoTo 0
If ref Is Nothing Then
Debug.Print "add failed: " & line
End If
Wend
'<EH + cleanup...>
End Sub
Like, I said, limited environment, but hopefully it helps.