Microsoft Excel - Trying with VBA to extract information from outlook then create a calendar including only

Asked By Stephen P on 25-Jun-13 11:34 PM
I am trying to extract information from outlook, was able to do that with the following

 
Sub ListAppointments()
  
    Dim olApp As Object
    Dim olNS As Object
    Dim olFolder As Object
    Dim olApt As Object
    Dim NextRow As Long
  
    Set olApp = CreateObject("Outlook.Application")
  
    Set olNS = olApp.GetNamespace("MAPI")
  
    Set olFolder = olNS.GetDefaultFolder(9) 'olFolderCalendar
  
    Range("A1:b1").Value = Array("Start", "Subject", "Location")
  
  
    NextRow = 2
  
    For Each olApt In olFolder.Items
      Cells(NextRow, "a").Value = olApt.Start
      With Selection
      .NumberFormat = "MMMM dd"
      End With
      Cells(NextRow, "b").Value = olApt.Subject
     
     
      'Cells(NextRow, "C").Value = olApt.End
     
      NextRow = NextRow + 1
    Next olApt
  


I am also able to create a calendar using the following

Sub CreateCalendar()

Dim lMonth As Long

Dim strMonth As String

Dim rStart As Range

Dim strAddress As String

Dim rCell As Range

Dim lDays As Long

Dim dDate As Date

'Add new sheet and format

Worksheets.Add

ActiveWindow.DisplayGridlines = False

With Cells

.ColumnWidth = 6#

.Font.Size = 12

End With


'Create the Month headings

For lMonth = 1 To 4

Select Case lMonth

Case 1

strMonth = "January"

Set rStart = Range("A1")

Case 2

strMonth = "April"

Set rStart = Range("A8")

Case 3

strMonth = "July"

Set rStart = Range("A15")

Case 4

strMonth = "October"

Set rStart = Range("A22")

End Select

'Merge, AutoFill and align months

With rStart

.Value = strMonth

.HorizontalAlignment = xlCenter

.Interior.ColorIndex = 6

.Font.Bold = True

With .Range("A1:G1")

.Merge

.BorderAround LineStyle:=xlContinuous

End With

.Range("A1:G1").AutoFill Destination:=.Range("A1:U1")

End With

Next lMonth

'Pass ranges for months

For lMonth = 1 To 12

strAddress = Choose(lMonth, "A2:G7", "H2:N7", "O2:U7", _
"A9:G14", "H9:N14", "O9:U14", _
"A16:G21", "H16:N21", "O16:U21", _
"A23:G28", "H23:N28", "O23:U28")

lDays = 0

Range(strAddress).BorderAround LineStyle:=xlContinuous

'Add dates to month range and format

For Each rCell In Range(strAddress)

lDays = lDays + 1

dDate = DateSerial(Year(Date), lMonth, lDays)

If Month(dDate) = lMonth Then ' It's a valid date

With rCell

.Value = dDate

.NumberFormat = "ddd dd"
if (exists InStr(1, "Sat", "Sun") = 1) then rCell.Value = ""

GoTo 20

End With

End If
20
Next rCell

Next lMonth

'add con formatting

With Range("A1:U28")

.FormatConditions.Add Type:=xlCellValue, Operator:=xlEqual, Formula1:="=TODAY()"

.FormatConditions(1).Font.ColorIndex = 2

.FormatConditions(1).Interior.ColorIndex = 1

End With


Here is what I am looking to accomplish for a rolling 60 day period.


July
01 02 03 04 05
08 sample appt 09 10 another appt 11 12
15 16 appt made 17 18 19
22 23 24 25 26
29 30 31


Can anyone please help

Harry Boughen replied to Stephen P on 26-Jun-13 06:41 AM
Hello Stephen,
I'm thinking on my feet here but I would envisage that first you determine the range of your sixty day period eg TODAY() to TODAY()+60.
Then determine the cell for the first day in that range. For that day check the appointment end column to find the first post date, check the appointment start column and if the date is included append the appointment text to the day number.  Then check the next and subsequent appointment start column cells and if the date is included append the text.  If not, proceed to the next next date in the rolling range and repeat. 
Hope this helps.  If you want I could try to write some pseudo-code to help out but it could take a little while to get around to it.
Regards
Harry
Harry Boughen replied to Stephen P on 27-Jun-13 05:14 AM
Hello Stephen,
Have a look at the attached file.  I haven't tested all possibilities but it works OK for single day events.  Select Sheet3 and run the appointment macro.
diary_test.zip
Let us know how you go.
Harry
Stephen P replied to Harry Boughen on 27-Jun-13 11:02 AM
Thank you Harry, that gives me a great starting point, so I will work with it and tweak it, concept is on the money thouugh (I know there will be some possibilities for bugs)
Harry Boughen replied to Stephen P on 27-Jun-13 05:03 PM
Hi Stephen,
Forgot to mention that for the dynamic ranging to work the data has to be sorted ascending on the end date column.  I wasn't sure how long a record you would be pulling but if it is not too long the overhead of searching the whole list would not be too great.  If you persist with the dynamic ranging, the start date does not have to be on a separate sheet.
Good luck with your project.
Harry
Stephen P replied to Harry Boughen on 27-Jun-13 06:41 PM
Have been trying to toy with it today, when I realized the end date is necessary, so I'll try to tweak it so that it is only the Start date, also trying to tweak it so that I can put more than one event on a particular day
Harry Boughen replied to Stephen P on 27-Jun-13 07:06 PM
Hi Stephen,
More than one event on a day does work.  At the moment they are separated only by a space in the string but you could put a line feed if you wanted to.
I thought you original post indicated that you had both the start and end date and that made it easier for events that crossed multiple days.
Regards
Harry
Stephen P replied to Harry Boughen on 27-Jun-13 08:21 PM
yes I tried multiple events, now need to figure out how to put a line feed in I tried using chr(10)
each event does not cross days, so I'll need to remove all references to enddate or add a column which includes the end date
Harry Boughen replied to Stephen P on 27-Jun-13 08:26 PM
Hi Stephen,
Replace the " " in the concatenation with vbLf.
Regards
Harry
Stephen P replied to Harry Boughen on 28-Jun-13 05:22 PM
Harry, I seem to semi run into a brick wall while trying to tweak this code to work with what I am doing.

I realize that you have a named range in, but for some reason the range does not work once I have the data extracted from outlook, which is a bit confusing since the formula for the offset seems to be the same. Then trying to elminate the endrange does not seem to work, as it then shows 1 day and then runs into an error.

Thoughts?
Harry Boughen replied to Stephen P on 29-Jun-13 02:49 AM
Hi Stephen,
I have modified by test file to work from the startdate column.  Also added some cell formatting to tidy up the look of it.  No signs of any problems.
diary_test_a.zip
The only thing I can think of could be that the data is imported as strings or is not in ascending order.  So try running a DATEVALUE function over it and see what happens.  Then try sorting it.
Regards
Harry
Stephen P replied to Harry Boughen on 29-Jun-13 02:17 PM
ok Harry, one step closer I have figured that once the named range regardless of start or end date, if data is altered the data needs to be resorted prior to the macro, so I'll add a step to sort prior to setting the range let me tinker a bit more otherwise, excellent input you should get a greater score on this
Stephen P replied to Harry Boughen on 29-Jun-13 02:56 PM
ok making definite progress here. Have information populating, seem to have lost the ability to show multiple appointments on same day, going to try assigning separate times if it is the same time ( I think that should solve the issue of overwriting, or maybe ammend may be better?)
Harry Boughen replied to Stephen P on 29-Jun-13 06:21 PM
Hi Stephen,
Good to see you are making progress.  I would have to see some of your actual data (desensitised if necessary) but it might be only a little tweak that is necessary.
Regards
Harry