Capturing Voting Response Values From Outlook using Excel VBA
Solution 1
Sub CollectVotesToExcel()Dim objNS As Outlook.NameSpace
Dim objInbox As Outlook.MAPIFolder
Dim objMail As Outlook.MailItem, objItem As Object
Dim objExcel As Excel.Application
Dim objWks As Excel.Worksheet, objWkb As Excel.Workbook
Dim objTimeRange As Excel.Range, objRange As Excel.Range
Dim intX As Integer
Set objNS = Application.GetNamespace("MAPI")
Set objInbox = objNS.GetDefaultFolder(olFolderInbox)
Set objExcel = New Excel.Application
objExcel.Workbooks.Open "C:\MyWorkbook.xls"
objExcel.Visible = True
Set objWks = objExcel.ActiveSheet 'Use default Sheet1
Set objTimeRange = objWks.UsedRange
For Each objItem In objInbox.Items
If objItem.Class = olMail Then
Set objMail = objItem
If objMail.VotingResponse <> "" Then
Set objRange = objTimeRange.Find(objMail.VotingResponse, , ,
xlWhole)
If Not objRange Is Nothing Then
objWks.Cells(2, objRange.Column).Value =
objMail.SenderEmailAddress
End If
End If
End If
Set objMail = Nothing
Set objItem = Nothing
Next
Set objRange = Nothing
Set objTimeRange = Nothing
Set objMail = Nothing
Set objWkb = Nothing
Set objWks = Nothing
Set objExcel = Nothing
Set objNS = Nothing
Set objInbox = Nothing
End Sub
Solution 2
Sub ExportVotingStatistics _Excel ()Dim objMail As Outlook.MailItem
Dim objRecipient As Outlook.recipient
Dim objVoteDictionary As Object
Dim varVotingCounts As Variant
Dim varVotingOptions As Variant
Dim varVotingOption As Variant
Dim i As Long
Dim objExcelApp As Excel.Application
Dim objExcelWorkbook As Excel.Workbook
Dim objExcelWorksheet As Excel.Worksheet
Dim nRow As Integer
Set objMail = Application.ActiveExplorer.Selection(1)
'Create a new excel worksheet
Set objExcelApp = CreateObject("Excel.Application")
Set objExcelWorkbook = objExcelApp.Workbooks.Add
Set objExcelWorksheet = objExcelWorkbook.Sheets(1)
'Fill in the predefined values
With objExcelWorksheet
.Cells.Font.Name = "Cambria"
.Cells(1, 1) = "Voting Results for Email:"
.Cells(1, 2) = Chr(34) & objMail.Subject & Chr(34)
.Cells(3, 1) = "Voting Options"
.Cells(3, 2) = "Voting Counts"
End With
Set objVoteDictionary = CreateObject("Scripting.Dictionary")
'get the default voting options
varVotingOptions = Split(objMail.VotingOptions, ";")
'Add the voting responses to the dictionary
For Each varVotingOption In varVotingOptions
objVoteDictionary.Add varVotingOption, 0
Next
'Add a custom voting response - "No Reply"
objVoteDictionary.Add "No Reply", 0
'Process the all voting responses
For Each objRecipient In objMail.Recipients
If objRecipient.TrackingStatus = olTrackingReplied Then
If objVoteDictionary.Exists(objRecipient.AutoResponse) Then
objVoteDictionary.Item(objRecipient.AutoResponse) = objVoteDictionary.Item(objRecipient.AutoResponse) + 1
Else
objVoteDictionary.Add objRecipient.AutoResponse, 1
End If
Else
objVoteDictionary.Item("No Reply") = objVoteDictionary.Item("No Reply") + 1
End If
Next
'Get the voting options and vote counts
varVotingOptions = objVoteDictionary.Keys
varVotingCounts = objVoteDictionary.Items
'Fill in the values in specific cells
nRow = 4
For i = LBound(varVotingOptions) To UBound(varVotingOptions)
With objExcelWorksheet
.Cells(nRow, 1) = varVotingOptions(i)
.Cells(nRow, 2) = varVotingCounts(i)
End With
nRow = nRow + 1
Next
'Save the new Excel file
objExcelWorksheet.Columns("A:B").AutoFit
strExcelFile = "E:\Voting Results " & Format(Now, "YYYY-MM-DD hh-mm-ss") & ".xlsx"
objExcelWorkbook.Close True, strExcelFile
MsgBox "Complete!", vbExclamation
End Sub
Comments
Post a Comment