I am using a modified code I found online, and I am having issues finding a shared calendar in outlook.
Sub SharedCalendarEventCreator()
Dim olApp As Outlook.Application
Dim outNameSpace As Namespace
Dim outSharedName As Outlook.Recipient
Dim outCalendarFolder As MAPIFolder
Dim olAppItem As Outlook.AppointmentItem
Dim r As Long
Dim eduSheet As Worksheet
On Error Resume Next
Worksheets("Schedule").Activate
Set olApp = GetObject("", "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not available!"
Exit Sub
End If
End If
Set outNameSpace = olApp.GetNamespace("MAPI")
Set outCalendarFolder = outNameSpace.Folders("Lunch Calendar")
Set outCalendarFolder = outCalendarFolder.Folders("Calendar")
Set olAppItem = outCalendarFolder.Items.Add(olAppointmentItem)
r = 7
Do Until Trim$(Cells(r, 1).Value) = ""
With olAppItem
.Subject = "SOF " & Cells(1, 2).Value & " " & Cells(2, 2).Value & " " & Cells(3, 2).Value & " " & Cells(r, 2).Value
.Start = Cells(r, 1).Value
vArray = Split(Cells(4, 2).Value2, ";")
For Each vElement In vArray
'.Recipients.Add .Recipients.Add(vElement)
Next vElement
.MeetingStatus = olMeeting
.AllDayEvent = True
.Body = Cells(r, 3).Value
.ResponseRequested = False
.Send
r = r + 1
End With
Loop
Set olAppItem = Nothing
Set outCalendarFolder = Nothing
Set outNameSpace = Nothing
Set olApp = Nothing
End Sub
Picture of the Calendar location:
I believe the issue is within these lines, but I have tried to change them without any luck:
Set outNameSpace = olApp.GetNamespace("MAPI")
Set outCalendarFolder = outNameSpace.Folders("Lunch Calendar")
Set outCalendarFolder = outCalendarFolder.Folders("Calendar")
Set olAppItem = outCalendarFolder.Items.Add(olAppointmentItem)
The error I received:
When I hit debug, this is the line it highlights:
Any help is greatly appreciated.
Navigate the folder tree from "janedoe#gmail" to "Calendar" to "Lunch Calendar".
Option Explicit
Sub SharedCalendarEventCreator()
' Early binding - Set reference to Outlook XX.X Object Library
Dim olApp As Outlook.Application
Dim outNameSpace As Outlook.Namespace
Dim outMailboxFolder As Outlook.Folder
Dim outCalendarFolder As Outlook.Folder
Dim outCalendarSubFolder As Outlook.Folder
Dim olAppItem As Outlook.AppointmentItem
On Error Resume Next
Set olApp = GetObject("", "Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
On Error Resume Next
Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
If olApp Is Nothing Then
MsgBox "Outlook is not available!"
Exit Sub
End If
End If
Set outNameSpace = olApp.GetNamespace("MAPI")
Set outMailboxFolder = outNameSpace.Folders("janedoe#gmail")
Set outCalendarFolder = outMailboxFolder.Folders("Calendar")
Set outCalendarSubFolder = outCalendarFolder.Folders("Lunch Calendar")
'Set ActiveExplorer.CurrentFolder = outCalendarSubFolder
Set olAppItem = outCalendarSubFolder.Items.Add(olAppointmentItem)
olAppItem.Display
Set olAppItem = Nothing
Set outCalendarSubFolder = Nothing
Set outCalendarFolder = Nothing
Set outMailboxFolder = Nothing
Set outNameSpace = Nothing
Set olApp = Nothing
End Sub
Related
Now I'm export Global contact list in outlook 2016. to Excel file. This's my code:
Sub ExportOutlookAddressBook()
Application.ScreenUpdating = False
Dim olApp As Outlook.Application
Dim olNS As Outlook.Namespace
Dim olAL As Outlook.AddressList
Dim olEntry As Outlook.AddressEntry
Set olApp = Outlook.Application
Set olNS = olApp.GetNamespace("MAPI")
Set olAL = olNS.AddressLists("Global Address List") 'Change name if different contacts list name
ActiveWorkbook.ActiveSheet.Range("a1").Select
For Each olEntry In olAL.AddressEntries
'ActiveCell.Value = olEntry.GetContact.FullName
ActiveCell.Offset(0, 0).Value = olEntry.Name
ActiveCell.Offset(1, 0).Select
Next olEntry
Set olApp = Nothing
Set olNS = Nothing
Set olAL = Nothing
Application.ScreenUpdating = True
End Sub
It have Runtime Error 91!.How can I Fix this Error? >/\<
Use GetExchangeUser istead of GetContact.
For example:
Dim olExUser As Outlook.ExchangeUser
Set olExUser = Nothing
Set olExUser = olEntry.GetExchangeUser
If Not olExUser Is Nothing Then
With olExUser
ActiveCell.Value = .FirstName & " " & .LastName
End With
End If
Can somebody please help me out. I have this code and it reads all appointments from the calendar for specific dates; however, the code doesn't display any recurring meetings within given dates:
ToDate = CDate("10/12/2019")
FromDate = CDate("10/06/2019")
'On Error Resume Next
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
'include reccuring items
'-------------------------
olFolder.Items.Sort ("[Start]")
olFolder.Items.IncludeRecurrences = True
'-------------------------
NextRow = 2
With Sheets("Sheet1") 'Change the name of the sheet here
.Range("A1:F1").Value = Array("Report Date", "Date", "Time spent", "Location", "Categories", "Title")
For Each olApt In olFolder.Items
If (olApt.Start >= FromDate And olApt.Start <= ToDate) Then
.Cells(NextRow, "A").Value = Format(Now, "DD-MM-YY")
.Cells(NextRow, "B").Value = CDate(olApt.Start)
.Cells(NextRow, "C").Value = olApt.End - olApt.Start
.Cells(NextRow, "C").NumberFormat = "HH:MM"
.Cells(NextRow, "E").Value = olApt.Categories
.Cells(NextRow, "F").Value = olApt.Subject
NextRow = NextRow + 1
Else
End If
Next olApt
.Columns.AutoFit
End With
Can somebody please help me out and let me know what I am doing wrong?!
Thank you!
Items in a folder have to be made into a collection to then manipulate.
On Error Resume Next ' This is a rare proper use.
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
Set itmCollection = olFolder.Items
'include recurring items
'-------------------------
itmCollection.Sort ("[Start]")
itmCollection.IncludeRecurrences = True
'-------------------------
For Each olApt In itmCollection
Demo code
Option Explicit
Sub apptsInDateRangeIncludingRecurrences()
Dim ToDate As Date
Dim FromDate As Date
Dim olApp As Object
Dim olNS As Object
Dim olFolder As Object
Dim itmCollection As Object
Dim itmCollectionFrom As Object
Dim itmCollectionFromTo As Object
Dim sFilter As String
Dim olApt As Object
ToDate = CDate("10/12/2019")
FromDate = CDate("10/06/2019")
On Error Resume Next ' This is a rare proper use.
Set olApp = GetObject(, "Outlook.Application")
If Err.Number > 0 Then Set olApp = CreateObject("Outlook.Application")
On Error GoTo 0
Set olNS = olApp.GetNamespace("MAPI")
Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
Set itmCollection = olFolder.Items
'include recurring items
'-------------------------
itmCollection.Sort ("[Start]")
itmCollection.IncludeRecurrences = True
'-------------------------
'Filter for applicable items
sFilter = "[Start]>='" & FromDate & "'"
'Debug.Print sFilter
Set itmCollectionFrom = itmCollection.Restrict(sFilter)
sFilter = "[Start]<='" & ToDate & "'"
'Debug.Print sFilter
Set itmCollectionFromTo = itmCollectionFrom.Restrict(sFilter)
For Each olApt In itmCollectionFromTo
If olApt.Start >= FromDate Then
Debug.Print olApt.Start & " " & olApt.Subject
End If
Next olApt
Debug.Print "Done."
End Sub
I have a worksheet set up to send email notifications when certain cells contain values, such as "75%" and "Due".
Around half the time I get
Runtime error 462
It seems this is because I have not specified elements of my code properly.
Private Sub Worksheet_Change(ByVal Target As Range)
Call Check_Project_Progress
End Sub
Private Sub Send_Email(Optional ByVal email_title As String = "")
Dim olNS As Namespace
Dim olMail As MailItem
Set olNS = GetNamespace("MAPI")
Set olMail = CreateItem(olMailItem)
With olMail
.Subject = email_title
.To = "LearnDataAnalysis#outlook.com"
.Body = "Value reads 75%"
.SendUsingAccount = olNS.Accounts.Item(1)
.Display
'.Send
End With
Set olMail = Nothing
Set olNS = Nothing
End Sub
Private Sub Send_Email2(Optional ByVal email_title As String = "")
Dim olNS As Namespace
Dim olMail As MailItem
Set olNS = GetNamespace("MAPI")
Set olMail = CreateItem(olMailItem)
With olMail
.Subject = email_title
.To = "LearnDataAnalysis#outlook.com"
.Body = "PLA determination due"
.SendUsingAccount = olNS.Accounts.Item(1)
.Display
'.Send
End With
Set olMail = Nothing
Set olNS = Nothing
End Sub
Private Sub Check_Project_Progress()
Dim LastRow As Long, RowNumber As Long
Dim ws As Worksheet
Set ws = ActiveSheet
With ws
LastRow = 500
If 4 > LastRow Then Exit Sub
For RowNumber = 4 To LastRow
If .Cells(RowNumber, "AB").Value = 0.75 And .Cells(RowNumber, "AD").Value <> "S" Then
.Cells(RowNumber, "AD").Value = "S"
.Cells(RowNumber, "AE") = "Email sent on:" & Now()
Call Send_Email(.Cells(RowNumber, "C").Value & " is approaching deadline")
End If
If 4 > LastRow Then Exit Sub
If .Cells(RowNumber, "AC").Value = "Due" And .Cells(RowNumber, "AD").Value <> "S,S" Then
.Cells(RowNumber, "AD").Value = "S,S"
.Cells(RowNumber, "AF") = "Email sent on:" & Now()
Call Send_Email2(.Cells(RowNumber, "C").Value & " has met deadline")
End If
Next RowNumber
End With
Set ws = Nothing
End Sub
Again, the error message is Runtime error 462. I have little idea what I'm doing.
You are getting that error becuase the code is not able to connect to the outlook application.
Try this. I have shown you for Send_Email. Adapt it for Send_Email2 as well.
Private Sub Send_Email(Optional ByVal email_title As String = "")
Dim olNS As Object
Dim olMail As Object
Dim OutlookApp As Object
'~~> Establish an Outlook application object
On Error Resume Next
Set OutlookApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Set OutlookApp = CreateObject("Outlook.Application")
End If
Err.Clear
On Error GoTo 0
'~~> Check if we managed to connect to Outlook
If OutlookApp Is Nothing Then
MsgBox "unable to connect to outlook"
Exit Sub
End If
Set olNS = OutlookApp.GetNamespace("MAPI")
Set olMail = OutlookApp.CreateItem(0)
With olMail
.Subject = email_title
.To = "LearnDataAnalysis#outlook.com"
.Body = "Value reads 75%"
.SendUsingAccount = olNS.Accounts.Item(1)
.Display
'.Send
End With
End Sub
I am to write a VBA code that would retrive emails from Outlook based on certain conditions. The problem I have is that I have to denote a certain folder in my code (in the example below the folder denoted is "PRE Costumer". I would like to retrive all emails from my 'inbox' or in better case from all outlook folders. The problem is that my inbox consists of many subfolders (because of rules0. My problem is that I may not know all the subfolders names (as many useres are going to use the macro and even someone can have the e mails in Personal Folders).
Could you please advise is there a way to overcome this problem?
Please let me know if this question is vague (as I am newcomer)
Please find the line that I have probelm with marked with a comment.
Sub GetFromInbox()
Dim olApp As Outlook.Application
Dim olNs As Namespace
Dim Fldr As MAPIFolder
Dim olMail As Variant
Dim i As Integer
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
'Below is the line I have problem with
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("PRE Customer")
i = 1
x = Date
For Each olMail In Fldr.Items
If InStr(olMail.Subject, "transactions") > 0 _
And InStr(olMail.ReceivedTime, x) > 0 Then
ActiveSheet.Cells(i, 1).Value = olMail.Subject
ActiveSheet.Cells(i, 2).Value = olMail.ReceivedTime
ActiveSheet.Cells(i, 3).Value = olMail.SenderName
i = i + 1
End If
Next olMail
Set Fldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
Just loop through all the folders in Inbox.
Something like this would work.
Edit1: This will avoid blank rows.
Sub test()
Dim olApp As Outlook.Application, olNs As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder, olMail As Outlook.MailItem
Dim eFolder As Outlook.Folder '~~> additional declaration
Dim i As Long
Dim x As Date, ws As Worksheet '~~> declare WS variable instead
Dim lrow As Long '~~> additional declaration
Set ws = Activesheet '~~> or you can be more explicit using the next line
'Set ws = Thisworkbook.Sheets("YourTargetSheet")
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
x = Date
For Each eFolder In olNs.GetDefaultFolder(olFolderInbox).Folders
'Debug.Print eFolder.Name
Set olFolder = olNs.GetDefaultFolder(olFolderInbox).Folders(eFolder.Name)
For i = olFolder.Items.Count To 1 Step -1
If TypeOf olFolder.Items(i) Is MailItem Then
Set olMail = olFolder.Items(i)
If InStr(olMail.Subject, "transactions") > 0 _
And InStr(olMail.ReceivedTime, x) > 0 Then
With ws
lrow = .Range("A" & .Rows.Count).End(xlup).Row
.Range("A" & lrow).Offset(1,0).value = olMail.Subject
.Range("A" & lrow).Offset(1,1).Value = olMail.ReceivedTime
.Range("A" & lrow).Offset(1,2).Value = olMail.SenderName
End With
End If
End If
Next i
Set olFolder = Nothing
Next eFolder
End Sub
Above takes care of all subfolders in Inbox.
Is this what you're trying?
To fix your error (olFolderInbox is a Outlook only constant, so you need to define it in vba that is not Outlook):
Const olFolderInbox = 6
'...
Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("PRE Customer")
Also to prevent missing Reference when run from another computer, I would:
Dim olApp As Object
Dim olNs As Object
Dim Fldr As Object
Dim olMail As Object
Dim i As Long
Set olApp = CreateObject("Outlook.Application")
'...
You might also want to disable ScreenUpdating, then enable it in Excel if you expect a long list.
UPDATE (Solution for all folders from a Root Folder)
I used something slightly different for comparing the dates.
Option Explicit
Private lRow As Long, x As Date, oWS As Worksheet
Sub GetFromInbox()
Const olFolderInbox = 6
Dim olApp As Object, olNs As Object
Dim oRootFldr As Object ' Root folder to start
Dim lCalcMode As Long
Set olApp = CreateObject("Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set oRootFldr = olNs.GetDefaultFolder(olFolderInbox).Folders("PRE Customer")
Set oWS = ActiveSheet
x = Date
lRow = 1
lCalcMode = Application.Calculation
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
GetFromFolder oRootFldr
Application.ScreenUpdating = True
Application.Calculation = lCalcMode
Set oWS = Nothing
Set oRootFldr = Nothing
Set olNs = Nothing
Set olApp = Nothing
End Sub
Private Sub GetFromFolder(oFldr As Object)
Dim oItem As Object, oSubFldr As Object
' Process all mail items in this folder
For Each oItem In oFldr.Items
If TypeName(oItem) = "MailItem" Then
With oItem
If InStr(1, .Subject, "transactions", vbTextCompare) > 0 And DateDiff("d", .ReceivedTime, x) = 0 Then
oWS.Cells(lRow, 1).Value = .Subject
oWS.Cells(lRow, 2).Value = .ReceivedTime
oWS.Cells(lRow, 3).Value = .SenderName
lRow = lRow + 1
End If
End With
End If
Next
' Recurse all Subfolders
For Each oSubFldr In oFldr.Folders
GetFromFolder oSubFldr
Next
End Sub
I'm trying to add appointments to a non default calendar in Outlook through Excel.
All is okay when I add the appointment to the default calendar.
Code for default calendar:
Sub Appointments()
Const olAppointmentItem As Long = 1
Dim OLApp As Object
Dim OLNS As Object
Dim OLAppointment As Object
On Error Resume Next
Set OLApp = GetObject(, "Outlook.Application")
If OLApp Is Nothing Then Set OLApp = CreateObject("Outlook.Application")
On Error GoTo 0
If Not OLApp Is Nothing Then
Set OLNS = OLApp.GetNamespace("MAPI")
OLNS.Logon
Set OLAppointment = OLApp.Item.Add(olAppointmentItem)
OLAppointment.Subject = Range("A1").Value
OLAppointment.Start = Range("C3").Value
OLAppointment.Duration = Range("C1").Value
OLAppointment.ReminderMinutesBeforeStart = Range("D1").Value
OLAppointment.Save
Set OLAppointment = Nothing
Set OLNS = Nothing
Set OLApp = Nothing
End If
End Sub
I'm trying to use the "Folders" object to set the non default calendar but Excel returns a compile error.
Sub Appointments()
Const olAppointmentItem As Long = 1
Dim OLApp As Object
Dim OLNS As Object
Dim OLAppointment As Object
Dim miCalendario As Object
On Error Resume Next
Set OLApp = GetObject(, "Outlook.Application")
If OLApp Is Nothing Then Set OLApp = CreateObject("Outlook.Application")
On Error GoTo 0
If Not OLApp Is Nothing Then
Set OLNS = OLApp.GetNamespace("MAPI")
OLNS.Logon
Set miCalendario = OLApp.Session.GetDefaultFolder(9).Folders("a")
Set OLAppointment = miCalendario.Item.Add(olAppointmentItem)
OLAppointment.Subject = Range("A1").Value
OLAppointment.Start = Range("C3").Value
OLAppointment.Duration = Range("C1").Value
OLAppointment.ReminderMinutesBeforeStart = Range("D1").Value
OLAppointment.Save
Set OLAppointment = Nothing
Set OLNS = Nothing
Set OLApp = Nothing
End If
End Sub
I made this script for Outlook. I'm trying to modify for Excel.
Sub AddContactsFolder()
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myNewFolder As Outlook.AppointmentItem
Set myNameSpace = Application.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderCalendar).Folders("aa")
MsgBox myFolder
Set myNewFolder = myFolder.Items.Add(olAppointmentItem)
With myNewFolder
.Subject = "aaaaa"
.Start = "10/11/2013"
.ReminderMinutesBeforeStart = "20"
.Save
End With
End Sub
The line
Set OLAppointment = miCalendario.Item.Add(olAppointmentItem)
must be
Set OLAppointment = miCalendario.Items.Add(olAppointmentItem)