Passing objects as parameter?

lolarennt

New member
Local time
Today, 07:22
Joined
Mar 12, 2009
Messages
3
Hi,

My first post here. Please be lenient...

I am trying to pass an object to a function. I am running through an outlook inbox, and I need to pass an email as an object. The line where I pass the object to the function causes Access to throw me an error: "run time error 424: object required".

I am confused. I clearly pass an object, and the VBA complains that an object is required. Can somebody shed some light on this?

Private Sub ReadInbox()
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim mailObject As Object

Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set TempRst = CurrentDb.OpenRecordset("abstracts")
Set InboxItems = Inbox.Items

For Each mailObject In InboxItems
' the next line causes the error
checkTriggerPresent (mailObject)
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set mailObject = Nothing
Set TempRst = Nothing
Set atmt = Nothing
End Sub



Function checkTriggerPresent(mailObject As Object) As String
msgbox ("It doesn't work")
End Function
 
Change this:

Dim mailObject As Object

to this:

Dim mailObject As Variant
 
Also, change this:
Code:
Function checkTriggerPresent(mailObject As Variant) As String
msgbox ("It doesn't work")
End Function

(I'm hoping that is just something you are getting started on)
 
maybe you possibly need this as well?:
Code:
set mailObject = olApp.mailItem
 
Also, change this:
Code:
Function checkTriggerPresent(mailObject As Variant) As String
msgbox ("It doesn't work")
End Function
(I'm hoping that is just something you are getting started on)

Hi Bob,

I tried the variant (changed it both in the dim statement and in the function call). It works to a certain degree. The function now actually gets called, but I hit a snag a bit later. Here is my code so far:

Private Sub ReadInbox()
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim mailObject As variant

Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set TempRst = CurrentDb.OpenRecordset("abstracts")
Set InboxItems = Inbox.Items

For Each mailObject In InboxItems
' check if the mailobject contains an abstract
checkTriggerPresent (mailObject)
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set mailObject = Nothing
Set TempRst = Nothing
End Sub



Function checkTriggerPresent(mailObject As variant) As String
For Each atmt In mailObject.Attachments
FileName = "C:\temp\" & atmt.FileName
atmt.SaveAsFile FileName
End If
Next atmt
End Function

Now, when I get in the loop (for each atmt in mailobject.attachments), I get the same error: object required.

I also tried the other suggestion (defining mailobject as outlook.mailitem), and this gives me my original error (object required when calling the function, I don't even get to go in the function).

All this is in acces 2007 if that makes any difference.

Thanks for your help. This is driving me nuts. I truly don't understand why access/vba complains that an object is required when I clearly pass an object to begin with...
 
You have to declare atmt so like:

Code:
Function checkTriggerPresent(mailObject As variant) As String
[COLOR="Red"]Dim atmt As Variant[/COLOR]

   For Each atmt In mailObject.Attachments
       FileName = "C:\temp\" & atmt.FileName
       atmt.SaveAsFile FileName
   End If
Next atmt
End Function
Normally, whenever you do a "For each ITEM in SOMETHING" if they are objects declaring as a variant seems to make it work in places where specific declaration doesn't work and in this case you haven't declared it at all.
 
Hi Bob,

Thanks for your help. My code now reads (only change is the dim statement you suggested - stupid of me that I did not have that there...)

Private Sub ReadInbox()
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim mailObject As variant

Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set TempRst = CurrentDb.OpenRecordset("abstracts")
Set InboxItems = Inbox.Items

For Each mailObject In InboxItems
' check if the mailobject contains an abstract
checkTriggerPresent (mailObject)
Next

Set OlApp = Nothing
Set Inbox = Nothing
Set InboxItems = Nothing
Set mailObject = Nothing
End Sub



Function checkTriggerPresent(mailObject As variant) As String
dim atmt as variant
For Each atmt In mailObject.Attachments
FileName = "C:\temp\" & atmt.FileName
atmt.SaveAsFile FileName
End If
Next atmt
End Function

Unfortunately it still doesn't work. Things appear to go wrong when I call the function (highlighted in red).

When I do a watch on mailobject, I notice the following:
1. In my main sub, the watch shows the + sign, so when I expand that, it has all the properties of a mailitem
2. When I watch mailobject in my function (after it has been called), it loses the + sign, so it lost all the properties.

So, when I then try to loop through the attachments, it can't find them, and access throws the "object required" error (at the line highlighted in blue).

Any suggestions? This really shouldn't be that hard, but I can't figure it out...
 
As I don't use the mail objects like that I'm not sure what to say. Perhaps if you declared mailObject as a Public Variable instead of private and instead of passing it to the function it was just available to both. I don't know for sure.
 
Remove the brackets.
i.e.
checkTriggerPresent mailObject

You then avoid the attempted coersion into ByVal.
 
But why? (I don’t hear anyone ask. ;) )

Obviously I can’t know the real reasons why it should be that way but parenthesis (brackets) imply order of evaluation. So it might seem natural that the brackets try to force an evaluation of the object prior to it being passed. From a programming point of view, the evaluation of an object may not be reasonable or even possible. So an error should be raised when trying to force the evaluation to ByVal.

That’s my story and I’m sticking to it until another better explanation comes along. :)
 

Users who are viewing this thread

Back
Top Bottom