Converting email count vba to Excel table

hamezz123

New Member
Joined
Jan 13, 2020
Messages
6
Office Version
  1. 365
Platform
  1. Windows
I am trying to convert this VBA which counts the catagories in outlook and gives it back as a text box but i want to transfer this data to an excel table so it is useable as i have to do it manually at the moment

Sub CategoriesEmails()

Dim oFolder As MAPIFolder
Dim oDict As Object
Dim sStartDate As String
Dim sEndDate As String
Dim oItems As Outlook.Items
Dim sStr As String
Dim sMsg As String


On Error Resume Next
Set oFolder = Application.ActiveExplorer.CurrentFolder

Set oDict = CreateObject("Scripting.Dictionary")

sStartDate = "08/20/2009" 'InputBox("Type the start date (format MM/DD/YYYY)")
sEndDate = "02/09/2020" 'InputBox("Type the end date (format MM/DD/YYYY)")

Set oItems = oFolder.Items.Restrict("[Received] >= '" & sStartDate & "' And [Received] <= '" & sEndDate & "'")
oItems.SetColumns ("Categories")

For Each aitem In oItems
sStr = aitem.Categories
If Not oDict.Exists(sStr) Then
oDict(sStr) = 0
End If
oDict(sStr) = CLng(oDict(sStr)) + 1
Next aitem

sMsg = ""
For Each aKey In oDict.Keys
sMsg = sMsg & aKey & ": " & oDict(aKey) & vbCrLf
Next
MsgBox sMsg

Set oFolder = Nothing

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Hi Hamezz,
replace the sMsg block with something like this:

VBA Code:
Set Sht = Worksheets("Sheet1")
Rw = 2
For Each aKey In oDict.Keys
    Sht.Range("A" & Rw).Value = aKey
    Sht.Range("B" & Rw).Value = oDict(aKey)
Next
Cheers,
Koen
 
Upvote 0
I have gotten to this stage so far I see that excel is doing something but i don't know what and cant find it
an Excel sheet appears and then disappears and i cant see any location for it

VBA Code:
 Sub CategoriesEmails()

    Dim oFolder As MAPIFolder
    Dim oDict As Object
    Dim sStartDate As String
    Dim sEndDate As String
    Dim oItems As Outlook.Items
    Dim sStr As String
    Dim sMsg As String
    Dim strFldr As String
    Dim OutMail As Object
    Dim xlApp As Object

    On Error Resume Next
    Set oFolder = Application.ActiveExplorer.CurrentFolder
    
    oItems.SetColumns ("Categories")
    
    Set oDict = CreateObject("Scripting.Dictionary")

    sStartDate = "08/20/2019"
    sEndDate = "02/09/2020"
    
    Set oItems = oFolder.Items.Restrict("[Received] >= '" & sStartDate & "' And [Received] <= '" & sEndDate & "'")
    oItems.SetColumns ("Categories")

    For Each aItem In oItems
    sStr = aItem.Categories
    If Not oDict.Exists(sStr) Then
    oDict(sStr) = 0
    End If
    oDict(sStr) = CLng(oDict(sStr)) + 1
    Next aItem

    sMsg = ""
    i = 0

    strFldr = "C:\"
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Application.Visible = True
    xlApp.Workbooks.Open strFldr & "TestingCatagories.xlsx"
    xlApp.Sheets("Sheet1").Select
    For Each aKey In oDict.Keys
    xlApp.Range("A1").Offset(i, 0).Value = sMsg & aKey
    xlApp.Range("B1").Offset(i, 0).Value = oDict(aKey) & vbCrLf
    i = i + 1
    Next
    xlApp.Save

    Set oFolder = Nothing

     End Sub

Any idea how to fix?
 
Upvote 0
The answer is in your code:
VBA Code:
strFldr = "C:\"  'the location where the result will be stored
xlApp.Workbooks.Open strFldr & "TestingCatagories.xlsx" 'This is the location and the file name
xlApp.Save 'This saves the file
Cheers,
Koen
 
Upvote 0
So should this work or am i missing spots? excel appears and then disappears and nothing is saved?
 
Upvote 0
There should be a result in C:\TestingCatagories.xlsx . If not, try changing the directory to a location you can access (C:\ might be protected), that could help. Try e.g. this updated code:

VBA Code:
    strFldr = "C:\"
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Application.Visible = True
    'xlApp.Workbooks.Open strFldr & "TestingCatagories.xlsx"  -> this probably fails, as the file doesn't exist, try creating a new file instead

    Set xlWB = xlApp.Workbooks.Add
    Set xlWS = xlWB.Worksheets(1)  'references to the first worksheet, name doesn't matter
    'xlApp.Sheets("Sheet1").Select -> not needed to select
    For Each aKey In oDict.Keys
        xlWS.Range("A1").Offset(i, 0).Value = sMsg & aKey
        xlWS.Range("B1").Offset(i, 0).Value = oDict(aKey) & vbCrLf
        i = i + 1
    Next
    xlWB.SaveAs fullFilePath, AccessMode:=xlExclusive,ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges    'This might fail if a file with that name is present, not tested
    'xlWB.Close (True)
    'File should stay open...
 
Upvote 0

Forum statistics

Threads
1,223,882
Messages
6,175,164
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top