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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

code runs in access 2003 but not in 2010 4

Status
Not open for further replies.

kiwieur

Technical User
Apr 25, 2006
200
0
0
GB
I have been using the code below in access 2003 for around 18 months to send an e-mail I am now testing this using 2010 and I am getting an

"application defined or object defined error" at the line highlighted in red

Code:
Public Function cmdPrint_Click(view As Integer)
'On Error GoTo cmdPrint_Click_Error

Dim wnd As Long
Dim uClickYes As Long
Dim Res As Long
Dim strFilename As String
Dim strNewName As String
Dim strHead As String
Dim strSub As String
Dim DDate As String
Dim strfrp As String
Dim strContact As String


strFilename = "Some Company"
strSub = " "
strOrderID = Forms![frmOrdersMain]![txtOrderNo]
strOrderBy = Forms![frmOrdersMain]![txtOrderedBy]

D = Format(Now, "dd")
m = Format(Now, "mm")
Y = Format(Now, "yy")

DDate = D & "-" & m & "-" & Y

strHead = strFilename & "  Order No_" & strOrderID & "  Dated " & DDate

strNewName = DLookup("[Path]", _
    "tblFilePaths", "[Type] = 'E-Mail'") & strFilename & "_Order No_" & strOrderID & "_" & DDate & ".pdf"

Forms![frmOrdersMain]![txtFileName] = strFilename & "_Order No_" & strOrderID & "_" & DDate & ".pdf"

Call SaveReportAsPDF("rptOrderB", strNewName)

 Dim patha, pathT, pathC, pathH, pathS, CustMail, KamMail As String

SuppMail1 = Forms!frmOrdersMain!txtEMail
SuppMail2 = Forms!frmOrdersMain!txtEMail2
patha = strNewName

strSub = "Dear Sir/Madam " & _
vbCrLf & vbCrLf & _
"Please find attached a PDF document relating to our Order No  " & strOrderID & _
vbCrLf & vbCrLf & _
"Kind regards" & _
vbCrLf & vbCrLf & _
strOrderBy

pathT = SuppMail1

pathC = SuppMail2

'pathC = DLookup("[E-mail]", "tblreps", _
    "repid = " & Forms!frmPriceInputScreen!ExternalRep)

If IsNull(pathT) And IsNull(pathC) = True Then

    MsgBox "You Must Select A Supplier Contact To EMail The Order To", vbInformation, "Email"
    
    Exit Function

End If


pathH = strHead
pathS = strSub

Dim result As Integer
Dim displaymessage As Boolean
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment ' Create the Outlook session.


' Create the message.

Set objOutlookMsg = objOutlook.CreateItem(olMailItem)

With objOutlookMsg

' Add the To recipient(s) to the message.

If Len(Nz(pathT)) > 0 Then

    [b][red]Set objOutlookRecip = .Recipients.Add(pathT)[/red][/b]
    objOutlookRecip.Type = olTo
    
End If

' Add the CC recipient(s) to the message.

If Len(Nz(pathC)) > 0 Then

'If pathC <> "" Then

    Set objOutlookRecip = .Recipients.Add(pathC)
    objOutlookRecip.Type = olCC

End If


 ' Set the "From" field
   objOutlookMsg.SentOnBehalfOfName = ""


' Set the Subject, Body, and Importance of the message.

.Subject = (pathH)
.Body = (pathS)
.Importance = olImportanceHigh 'High importance

' Add attachments to the message.
'If Not IsMissing(AttachmentPath) Then

Set objOutlookAttach = .Attachments.Add(patha)

' Resolve each Recipient's name.

For Each objOutlookRecip In .Recipients

objOutlookRecip.Resolve

Next


    .Display


End With

Set objOutlook = Nothing


   On Error GoTo 0
   Exit Function

cmdPrint_Click_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure cmdPrint_Click of Module Email"



End Function

could someone tell me where I am going wrong please [ponder][sadeyes]

Regards

Paul

 
1DMF,

I have even been able to add an attachment however the To e-mail address is blank.

any ideas?

Regards

Paul

 
nope, can I see your code that uses the wrapper.

Also what version of Access are you using?

I'm using Office 2010 Pro with SP1 , all users have Access 2010 Runtime with SP1.

I'll have a test with my access 2007 at home tonight, I dont' have access 2003 anymore and the DB is in 2007 format now, so not sure if any of this could be an issue.

if you comment out the clearing of the oReciept , does it make a difference?

"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!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Just a thought, I remember Outlook 2003 used to moan if you tried to send yourself an email as the only TO recipient if you were also the sender programatically via VBA.

Are you using the same TO and sender email addresses?





"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!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
Well I just tested at home using Access 2007 with Outlook 2007 and it worked perfectly?

I even opened a brand new blank Access 2007 database.

Coppied the exact EmailWrapper code from my FAQ and then used the test code above.

Worked first time, no errors, sent / received email as expected.

Not sure why you might be having this problem?

Have you converted the Access 2003 application to the new 2007 format?

"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!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
 
1DMF,
Firstly thanks for helping me

Tested with Access 2003

I just tried "comment out the clearing of the oReciept" makes no difference e-mail address still blank. Tried it with outlook open and closed - no difference.

Just created a brand new access 2010 database using Office 2010 Pro with SP1 on my other PC, imported the email wrapper, I put that in the global code module and then just ran atest via the immedate window as you had done and I get "Application-defined or object defined error"

Just created a brand new access 2007 database using office 2007 plus with service pack 2 on another PC I have, imported the email wrapper, I put that in the global code module and then just ran atest via the immedate window as you had done and I get "Application-defined or object defined error"

seems really strange


Regards

Paul

 
MaZeWorX,

Am i being really stupid here but I cannot see anywhere in either of the 2 functions where we Dim Outlook

Code:
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment

Your first function
Code:
Public Function FcnSendEmail(strTo As String, _
                             strSubject As String, _
                             strMessageBody As String, _
                             Optional strAttachmentPath As String, _
                             Optional strCC As String, _
                             Optional strBCC As String) As Boolean
    On Error Resume Next

    Set objOutlook = GetObject(, "Outlook.Application")
    If Err <> 0 Then
        Set objOutlook = CreateObject("Outlook.Application")
    End If
    Err.Clear
    On Error GoTo FcnSendEmail_Error
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)    ' Create the message.
    With objOutlookMsg
        .To = strTo
        If Len(strCC) > 0 Then    ' Add the CC recipient(s) to the message.
            .CC = strCC
        End If
        
        .SentOnBehalfOfName = ""    ' Set the From field
        .Subject = strSubject    ' Set the Subject
        .Body = strMessageBody    ' Set message body
        .Importance = olImportanceHigh    'Set importance

        If Len(strAttachmentPath) > 0 Then
            Set objOutlookAttach = .Attachments.Add(strAttachmentPath)
        End If
        If Len(strBCC) > 0 Then
            .BCC = strBCC
        End If

        For Each objOutlookRecip In .Recipients    ' Resolve each Recipient's name.
            objOutlookRecip.Resolve
        Next
        .Display
    End With
    FcnSendEmail = True
    Set objOutlook = Nothing

   On Error GoTo 0
   Exit Function

FcnSendEmail_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure FcnSendEmail"
End Function

Your second function

Code:
Public Function SendMail_Man()

    Dim strFilename, DDate As String
    Dim strTo, strSubject, strMessageBody As String
    Dim strAttachmentPath, strCC, strBCC As String
    Dim blnSuccess As Boolean

    On Error GoTo SendMail_Man_Error

    strFilename = "Some Company"
    strOrderID = Forms![frmOrdersMain]![txtOrderNo]
    strOrderBy = Forms![frmOrdersMain]![txtOrderedBy]

    DDate = Format(Now(), "dd-mm-yy")

    strTo = Forms!frmOrdersMain!txtEMail
    strCC = Forms!frmOrdersMain!txtEMail2
    strSubject = strFilename & "  Order No_" & strOrderID & "  Dated " & DDate
    strAttachmentPath = DLookup("[Path]", _
                                "tblFilePaths", "[Type] = 'E-Mail'") & strFilename & "_Order No_" & strOrderID & "_" & DDate & ".pdf"

    strbody = "Dear Sir/Madam " & _
              vbCrLf & vbCrLf & _
              "Please find attached a PDF document relating to our Order No  " & strOrderID & _
              vbCrLf & vbCrLf & _
              "Kind regards" & _
              vbCrLf & vbCrLf & _
              strOrderBy

    Forms![frmOrdersMain]![txtFileName] = strFilename & "_Order No_" & strOrderID & "_" & DDate & ".pdf"

    If IsNull(strTo) Or strTo = "" Then
        MsgBox "You Must Select A Supplier Contact To EMail The Order To", vbInformation, "Email"
        Exit Function
    End If

    blnSuccess = SaveReportAsPDF("rptOrderB", strNewName)
    If blnSuccess = True Then
        Call FcnSendEmail(strTo, strSubject, strMessageBody, strAttachmentPath, strCC, strBCC)
    Else
        MsgBox "Failed to create Report"
        Exit Function
    End If

    On Error GoTo 0
    Exit Function

SendMail_Man_Error:

    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in procedure SendMail_Man of Module Module3"

End Function

Sorry if i am being a bit dense on this



Regards

Paul

 
The code use late binding.

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
"Application-defined or object defined error"
is because you don't have outlook open.

There is no defensive programming in the code to protect against users trying to send emails when outlook isn't open.

I'm a little confused that you are running the code in Access 2003, when your thread title is
code runs in access 2003 but not in 2010
- aren't we trying to get working code for the newer version of access not the old one?

1. create a blank DB in access 2007 format.
2. import wrapper code
3. paste in test code
4. open outlook
5. run test code

Hopefully it will then run fine. Of course if you wan't to add error handling for users not having outlook open, you are most welcome and would be interested in seing what you implement.

I just wrote the simple wrapper as an example of how to move away from CDO 1.2.1.

All users where I work have outlook open the entire day so checking for it being open wasn't something i needed to worry about.

Even when i do get the odd support request (from new staff members), it only takes one second to explain they need to have outlook open , and I never normally get any further request for support from them for the same issue!

Hope you finally figure it out, but just holla if you need more help.

As PHV states, my code also has been changed to use late bindings. This means it doesn't matter what version of Office the user has, though that doesn't include Access!

What we mean by that is if you integrate with Outlook, Excel, Word, you can use late binding and Office will handle opening the required application.

This way you can remove your references within the application to a specific version of Office and it will then run independently.

This is a huge bonus when you have a mixed office version environment, it will also make the boss happy, because it means you can move away from every client machine having to have the full version of office installed just to have Access.

Here at work, we now run Access 2010 Runtime on ALL users machines, which is FREE! (obviously I still need the full version of Office 2010 Pro to develop the access applications!)

Now there are no compatability issues with the access code base, the app works the same on everyone's machine (well remote Vista users can still be a pain in the backside, but hey that's Vista for you!), and the boss only has to purchase the standard version of office with new machines (Word, Excel, Outlook) saving @ £100.00 per user!

It also means those users with legacy machines and Office 2003, becuase the DB's they run are on Access Runtime 2010 and the code uses late binding, it still works perfectly when pluging into Outlook, Excel & Word.

You really should think about going down the runtime / package solution method of rolling out your applications, it has saved me a shed load of work not having to maintain various code base versions, eveyone is using the same systems, the boss saves money and rolling out changes is a doddle!








"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!"

MIME::Lite TLS Email Encryption - Perl v0.02 beta
[url=http://d
 
1DMF,

Thanks for taking the time to reply to me, i will try and explain a little better what I am trying to achieve at the moment.

99% of users are on office 2003 including access and outloook,It was the companies intention to move to office 2007 initially but now they have decided to just go straight to office 2010.

we have several DB's split as FE/BE created in access 2003 that use outlook automation these were written by someone who has now left the company and I have been tasked with testing all of these DB's and making them work with 2010. My problem is that a few of our users already have office 2007 and this means that initially I have to change the code so that the outlook automation will run from the users PC irrespective of the office version they are running.

I have tested your email wrapper again today using a new blank 2007 DB and outlook open and i get the e-mail message but the "To" field is blank

I have tested your e-mail wrapper again with a fresh new 2010 Db and outlook open and i get the e-mail message but the "To" field is blank

This is the same as when I tried it with a new 2003 DB, the code runs fine if outlook is open but the "to" field is blank.

commenting out "Set oReceipt = Nothing" makes no difference


Regards

Paul

 
Hmm, so you are sent the email, and you recieve the email but the TO field is blank?

Very odd, if the address wasn't in the TO field you wouldn't receive the email?

If you open the recieved email and click reply, does it correctly populate the email showing sender/recipient details in the email body?

I didn't expect commenting the oReceipt to do anything, as it is simply clearing the recipient object after the addresees are added to the email object, so there is no link between the two.

If you add multiple recipients as well as BCC recipients do you see them correctly when you view your sent items?







"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!"

Free Electronic Dance Music Downloads
 
I've done a quick Goole and scoured many other people's code , they all use the same method as I for adding recipients...
Code:
 With objMail
        numRecips = UBound(toRecip)
        For i = 1 To numRecips
            If LenB(toRecip(i)) > 0 Then
                .Recipients.Add toRecip(i)  
            End If
        Next i

And is how Microsoft show it being used..
I'm not sure why you are seing this behaviour.

Try the following instead of what's currenty in the wrapper
Code:
    Dim vName As Variant
    
    With l_Msg
    
        'add recipients
        For Each eml In Recip
        
            vName = Split(eml, "@")
            MsgBox vName(0)
            Set oReceipt = .Recipients.Add("'" & vName(0) & "'<" & eml & ">")
            oReceipt.Type = olTo
        Next

Which gives name and email address. Does it make a difference?

Another thought, Do you still have references to Office in the application and CDO 1.2.1

If you remove the reference to CDO 1.2.1 , does that resolve the issue?



"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!"

Free Electronic Dance Music Downloads
 
Oh, I've left a debug statement in there!
Code:
MsgBox vName(0)

"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!"

Free Electronic Dance Music Downloads
 
1DMF,

sorry once again my terminology sucks what I meant to say was

I have set it so that the e-mail displays rather than sends and the "To" field is Blank in 2003, 2007 & 2010

REFERENCES for 2010

Visual Basic For Applications
Microsoft Office 14.0 Object Library
OLE Automation
Microsoft Office 14.0 Access Database Engine Object



Regards

Paul

 
dunno , I cannot reproduce your problem?

The references I have are...

Visual Basic For Applications
Microsoft Office 14.0 Object Library
OLE Automation
Microsoft Internet Controls
Microsoft Office 14.0 Access database engine Object Library

So unfortunately i'm stumped, as this is not something I can reproduce my end.

Anyone else got any ideas?





"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!"

Free Electronic Dance Music Downloads
 
Try adding

Microsoft Outlook 14.0 Object Library

???
 
Doing so binds the application to a specfic version of Office.

The point of having 'late bindings' in classes / methods is so your application will work with ANY version of office.



"In complete darkness we are all the same, it is only our knowledge and wisdom that 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!"

Free Electronic Dance Music Download
 
I had same issue kiwieur had. When I added "Microsoft Outlook 14.0 Object Library" problem went away but we are all on Outlook 2010 (ver 14). Yes, it does defeat the purpose of having 'late bindings'. Without that reference the "To:" email line is blank and the undeliverable error is as follows. With it, your code works beautifully!

Your message did not reach some or all of the intended recipients.
Subject: Document Approval Request
Sent: 5/29/2012 2:24 PM
The following recipient(s) cannot be reached:
<email name of added recipient even though not on "To:" line> on 5/29/2012 2:24 PM
This message could not be sent. Try sending the message again later, or contact your network administrator. Error is [0x80070057-00000000-00000000].


 
Very odd, I cannot re-create that behaviour?

We have a mixed office environment 2003/2007/2010 , it works fine for everyone on all versions. the only common denominator is they all run Acess 2010 runtime with SP1 and the hotfix that is required (KB2596585).

"In complete darkness we are all the same, it is only our knowledge and wisdom that 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!"

Free Electronic Dance Music Download
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top