Outlook VBA, show meeting series expiring in the next 30 days - vba

I am trying to adapt an outlook vba script that shows if a named meeting series is expiring on that day. I would like to make it look through all my meeting series, and show a reminder message for any when the end date is within the next 30 days.
Here is a start of what I currently have that will work if I supply the one meeting name (TestRecur):
Sub ExtendAppointmentSample()
Dim myApptItem As Outlook.AppointmentItem
Dim myRecurrPatt As Outlook.RecurrencePattern
Dim myNamespace As Outlook.NameSpace
Dim myFolder As Outlook.Folder
Dim myItems As Outlook.Items
Dim QuestionToMessageBox
QuestionToMessageBox = "Appointment is about to expire, would you like to extend it?"
Set myNamespace = Application.GetNamespace("MAPI")
Set myFolder = myNamespace.GetDefaultFolder(olFolderCalendar)
Set myItems = myFolder.Items
' specify the Item Object here but I would like it to cycle through all meeting series and look for those ending within 30 days
Set myApptItem = myItems.Item("TestRecur")
Set myRecurrPatt = myApptItem.GetRecurrencePattern
If myRecurrPatt.PatternEndDate < DateAdd("d", 30, Date) Then
YesOrNoAnswerToMessageBox = MsgBox(QuestionToMessageBox, vbInformation, "Extend")
'Next line does not work; not sure how to get each Meeting Title
'MsgBox (MeetingTitle)
End If
End Sub

show meeting series expiring in the next 30 days
In that case you need to find all meetings that take place during these 30 days in the calendar. Then you can check their PatternEndDate property value when they ends. To get all calendar items for a specific time frame you can use the Find/FindNext or Restrict methods of the Items class. Read more about these methods in the articles that I wrote for the technical blog:
How To: Retrieve Outlook calendar items using Find and FindNext methods
How To: Use Restrict method in Outlook to get calendar items
For example, you may use the following code to find appointments for a specific time frame (VB.NET):
Private Sub FindActiveAppointments(folder As Outlook.MAPIFolder)
Dim dateTimeStart As String = DateTime.Now.ToString("MM/dd/yyyy hh:mm tt")
Dim dt As DateTime = New DateTime(DateTime.Now.Year, DateTime.Now.Month, _
DateTime.Now.Day, 23, 59, 0, 0)
Dim dateTimeEnd As String = dt.ToString("MM/dd/yyyy hh:mm tt")
Dim searchCriteria As String = "[Start]<=""" + dateTimeEnd + """ AND [End]>=""" + _
dateTimeStart + """"
Dim strBuilder As StringBuilder = Nothing
Dim counter As Integer = 0
Dim appItem As Outlook._AppointmentItem = Nothing
Dim folderItems As Outlook.Items = Nothing
Dim resultItem As Object = Nothing
folderItems = folder.Items
folderItems.IncludeRecurrences = True
folderItems.Sort("[Start]")
If (folderItems.Count > 0) Then
resultItem = folderItems.Find(searchCriteria)
If Not IsNothing(resultItem) Then
strBuilder = New StringBuilder()
Do
If (TypeOf (resultItem) Is Outlook._AppointmentItem) Then
counter += 1
appItem = resultItem
strBuilder.AppendLine("#" + counter.ToString() + _
" Subbject: " + appItem.Subject + _
" Organizer: " + appItem.Organizer + _
" Location: " + appItem.Location)
End If
resultItem = folderItems.FindNext()
Loop Until IsNothing(resultItem)
End If
End If
If Not IsNothing(strBuilder) Then
Debug.WriteLine(strBuilder.ToString())
Else
Debug.WriteLine("There is no match in the " + _
folder.Name + " folder.")
End If
End Sub

Related

Retrieving MS Access Database to vb6 and filter data using 2 DTpicker for the sum rate

Private Sub Command5_Click()
Dim li As ListItem
Dim lngRunningTotal As Long
For Each li In ListView1.ListItems
lngRunningTotal = lngRunningTotal + CLng(li.SubItems(6)) 'calculate all the in Total min column
Next
sumText.Text = CStr(lngRunningTotal) 'display total
End Sub
I want to filter data using 2 DTpicker for the sum rate.
Here's how you can do it with the code you posted:
Private Sub Command5_Click()
Dim li As ListItem
Dim lngRunningTotal As Long
Dim iLogDateIndex As Integer
Dim iMinutesIndex As Integer
iLogDateIndex = 5
iMinutesIndex = 6
For Each li In ListView1.ListItems
If CDate(li.SubItems(iLogDateIndex)) >= DTPickerStart And CDate(li.SubItems(iLogDateIndex)) <= DTPickerEnd Then
lngRunningTotal = lngRunningTotal + CInt(li.SubItems(iMinutesIndex)) ' calculate all the in Total min column
End If
Next
' Display total
sumText.Text = CStr(lngRunningTotal)
End Sub
The following demonstrates how to filter your data using an SQL Statement:
Private Sub cmdQuery_Click()
Dim objAdoConnection As New ADODB.Connection
Dim objRecordset As ADODB.Recordset
Dim sConnectionString As String
Dim sSQLStatement As String
Dim sDatabaseFile As String
Dim itm As ListItem
' Path to Access database
sDatabaseFile = "C:\Temp\Stack\ADO\Database.accdb"
' Connection string
sConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=" & sDatabaseFile
' Open Connection
objAdoConnection.Open sConnectionString
' Open recordset with SQL query
sSQLStatement = "SELECT * FROM TimeCards WHERE WorkDate BETWEEN #" & DTPickerStart & "# AND #" & DTPickerEnd & "#"
Set objRecordset = objAdoConnection.Execute(sSQLStatement)
' Clear ListView
ListView1.ListItems.Clear
' Move Recordset to first record
objRecordset.MoveFirst
' Display record data
Do While Not objRecordset.EOF
' Add record to ListView
Set itm = ListView1.ListItems.Add(, , objRecordset.Fields("ID"))
itm.SubItems(1) = objRecordset.Fields("EmployeeID")
itm.SubItems(2) = objRecordset.Fields("WorkDate")
itm.SubItems(3) = objRecordset.Fields("WorkHours")
' Move to next record
objRecordset.MoveNext
Loop
' Close connection and release objects
objAdoConnection.Close
Set objRecordset = Nothing
Set objAdoConnection = Nothing
End Sub
I made a quick UI with two DTPicker controls and a Query button. The results get shown into a ListView control:
I also created a simple TimeCards table with the following data for testing:
You can research SQL Statements on the internet and find many ways you can filter your data.

Is it possible to register an appointment in several Outlook accounts since vb?

I am using the following code but I only registered the appointment in the main mail account, I would like to know it is possible to register the direct appointment in another Outlook account:
.Recipients.Add("Roger Harui")
Dim sentTo As Outlook.Recipients = .Recipients
Dim sentInvite As Outlook.Recipient
sentInvite = sentTo.Add("Holly Holt")
sentInvite.Type = Outlook.OlMeetingRecipientType.olRequired
The Send method sends an item using the default account specified for the session.
Private Sub CreateMeeting()
Dim appt As Outlook.AppointmentItem = _
CType(Application.CreateItem( _
Outlook.OlItemType.olAppointmentItem), Outlook.AppointmentItem)
appt.Subject = "Customer Review"
appt.MeetingStatus = Outlook.OlMeetingStatus.olMeeting
appt.Location = "36/2021"
appt.Start = DateTime.Parse("19/04/2017 10:00 AM")
appt.End = DateTime.Parse("19/04/2017 11:00 AM")
Dim recipRequired As Outlook.Recipient = _
appt.Recipients.Add("Ryan Gregg")
recipRequired.Type = _
Outlook.OlMeetingRecipientType.olRequired
Dim recipOptional As Outlook.Recipient = _
appt.Recipients.Add("Peter Allenspach")
recipOptional.Type = _
Outlook.OlMeetingRecipientType.olOptional
Dim recipConf As Outlook.Recipient = _
appt.Recipients.Add("Conf Room 36/2021 (14) AV")
recipConf.Type = _
Outlook.OlMeetingRecipientType.olResource
appt.Recipients.ResolveAll()
appt.Send()
End Sub
See How to: Specify Different Recipient Types for an Appointment Item for more information.

function to delete folders older than 10 days in vb

I want to create a function which should delete all subfolders of a folder which are 10 days older.
Shell script to delete directories older than n days
I want show all the folder and count how much old and delete if it is 10 days old.
enter code here
Private Function test(ByVal directory As String) As String()
Dim fi As New IO.DirectoryInfo(directory)
Dim path() As String = {}
For Each subfolder As IO.DirectoryInfo In fi.GetDirectories()
Array.Resize(path, path.Length + 1)
path(path.Length - 1) = subfolder.FullName
For Each s As String In test(subfolder.FullName)
Array.Resize(path, path.Length + 1)
path(path.Length - 1) = s
Dim w = IO.Path.GetFileName(s)
'' ListBox1.Items.Add(w)
Dim iDate As String = w
Dim oDate As DateTime = Convert.ToDateTime(iDate)
''MsgBox(oDate.Day & " " & oDate.Month & " " & oDate.Year)
DateTimePicker1.Value = DateTime.Today
Dim date2 As Date = oDate
Dim span = DateTimePicker1.Value - date2
Dim days As Double = span.TotalDays
'' MsgBox(days)
'' ListBox1.Items.Add(days)
Next
Next
this part is not working
If days > 10 Then
fi.Delete()
End If
Iterate through the directory, get each folder's properties, and get the TimeSpan difference from today to the folder's creation date.
Try
Dim dtCreated As DateTime
Dim dtToday As DateTime = Today.Date
Dim diObj As DirectoryInfo
Dim ts As TimeSpan
Dim lstDirsToDelete As New List(Of String)
For Each sSubDir As String In Directory.GetDirectories(sDirectory)
diObj = New DirectoryInfo(sSubDir)
dtCreated = diObj.CreationTime
ts = dtToday - dtCreated
'Add whatever storing you want here for all folders...
If ts.Days > 10 Then
lstDirsToDelete.Add(sSubDir)
'Store whatever values you want here... like how old the folder is
diObj.Delete(True) 'True for recursive deleting
End If
Next
Catch ex As Exception
MessageBox.Show(ex.Message, "Error Deleting Folder", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try

Access another Inbox which is not mine Outlook Addin

How would I get a folder that I, as a user, have been added to.
I need to do an addin for work, how would I access an inbox which isn't mine?
So the top one is my personal inbox, I need to access the inbox within 'MIS'.
Private Sub ThisApplication_NewMail() Handles Application.NewMail
Dim myNameSpace = Application.GetNamespace("MAPI")
Dim oParentFolder = myNameSpace.Folders("MIS")
Dim mis = oParentFolder.Folders.Item("Inbox")
Dim moveMail As Outlook.MailItem = Nothing
Dim mItems As Outlook.Items = mis.Items
mItems.Restrict("[Read] = true")
Dim destFolder As Outlook.MAPIFolder = mis.Folders("Test")
Dim SubjName = "TestingAddin123"
Dim sender As String = "michael"
Dim FName As String = "[Some recurring subject]"
Dim tStamp As String = Format(DateTime.Now, "ddMMyy").ToString()
Try
For Each eMail As Object In mItems
moveMail = TryCast(eMail, Outlook.MailItem)
If Not moveMail Is Nothing Then
If InStr(moveMail.SenderEmailAddress, sender) Then
If InStr(moveMail.Subject, SubjName) > 0 Then
Dim rn As New Random
Dim n = rn.Next(1, 9999)
'n()
moveMail.SaveAs("W:\NS\" & FName & "_" & tStamp & n.ToString() + ".html", Outlook.OlSaveAsType.olHTML)
moveMail.Move(destFolder)
End If
End If
End If
Next eMail
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
So I'm using the above code so far but I don't seem to be able to find the MIS Inbox.
How would I achieve this?
Try to use the Namespace.CreateRecipient / Namespace.GetSharedDefaultFolder methods.

Issues enumerating and outputting items in outlook's sent items folder

As the title says, I'm having issues with enumerating and outputting item in my sent items folder in outlook. Specifically I'm looking for sent tasks. It keeps telling me there's nothing in the folder, when there is. The code is:
Private Sub GetSentTasks(objApp As Microsoft.Office.Interop.Outlook.Application)
Dim objNS As Outlook.NameSpace = objApp.GetNamespace("MAPI")
Dim folder As Outlook.MAPIFolder = _
objNS.GetDefaultFolder( _
Outlook.OlDefaultFolders.olFolderSentMail)
Dim searchCriteria As String = "[MessageClass] = 'IPM.TaskRequest'"
Dim strBuilder As StringBuilder = Nothing
Dim counter As Integer = 0
Dim taskItem As Outlook._TaskItem = Nothing
Dim folderItems As Outlook.Items = Nothing
Dim resultItem As Object = Nothing
Dim TTDcounter As Integer = 0
Try
folderItems = folder.Items
folderItems.IncludeRecurrences = True
If (folderItems.Count > 0) Then
resultItem = folderItems.Find(searchCriteria)
If Not IsNothing(resultItem) Then
strBuilder = New StringBuilder()
Do
If (TypeOf (resultItem) Is Outlook._TaskRequestItem) Then
counter += 1
taskItem = resultItem
'If taskItem.Categories = "TTD" Then
TTDcounter += 1
Dim listarray() As String = {taskItem.Delegator, taskItem.Subject, taskItem.DueDate, stripEstComp(taskItem.Body.ToString())}
taskPaneControl3.ListView1.Items.Add(TTDcounter).SubItems.AddRange(listarray)
'End If
End If
Marshal.ReleaseComObject(resultItem)
resultItem = folderItems.FindNext()
Loop Until IsNothing(resultItem)
End If
End If
If Not IsNothing(strBuilder) Then
Debug.WriteLine(strBuilder.ToString())
Else
Debug.WriteLine("There is no match in the " + _
folder.Name + " folder.")
End If
Catch ex As Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
Finally
If Not IsNothing(folderItems) Then Marshal.ReleaseComObject(folderItems)
End Try
End Sub