I am trying to generate emails in a chosen Outlook folder from an Excel macro.
In the code below, 'TestDraft1' works, generating an email in the folder chosen by the user via the user the Namespace.PickFolder method.
However, since I want to generate many emails in the same folder, I need the user to be able to pick the folder just once. In 'TestDraft2' below, I try to save the folder as an Object, then pass it to the move function. This fails with "Runtime Error 13, Type Mismatch". The 'folder' variable gives the type "MAPIFolder".
What am I missing here? How is the object I assign to 'folder' different than the one I pass to 'Mailitem.move' ?
Edit Note that I'm using late-binding, which is why I'm defining my variables as Objects. I'd prefer to keep it that way, since I'd like to be able to distribute it without special configuration. What I really want to know is why the first version succeeds while the second version fails. Further details: My employer uses Office 2007.
Edit 2 The problem had nothing to do with what the type of object returned by PickFolder. It had to do with my misuse of VBA syntax. I've answered the question below, and I'm renaming the it to better address what I was really asking. Original title was: Returning an Outlook Folder object in Excel VBA using PickFolder
Sub test()
Call TestDraft1("test@example.com", "test1") ' succeeds
Call TestDraft2("test@example.com", "test2") ' fails
End Sub
Sub TestDraft1(recip As String, subj As String)
Dim OlApp As Object
Dim NS As Object
Dim OlMail As Object
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set OlMail = OlApp.createitem(0) 'olMailitem = 0
OlMail.Subject = subj
OlMail.Recipients.Add (recip)
OlMail.Move (NS.PickFolder) ' pass the results of the folder-picker directly
End Sub
Sub TestDraft2(recip As String, subj As String)
Dim OlApp As Object
Dim NS As Object
Dim folder As Object
Dim OlMail As Object
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set folder = NS.PickFolder ' save the results of the folder-picker...
' MsgBox (TypeName(folder)) ' returns "MAPIFolder
Set OlMail = OlApp.createitem(0) 'olMailitem = 0
OlMail.Subject = subj
OlMail.Recipients.Add (recip)
OlMail.Move (folder) ' ... and use the saved results of the folder-picker -> runtime error
End Sub
This shows how new I am to VBA syntax.
Replacing:
OlMail.Move (folder)
with
OlMail.Move folder
fixed the problem.
This is what I believe is going on: MailItem.Move returns no result, so it uses the non-parentheses syntax.
See: http://msdn.microsoft.com/en-us/library/office/gg278645
Thus, (folder) is interpreted as an expression, and returns the text value of the folder name inside the parentheses. Evidence:
Dim testobj as Object
Set testobj = (folder) ' fails: Object required
Dim testvar as Variant
testvar = (folder)
MsgBox (VarType(testvar) = vbString) ' returns True
So, while folder
is an object of the correct type, (folder)
is an expression which evaluates to a string.
maybe you just miss the objmail.save before moving it.
and your definitions are a problem for sure: Change the first lines of your Sub as follows (untested):
Sub TestDraft2(recip As String, subj As String)
Dim olApp As Outlook.Application
Dim NS As Outlook.Namespace
Dim folder As Outlook.MAPIFolder
Dim OlMail As Outlook.mailitem
Set OlApp = CreateObject("Outlook.Application")
Set NS = OlApp.GetNamespace("MAPI")
Set folder = NS.PickFolder
that could help...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.