I have the below in Outlook and no emails are populating on my desktop spreadsheet. This is a test. I need to have 7 mailbox folders to export to 7 worksheets in an email tracking workbook. Maybe there is something incorrect in the below. Any help would be appreciated, thank you.
Public WithEvents GMailItems As Outlook.Items
Private Sub Application_Startup()
Set GMailItems = Outlook.Application.Session.GetDefaultFolder(olFolderInbox).Items
End Sub
Private Sub GMailItems_ItemAdd(ByVal Item As Object)
Dim xMailItem As Outlook.MailItem
Dim xExcelFile As String
Dim xExcelApp As Excel.Application
Dim xWb As Excel.Workbook
Dim xWs As Excel.Worksheet
Dim xNextEmptyRow As Integer
On Error Resume Next
If Item.Class <> olMail Then Exit Sub
Set xMailItem = Item
xExcelFile = "C:\Users\mmos6\Desktop\Outlook.xlsx"
If IsWorkBookOpen(xExcelFile) = True Then
Set xExcelApp = GetObject(, "Excel.Application")
Set xWb = GetObject(xExcelFile)
If Not xWb Is Nothing Then xWb.Close True
Else
Set xExcelApp = New Excel.Application
End If
Set xWb = xExcelApp.Workbooks.Open(xExcelFile)
Set xWs = xWb.Sheets(1)
xNextEmptyRow = xWs.Range("B" & xWs.Rows.Count).End(xlUp).Row + 1
With xWs
.Cells(xNextEmptyRow, 1) = xNextEmptyRow - 1
.Cells(xNextEmptyRow, 2) = xMailItem.Received
.Cells(xNextEmptyRow, 3) = xMailItem.StartDate
.Cells(xNextEmptyRow, 4) = xMailItem.FlagCompletedDate
.Cells(xNextEmptyRow, 5) = xMailItem.Categories
.Cells(xNextEmptyRow, 6) = xMailItem.From
.Cells(xNextEmptyRow, 7) = xMailItem.To
.Cells(xNextEmptyRow, 8) = xMailItem.CC:
.Cells(xNextEmptyRow, 9) = xMailItem.Subject
End With
xWs.Columns("A:I").AutoFit
xWb.Save
End Sub
Function IsWorkBookOpen(FileName As String)
Dim xFreeFile As Long, xErrNo As Long
On Error Resume Next
xFreeFile = FreeFile()
Open FileName For Input Lock Read As #xFreeFile
Close xFreeFile
xErrNo = Err
On Error GoTo 0
Select Case xErrNo
Case 0: IsWorkBookOpen = False
Case 70: IsWorkBookOpen = True
Case Else: Error xErrNo
End Select
End Function
Bookmarks