Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Application-defined or object-defined error 287

Status
Not open for further replies.

1DMF

Programmer
Jan 18, 2005
8,795
GB
Hi,

We are having the occassional error as per the subject line when the following code runs...
Code:
Function SendEmail(ByVal sTo As String, ByVal sSubject As String, ByVal sBody As String, Optional sFile As String, Optional sBlind As String)

    On Error GoTo EH_SendEmail
    
    Dim oApp As Outlook.Application
    Dim sEmail As String
    Dim iPos As Integer
    Dim i As Integer
    Dim iCount As Integer
    Dim oMailItem As Outlook.MailItem
    Dim oReceipt As Outlook.Recipient
    Dim oAttach As Outlook.Attachment '- use to attach a file
    Dim vEmail As Variant
    
    SendEmail = True
    
    'open Outlook
    Set oApp = CreateObject("Outlook.Application")
       
    'Create mail message
    Set oMailItem = oApp.CreateItem(olMailItem)
    
    'set email recipient
    vEmail = Split(sTo, ";")
        
    With oMailItem


        For i = 0 To UBound(vEmail)
            Set oReceipt = .Recipients.Add(vEmail(i))
            oReceipt.Type = olTo
        Next i

        .Subject = sSubject
        ' .Body = sBody ' changed to be HTML email
        .HTMLBody = sBody
        'if Blind Copy needed
        If Not IsNull(sBlind) And sBlind <> "" Then
            .BCC = sBlind
        End If
         'use to attach a file at sFile
        If Not IsNull(sFile) And sFile <> "" Then
            Set oAttach = .Attachments.Add(sFile)
        End If
        'send to outlook outbox
        .Send
    End With
        
    Set oReceipt = Nothing
    Set oAttach = Nothing
    Set oMailItem = Nothing
        
    Set oApp = Nothing

Exit_SendEmail:
    Exit Function
    
EH_SendEmail:
    MsgBox "Error in EH_SendEmail " & Error(Err) & " " & CStr(Err) & vbCrLf & "Recipient = " & sTo & vbCrLf & "Subject = " & sSubject & vbCrLf & "Content = " & sBody
    SendEmail = False
    Resume Exit_SendEmail

    
End Function

Now this code has run fine for years and not changed at all, but now on occassion it is erroring.

I've done the 'Google is my friend' and every thread I can find implies it is because the user clicks 'No' when the security warning comes up.

Well that can't be possible, we have a program installed called 'Express Click Yes', which automatically clicks 'Yes' , the user cannot click 'No' and the program only ever clicks 'Yes'.

Therefore what on earth is causing the error and making the function behave as if 'No' has been clicked, which isn't the case?

Your advice is appreciated.

Cheers,
1DMF.

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
If you are using a recent version of Outlook check your trust center settings; try enabling all macros.

Beir bua agus beannacht!
 
Nope this is still the same version we have always had, MSO2003.

If it was just he boss's new PC with MSO2007 I could understand there may be an issue.

Also would MSAccess VBA need outlook macro's enabling?

It's not an Outlook macro it's VBA code using the Ofice intergration application object.

I'll go check the outlook settings just in case they have changed something, but then I would expect it to error all the time, not sparodically.

It's not like it's a duff email address either as the recipient is hardcoded and it's an internal mailbox.







"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Also would MSAccess VBA need outlook macro's enabling?

Can't hurt to try. I also had a similar problem where I had to put the app in a trusted folder, and digitally signed it to boot. If you don't have a certificate, you can create one using selfcert.exe which should be included in MSO2K7.

Beir bua agus beannacht!
 
hmm, it might be worth me looking into that anyway.

Can I create a digitally signed .mde with MSO2003?

As It's an inhouse developed DB application, I might as well do what i can to make it 'authorised'.

Can you point me in the right direction?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Thanks but I can't follow those instructions.

The page goes to MSM Access for MSO2003 , but then you click the link at the bottom for 'Using digital certificates to produce trusted solutions' and it takes you to which is for MSO2000 and the option
4. Expand Office Tools and set Digital Signature for VBA Projects to Run from My Computer.
doesn't exist. so I'm unable to continue?

Do you know how to do this under MSO2003?

"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
We don't use 2003; we have some old ACC2000 mdb's, and lots of 2007. You run selfcert.exe which places a certificate in the root folder. Then go to ControlPanel|Internet Options|Internet Properties|Content|Certificates and import the certificate into the Trusted Publishers store. Once that is completed you need to go to the VBA IDE and click on Tools|Digital Signature and choose the cert that you just created (at least in 2007 - there must be something similar in 2003). Also go to the trust center and either add the folder your app is in to trusted locations, or put the app in one that is already trusted. You can do a Windows search for selfcert.exe to determine its location.

Beir bua agus beannacht!
 
Thanks, I'll have a butcher's when I have some time to investigate further.

For the time being I changed the error code to
Code:
resume next

So at least the user won't get annoyed with messages.

What's really odd is when you ok the msgbox, the email still gets sent.



"In complete darkness we are all the same, only our knowledge and wisdom separates us, don't let your eyes deceive you."

"If a shortcut was meant to be easy, it wouldn't be a shortcut, it would be the way!"

Google Rank Extractor -> Perl beta with FusionCharts
 
Cool. Just one more thing: if you use selfcert it is only good on the PC that creted the certificate. If you are going to distribute, and decide to digitally sign your code, look into purchasing a cert from VeriSign or someone, unless your shop has one available to use.

Beir bua agus beannacht!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top