Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88029

Implementing an outlook macro in excel

$
0
0

I have the following macro in Outlook, and would like to use it frm Excel, how can I rewrite it that it will work from excel without an outlook macro being included?

Dim objExcelApp As Excel.Application
Dim objExcelWorkbook As Excel.Workbook
Dim objExcelWorksheet As Excel.Worksheet

Public Sub ExportAllFlaggedEmailsToExcel()

Dim objOutlookFile As Outlook.Folder
Dim objFolder As Outlook.Folder
Dim objNameSpace As NameSpace
Dim mailboxowner As Outlook.Recipient
Dim Shared_email_address As Folder
Dim outlookAPP As Outlook.Application

Set outlookAPP = Outlook.Application
Set objOutlookFile = Outlook.Application.Session.PickFolder
Set objNameSpace = Application.GetNamespace("MAPI")

'If Not (objOutlookFile Is Nothing) Then
   'Create a new Excel file
   Set objExcelApp = CreateObject("Excel.Application")
   Set objExcelWorkbook = objExcelApp.Workbooks.Add
   Set objExcelWorksheet = objExcelWorkbook.Sheets("sheet1")
   objExcelApp.Visible = True

    'Name_of_the_excel_file_created_by_the_vba = ActiveWorkbook.Name


    'Name_of_the_excel_file_created_by_the_vba.Select
   With objExcelWorksheet
       .Cells(1, 1) = "Subject"
       .Cells(1, 1).Font.Bold = True
       .Cells(1, 2) = "Email was sent On"
       .Cells(1, 2).Font.Bold = True
       .Cells(1, 3) = "From"
       .Cells(1, 3).Font.Bold = True
       .Cells(1, 4) = "To"
       .Cells(1, 4).Font.Bold = True
       .Cells(1, 5) = "Categroy"
       .Cells(1, 5).Font.Bold = True
  End With

  For Each objFolder In objOutlookFile.Folders
      If objFolder.DefaultItemType = olMailItem Then
         Call ProcessMailFolders(objFolder)
      End If
  Next

  objExcelWorksheet.Columns("A:F").AutoFit

  MsgBox "Completed!", vbInformation + vbOKOnly, "Export Emails"'End If
End Sub

Public Sub ProcessMailFolders(ByVal objCurrentFolder As Outlook.Folder)
Dim i As Long
Dim objMail As Outlook.MailItem
Dim objFlaggedMail As Outlook.MailItem
Dim nLastRow As Integer
Dim objSubfolder As Outlook.Folder
'***********************
'Outlook to export categorised emails to excel
 '***********************
 amount_of_emails = objCurrentFolder.Items.Count
 For i = 1 To objCurrentFolder.Items.Count
    If objCurrentFolder.Items(i).Class = olMail Then
       'Export the information of each flagged email to Excel
       Set objMail = objCurrentFolder.Items(i)
       On Error Resume Next
       If objMail.Categories = "Category_Name" Then
          Set objFlaggedMail = objMail

          With objExcelWorksheet
               nLastRow = .Range("A"& .Rows.Count).End(xlUp).Row + 1
               .Range("A"& nLastRow) = objFlaggedMail.Subject
               .Range("B"& nLastRow) = objFlaggedMail.SentOn
               '.Range("C"& nLastRow) = objFlaggedMail.ReceivedTime
               .Range("C"& nLastRow) = objFlaggedMail.SenderName
               .Range("D"& nLastRow) = objFlaggedMail.To
               .Range("E"& nLastRow) = "Category_Name"

         End With
      End If
    End If
Next i

If objCurrentFolder.Folders.Count > 0 Then
   For Each objSubfolder In objCurrentFolder.Folders
       Call ProcessMailFolders(objSubfolder)
   Next
End If
end sub

I know that it is not supported to call an outlook function/macro from excel, so therefore I would like to implement this on excel level, how could I start it?


Viewing all articles
Browse latest Browse all 88029

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>