Hi There I need help please with the below code.It automatically downloads information from my outlook account but I need to get it to just automatically download the said info for a specific folder called PIA, as and when new emails arrived.
Any help please to change the code to only download the info from the PIA folder in my outlook account, please? Thanks, Willem
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\DT168\Desktop\split document\kto-data.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.SenderName
.Cells(xNextEmptyRow, 3) = xMailItem.SenderEmailAddress
.Cells(xNextEmptyRow, 4) = xMailItem.Subject
.Cells(xNextEmptyRow, 5) = xMailItem.ReceivedTime
End With
xWs.Columns("A:E").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
Any help please to change the code to only download the info from the PIA folder in my outlook account, please? Thanks, Willem
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\DT168\Desktop\split document\kto-data.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.SenderName
.Cells(xNextEmptyRow, 3) = xMailItem.SenderEmailAddress
.Cells(xNextEmptyRow, 4) = xMailItem.Subject
.Cells(xNextEmptyRow, 5) = xMailItem.ReceivedTime
End With
xWs.Columns("A:E").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