0

I have a macro in Outlook named "DeleteMail", when I try to run it from excel somehow I get an error "438".

I think I'm missing something on before the last line.

Function OpenOL(Optional ProfileName) As Object

Dim objOL As Object

     'On Error Resume Next
     Set objOL = GetObject(, "Outlook.Application")
     
     If objOL Is Nothing Then
             Set objOL = CreateObject("Outlook.Application")
             objOL.Session.Logon ProfileName, , False, True
     End If
     
     Set OpenOL = objOL
        Call OpenOL.DeleteMail      '***Error Here****
     Set objOL = Nothing

End Function

Any help is appreciated.

Nick.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • Does this answer your question? [Call outlook VBA from Excel](https://stackoverflow.com/questions/5096353/call-outlook-vba-from-excel) – Foxfire And Burns And Burns Jul 26 '22 at 10:45
  • Hi PEH, I read that post too, it is similar to the one I have, but it it didn't help. – I Forte Si Daja Jul 26 '22 at 11:39
  • Moving the `DeleteMail` code into Excel as in your answer is the suggested answer here https://stackoverflow.com/questions/51893262/vba-call-outlook-macro-from-excel-object-does-not-support-property-or-method – niton Jul 27 '22 at 01:22

1 Answers1

0

Apparently I was overthinking it.

I just had to put the first macro "DeleteMail" in Excel and run it from there instead of keeping it in Outlook and trying to run it from Excel.

Here was the macro, hope it helps someone.

Public Sub DeleteMail() ' Deletes Mail from a Folder on same level as Default Folders

Dim outApp As Outlook.Application
Dim delFolder As Outlook.MAPIFolder
Dim item As Object
Dim entryID As String

Set outApp = CreateObject("outlook.application")
'Set delFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderJunk)' deletes Junk Mails
Set delFolder = outApp.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders("Meeting")

For i = delFolder.Items.Count To 1 Step -1
      delFolder.Items(i).Delete             
Next

Set item = Nothing
Set delFolder = Nothing
Set outApp = Nothing

End Sub