VBA code not running the 2nd time

Jacki

New Member
Joined
Jul 27, 2014
Messages
17
Hi Folks,

I am having some issues with the below code. I think it has to do with object reference, but I am not sure how to fix it. Really appreciate any help!

Code:
Sub ExportToExcel(strFilename As String, strFolderPath As String)
Dim olkMsg As Object
Dim olkFld As Object
Dim excApp As Object
Dim excWkb As Object
Dim excWks As Object
Dim intRow As Integer
Dim intVersion As Integer

If strFilename <> "" Then
  If strFolderPath <> "" Then
    Set olkFld = OpenOutlookFolder(strFolderPath)
    If TypeName(olkFld) <> "Nothing" Then
      intVersion = GetOutlookVersion()
      Set excApp = CreateObject("Excel.Application")
      Set excWkb = excApp.Workbooks.Add()
      Set excWks = excWkb.ActiveSheet

      'Write Excel Column Headers
      With excWks
        .Cells(1, 1) = "Subject"
        .Cells(1, 2) = "Message"
        .Cells(1, 3) = "Received"
        .Cells(1, 4) = "Sender"
      End With

      intRow = 2
      For Each olkMsg In olkFld.Items
        If olkMsg.UnRead = True Then
          'Only export messages, not receipts or appointment requests, etc.
          If olkMsg.Class = olMail Then
            'Add a row for each field in the message you want to export
            excWks.Cells(intRow, 1) = olkMsg.Subject
            excWks.Cells(intRow, 2) = olkMsg.Body
            excWks.Cells(intRow, 3) = olkMsg.ReceivedTime
            excWks.Cells(intRow, 4) = GetSMTPAddress(olkMsg, intVersion)
            intRow = intRow + 1
            Range("A1:D1000").WrapText = True
          End If
        End If
      Next
      Set olkMsg = Nothing
      excWkb.SaveAs strFilename
      excWkb.Close
      Else
        MsgBox "The folder '" & strFolderPath & "' does not exist in Outlook.", vbCritical + vbOKOnly, MACRO_NAME
      End If
    Else
      MsgBox "The folder path was empty.", vbCritical + vbOKOnly, MACRO_NAME
    End If
  Else
    MsgBox "The filename was empty.", vbCritical + vbOKOnly, MACRO_NAME
End If
 
Set olkMsg = Nothing
Set olkFld = Nothing
Set excWks = Nothing
Set excWkb = Nothing
Set excApp = Nothing
Call Macro2
End Sub
 
Last edited by a moderator:

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
Sorry, I forgot to mention what the problem is. When I first open outlook the macro runs fine. If I need to run the macro again it doesn't run. I don't get any error message.. it just doesn't run. I can fix it by closing and re-opening outlook, but that is not ideal.
 
Upvote 0

Forum statistics

Threads
1,223,240
Messages
6,170,951
Members
452,368
Latest member
jayp2104

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