title: Can I add "export to excel" to an existing VBA for Outlook?
body: <p>I have been using the code below to calculate the total hours spent between 2 dates by category.  It works perfectly, and now I'm looking for a way to not only run this code but also export the data collected to a specific excel worksheet.  Is there a simple addition to the code below, or do I have to have a completely different sub?</p>
  <pre><code>Sub TotalCategories()
  
  Dim app As New Outlook.Application
  Dim namespace As Outlook.namespace
  Dim calendar As Outlook.Folder
  Dim appt As Outlook.AppointmentItem
  Dim apptList As Outlook.Items
  Dim apptListFiltered As Outlook.Items
  Dim explorer As Outlook.explorer
  Dim view As Outlook.view
  Dim calView As Outlook.CalendarView
  Dim startDate As String
  Dim endDate As String
  Dim category As String
  Dim duration As Integer
  Dim outMsg As String
  
  ' Access appointment list
  Set namespace = app.GetNamespace(&quot;MAPI&quot;)
  Set calendar = namespace.GetDefaultFolder(olFolderCalendar)
  Set apptList = calendar.Items
  
  ' Include recurring appointments and sort the list
  apptList.IncludeRecurrences = True
  apptList.Sort &quot;[Start]&quot;
  
  ' Get selected date
  Set explorer = app.ActiveExplorer()
  Dim dte As String
   startDate = InputBox(&quot;Please Enter Start Date: &quot;, Default:=Format(Now, &quot;mm/dd/yyyy&quot;))
   endDate = InputBox(&quot;Please Enter End Date: &quot;, Default:=Format(Now, &quot;mm/dd/yyyy&quot;))
  
  ' Filter the appointment list
  strFilter = &quot;[Start] &gt;= '&quot; &amp; startDate &amp; &quot;'&quot; &amp; &quot; AND [End] &lt;= '&quot; &amp; endDate &amp; &quot;'&quot;
  Set apptListFiltered = apptList.Restrict(strFilter)
  
  ' Loop through the appointments and total for each category
  Set catHours = CreateObject(&quot;Scripting.Dictionary&quot;)
  For Each appt In apptListFiltered
      category = appt.Categories
      duration = appt.duration
      If catHours.Exists(category) Then
          catHours(category) = catHours(category) + duration
      Else
          catHours.Add category, duration
      End If
  Next
  
  ' Loop through the categories
  keyArray = catHours.Keys
  For Each Key In keyArray
      outMsg = outMsg &amp; Key &amp; &quot;: &quot; &amp; (catHours(Key) / 60) &amp; vbCrLf &amp; vbCrLf
  Next
  
  ' Display final message
  MsgBox outMsg, , &quot;Category Totals&quot;
  
  ' Clean up objects
  Set app = Nothing
  Set namespace = Nothing
  Set calendar = Nothing
  Set appt = Nothing
  Set apptList = Nothing
  Set apptListFiltered = Nothing
  Set explorer = Nothing
  Set view = Nothing
  Set calView = Nothing
  
  
  End Sub
  </code></pre>