Solved

Microsoft Access VBA Outlook

Posted on 2012-04-01
4
673 Views
Last Modified: 2012-04-01
I have used this code many times with no issues --- not sure why I'm getting "Compile Error - Expected function or variable." below where I have the <<<<<< arrows...

Any idea what's wrong with the code?


Public Function MySendEMail()

    If strMailAddress = "" Or IsNull(strMailAddress) Then
        MsgBox "No e-mail address on file.", vbInformation
        Exit Function
    End If

    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Dim rs As DAO.Recordset
    Dim maillist As DAO.Recordset
   
    Dim strCriteria As String, lngClientID As Long, EMailAddressOnFile As Boolean
    Dim theReport As String, intCount As Integer
   
   
    Set db = CurrentDb()

    strCriteria = "[CID]=" & lngID
   
    Set td = db.TableDefs("tblCustomer")
    Set rs = td.OpenRecordset(dbOpenDynaset, dbSeeChanges)
   
    rs.FindFirst strCriteria
    If rs.NoMatch = False Then

    Else
        MsgBox "No matching Borrower record. Try again.", vbInformation
        exit function
    End If
    rs.Close
   
    Set olapp = New Outlook.Application
    Set olMail = olapp.CreateItem(olMailItem)
    Set olNs = olapp.GetNamespace("MAPI")
    olNs.Logon

    With olMail
        .To = strMailAddress
        .Subject = "Correspondence from Chase"
        .BodyFormat = olFormatHTML
        '.Attachments.Add "c:\database temp\TransportOneInvoice.pdf"
        .HTMLBody = "<HTML><BODY> Dear " & strSalutation & " <br> <br> " & _
        strMemo & " <br> <br> " & _
        strRegarding & " <br> <br> " & _
        strMemo1 & " <br> <br> " & _
        strMemo2 & " <br> <br> " & _
        strMemo3 & " <br> <br> " & _
        strMemo4 & " <br> <br> " & _
        strClose & " <br> <br> " & _
        strSignatureLine & " <br> <br> " & _
       .Display                                      <<<<<<<<------------------------------------
    End With

    olNs.Logoff
           
    Set db = Nothing
    Set td = Nothing
    Set rs = Nothing

End Function
0
Comment
Question by:Ei0914
  • 2
4 Comments
 
LVL 119

Expert Comment

by:Rey Obrero
ID: 37793816
1. do a compact and repair of the app db
     office button > manage > compact and repair


2. check you references, make sure that there are no listed ref prefix with "MISSING"
if you find one uncheck then click OK,  then do  a DEBUG > Compile, make sure that there are no errors raised

post back the result.
0
 

Author Comment

by:Ei0914
ID: 37793882
No missing references. I created a new db and imported all the objects because I got a compatibility error (I created the app initially on a PC with 2010) -- this PC has 2007 -- so I wanted to see if that would fix the issue.

I am still getting the same error. When I debug/compile -- I am getting the same error.
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 37794231
I think you have one too many line continuation characters in there.

Try this:


Public Function MySendEMail()

    If strMailAddress = "" Or IsNull(strMailAddress) Then
        MsgBox "No e-mail address on file.", vbInformation
        Exit Function
    End If

    Dim db As DAO.Database
    Dim td As DAO.TableDef
    Dim rs As DAO.Recordset
    Dim maillist As DAO.Recordset
   
    Dim strCriteria As String, lngClientID As Long, EMailAddressOnFile As Boolean
    Dim theReport As String, intCount As Integer
   
   
    Set db = CurrentDb()

    strCriteria = "[CID]=" & lngID
   
    Set td = db.TableDefs("tblCustomer")
    Set rs = td.OpenRecordset(dbOpenDynaset, dbSeeChanges)
   
    rs.FindFirst strCriteria
    If rs.NoMatch = False Then

    Else
        MsgBox "No matching Borrower record. Try again.", vbInformation
        exit function
    End If
    rs.Close
   
    Set olapp = New Outlook.Application
    Set olMail = olapp.CreateItem(olMailItem)
    Set olNs = olapp.GetNamespace("MAPI")
    olNs.Logon

    With olMail
        .To = strMailAddress
        .Subject = "Correspondence from Chase"
        .BodyFormat = olFormatHTML
        '.Attachments.Add "c:\database temp\TransportOneInvoice.pdf"
        .HTMLBody = "<HTML><BODY> Dear " & strSalutation & " <br> <br> " & _
        strMemo & " <br> <br> " & _
        strRegarding & " <br> <br> " & _
        strMemo1 & " <br> <br> " & _
        strMemo2 & " <br> <br> " & _
        strMemo3 & " <br> <br> " & _
        strMemo4 & " <br> <br> " & _
        strClose & " <br> <br> " & _
        strSignatureLine & " <br> <br> "   '***** I  took off the &_ from this line.
       .Display                                     ' <<<<<<<<------------------------------------
    End With

    olNs.Logoff
           
    Set db = Nothing
    Set td = Nothing
    Set rs = Nothing

End Function
0
 

Author Closing Comment

by:Ei0914
ID: 37794336
Thanks a lot!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Outlook Free & Paid Tools
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now