Solved

Microsoft Access VBA Outlook

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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

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

Finding original email is quite difficult due to their duplicates. From this article, you will come to know why multiple duplicates of same emails appear and how to delete duplicate emails from Outlook securely and instantly while vital emails remai…
Read this checklist to learn more about the 15 things you should never include in an email signature.
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

770 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