Solved

Microsoft Access VBA Outlook

Posted on 2012-04-01
4
699 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 120

Expert Comment

by:Rey Obrero (Capricorn1)
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many people use more than one email account and so it becomes difficult for them to manage them when they use separate accounts,  so, in this article, I have shared an easy way to add Other Mail Accounts in your Google Inbox. It helps to combine all…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
Many of my clients call in with monstrous Gmail overloading issues with Outlook. A quick tip is to turn off the All Mail and Important folders from synching. Here is a quick video I made to show you how to turn off these and other folders in Gmail s…

839 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