I'm really new at VBA but I have this same code in another database, and now I've just copied the code and paste in another database but I get this Object Required error in the last line.
The code is bigger but I've just stopped on the line where I get the error.
Dim frm As Form, ctl As Control
Dim varItm As Variant
Dim stgMO, stgPID, stMail, stgMailCC As String
Dim Question As Long
Dim OutApp, OutMail As Object
Set frm = Forms!Overview
Set ctl = frm!cl_onboarding
stgMO = ctl.Column(7)
stgPID = ctl.Column(2)
stgMail = ctl.Column(8)
stgMailCC = ctl.Column(9)
Question = MsgBox("Do you want to send an e-mail containing the codes for this Agent?", vbYesNo, "Send e-mail")
If Question = vbYes Then
Set OutApp = Outlook.Application
you have dim stMail as a variable but then you use stgMail. just check your spelling.
You also use dim question as long, this confuses me a bit because I thought long meant an integer (there are min and max values but I cant remember).
You need to set a Reference in VBA to Outlook.
VBA editor -> menu Tools -> References
Select and check Microsoft Outlook 14.0 Object Library
Edit: while the above is true, you would probably get a different error if the reference was missing.
The problem may be in this line:
Dim OutApp, OutMail As Object
which actually gets evaluated as
Dim OutApp As Variant, OutMail As Object
and should read
Dim OutApp As Object, OutMail As Object
But a Variant can hold an object too, so this may also not be the cause of the error.
Related
Dim oApp As Outlook.Application
Dim oMail As MailItem
Dim oMail As String
Dim strbody As String
Dim fdatum As String
Dim VorschauBereich As Range
Dim Tabnr As Integer
Dim Tabtext As String
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(oIMailItem)
I get the error named in the title and cursor jumps to Dim Dim oApp As Outlook.Application
To solve this, you can either use early binding by adding the Mircrosoft Object Library in your references (from the tools menu). Or you can use Late binding by changing the Outlook object declaration to this:
Dim oApp As Object
Dim oMail As Object
and then creating the objects like this:
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(0)
Depending on whether or not your excel WB is used by other people, I prefer late binding to avoid any issues with references on other people's machines. The downside of late binding is that you don't get inteli-text help while coding. Hope this helps!
Tools, References, Available References.
Locate and check Microsoft Outlook xx.x Object Library.
The code below deletes appointments in a subfolder of the Outlook default calendar. I have commented out the line giving the run-time error 462: "The remote server machine does not exist or is unavailable".
Is there a change I could make to this code to solve this error? Thanks for any guidance.
Public Sub DeleteAppt()
Dim olApp As Object 'Outlook.Application
Dim olNS As Object 'Outlook.Namespace
Dim olAptItemFolder As Object 'Outlook.Folder
Dim olAptItem As Object 'Outlook.AppointmentItem
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
Set olNS = olApp.Session
Set olAptItemFolder = olNS.GetDefaultFolder(olFolderCalendar).Folders("TestCal")
''''For i = olAptItemFolder.Count To 1 Step -1
Set olAptItem = olAptItemFolder.Items(i)
If olAptItem.Subject Like "***" Then
olAptItem.Delete
End If
Next i
Set olAptItem = Nothing
Set olAptItemFolder = Nothing
Set olApp = Nothing
End Sub
olAptItemFolder has no Count property. olAptItemFolder.Items does. Apart from the issues others noted in comments above, try
For i = olAptItemFolder.Items.Count To 1 Step -1
Edited to add: if you are not setting a reference to something's object lirary, you can't use its enums unless you fully qualify each use. It's generally simpler, easier and quicker to find out the numerical value of the enum and use that instead. Then add a comment on the end of the line to remind yoursef, six months from now, that '9 = olFolderCalendar
i have a very simple small app in mind which will help me save time from alt tabbing from excel to outlook. i want to create a small userform that will have a textbox for a exchange user alias and return the exchange user's full name. now the issue i have here is that the guide in msdn is a little vague for a userform: https://msdn.microsoft.com/en-us/library/office/ff869721.aspx and i'm getting some error messages, some got fixed by activating some references. and the code is quite complicated.
so basically i have 2 textboxes and a button. textbox1 will accept the alias, textbox2 will return the username after clicking the button.
there are several examples but most of them will result in dumping the GAL to an excel file which i don't need.
thanks in advanced
This will give you what you want.
Private Function GetFullName(inAlias As String) As String
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olAdd As Outlook.AddressEntries
Dim olMem As Outlook.AddressEntry
Dim olLst As Outlook.AddressList
Dim olAlias As String
On Error Resume Next
Set olApp = New Outlook.Application
On Error GoTo 0
If olApp Is Nothing Then
GetFullName = "Source not available"
Exit Function
End If
Set olNS = olApp.GetNamespace("MAPI")
Set olLst = olNS.GetGlobalAddressList
Set olAdd = olLst.AddressEntries
For Each olMem In olAdd
On Error Resume Next
olAlias = olMem.GetExchangeUser.Alias
On Error GoTo 0
If olAlias = inAlias Then
GetFullName = olMem.GetExchangeUser.Name
Exit For
Else
GetFullName = "Invalid Alias"
End If
Next
Set olApp = Nothing: Set olNS = Nothing: Set olAdd = Nothing
End Function
The draw back is this may take a while if your GAL is quite large.
I'll check if I can dump the list to an array first and then manipulate from there.
Or if there is another way to get to the name via alias using other method.
But for now, try learning from this first.
This is a function, so to get it to your textbox, you can simply:
TextBox2 = GetFullname(TextBox1)
Note:
I intentionally declared all the objects, you need to know what type of object you're working on. Also I used On Error Resume Next because there are AddressEntry without Alias and that gives error. That's the easiest work around I can think of for now.
Requirement:
You need to reference Microsoft Outlook xx.x Object Library.
xx.x vary depending on the Outlook version installed in your machine.
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...
How do I track e-mails in and out of a shared in-box in Outlook using excel? We have a large number of e-mails coming in and we need to track responses to make sure that e-mails don't get lost.
Is there as way to get the results from advanced find to an excel sheet?
What view are you setting up in advanced find ? As you can write a VBA macros to pull items from your inbox and put them into you speadsheet. Alot of the advance find option are not in the outlook object model so it depends on the view you are trying to set up.
So can you tell me what you are doing in advanced find ..?
76mel
Ok using outlook tables you can put this in your Excel as a macro
Use "sfilter" to define your advance search criteria.
You will have to pump the data into Excel at the bottom.
Sub GetMail()
Dim oApp As Outlook.Application
Dim oFolder As Outlook.Folder
Dim oNameSpace As Outlook.Namespace
Dim emailCount As Integer
Dim counter As Integer
Dim sfilter As String
Dim oRow As Outlook.Row
Dim oTable As Outlook.Table
Dim i As Outlook.MailItem
Set oApp = CreateObject("Outlook.Application")
Set oNameSpace = oApp.Session
Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)
'Add what ever filter you want here using DASL
sfilter = "[LastModificationTime] > '5/1/2005'"
'Restrict with Filter
Set oTable = oFolder.GetTable(sfilter)
'Remove all columns in the default column set
oTable.Columns.RemoveAll
'Specify desired properties
With oTable.Columns
.Add ("EntryID")
.Add ("Subject")
.Add ("ReceivedTime")
End With
'Enumerate the table using test for EndOfTable
'Pump it into your worksheet
Do Until (oTable.EndOfTable)
Set oRow = oTable.GetNextRow()
Debug.Print (oRow("EntryID"))
Debug.Print (oRow("Subject"))
Debug.Print (oRow("ReceivedTime"))
Loop
'Clean up
Set oTable = Nothing
Set oFolder = Nothing
Set oNameSpace = Nothing
Set oApp = Nothing
End Sub
Maybe you should invest in a tool like FogBugz that can handle incoming email, filters spam and tracks responses.
I've found a stop gap measure; just highlight all results you get from advanced find, then ctrl + A, then ctrl + C, you can then paste the results into excel (ctrl + V).
Still I'd like to hear of any other solutions.
Excel doesn't do this well. At my company we simply use flags for anything urgent. When someone responds to a customer, they drag the original message to their folder within the shared mailbox.