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

Embed Query Results in an Email 1

Status
Not open for further replies.

LonnieJohnson

Programmer
Apr 16, 2001
2,628
US
I want to embed the results of two queries into an email. Is this possible? I do not want to attach. I want both queries to actually be in the body of the mail like two tables.

Lemmeno...

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
If you are using Outlook, you can do this.
 
Okayyyyyy,

I am using Outlook. How would one do this?

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
You could try exporting the queries as html files then reading them back into the body of an email. I think Outlook allows html formatted mail.
Peter

[η][β][π]
 
You could build the body line by line with code, create a report, or simply use the query:
Code:
Sub RTFBodyX()
Const ForReading = 1, ForWriting = 2, ForAppending = 3

Dim fs, f
Dim RTFBody, strTo
Dim MyApp As New outlook.Application
Dim MyItem As outlook.MailItem

'DoCmd.OutputTo acOutputReport, "Report1", acFormatRTF, "C:\Docs\Report1.rtf"
DoCmd.OutputTo acOutputQuery, "Query1", acFormatRTF, "C:\Docs\Query1.rtf"
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("C:\Docs\Query1.rtf", ForReading)
RTFBody = f.ReadAll
f.Close

Set MyItem = MyApp.CreateItem(olMailItem)
With MyItem
   .To = "me@email.com"
   .Subject = "txtSubjectLine"
   .Body = RTFBody
End With
MyItem.Display
End Sub

I have put a commented line for outputting a report in, just in case.
 
Hmmmmm [ponder] This strange method you speak of intrest me Remou. I shall try it and return with a reply. Thank you.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Remou, when it reads the file it returns some garblygook and this is what is in the email. I even tried changing the .Body to .HTMLBody. Any ideas.


{\rtf1\ansi\ansicpg1252{\colortbl\red0\green0\blue0;\red255\green255\blue255;\red192\green192\blue192;}{\fonttbl\f0\fcharset0\fnil Arial;\f1\fcharset0\fnil Arial;\f2\fcharset0\fnil Arial;}\trowd\trgaph40\clbrdrl\brdrs\brdrcf0\clbrdrt\brdrs\brdrcf0\clbrdrb\brdrs\brdrcf0\clbrdrr\brdrs\brdrcf0\clshdng10000\clcfpat2\cellx875\clbrdrl\brdrs\brdrcf0\clbrdrt\brdrs\brdrcf0\clbrdrb\brdrs\brdrcf0\clbrdrr\brdrs\brdrcf0\clshdng10000\clcfpat2\cellx2275\clbrdrl\brdrs\brdrcf0\clbrdrt\brdrs\brdrcf0\clbrdrb\brdrs\brdrcf0\clbrdrr\brdrs\brdrcf0\clshdng10000\clcfpat2\cellx3255\clbrdrl\brdrs\brdrcf0\clbrdrt\brdrs\brdrcf0\clbrdrb\brdrs\brdrcf0\clbrdrr\brdrs\brdrcf0\clshdng10000\clcfpat2\cellx4970\clbrdrl\brdrs\brdrcf0\clbrdrt\brdrs\brdrcf0\clbrdrb\brdrs\brdrcf0\clbrdrr\brdrs\brdrcf0\clshdng10000\clcfpat2\cellx6280{\trrh270\pard\intbl{\qc\fs20\b\f1\cf0\cb2 CSNBR\cell}\pard\intbl{\qc\fs20\b\f1\cf0\cb2 LNAME\cell}\pard\intbl{\qc\fs20\b\f1\cf0\cb2 FNAME\cell}\pard\intbl{\qc\fs20\b\f1\cf0\cb2 CSMGRNAME\cell}\pard\intbl{\qc\fs20\b\f1\cf0\cb2 KVCOFFICE\cell}\pard\intbl\row}\trowd\trgaph40\clbrdrl\brdrs\brdrcf2\clbrdrt\brdrs\brdrcf2\clbrdrb\brdrs\brdrcf2\clbrdrr\brdrs\brdrcf2\clshdng10000\clcfpat1\cellx875\clbrdrl\brdrs\brdrcf2\clbrdrt\brdrs\brdrcf2\clbrdrb\brdrs\brdrcf2\clbrdrr\brdrs\brdrcf2\clshdng10000\clcfpat1\cellx2275\clbrdrl\brdrs\brdrcf2\clbrdrt\brdrs\brdrcf2\clbrdrb\brdrs\brdrcf2\clbrdrr\brdrs\brdrcf2\clshdng10000\clcfpat1\cellx3255\clbrdrl\brdrs\brdrcf2\clbrdrt\brdrs\brdrcf2\clbrdrb\brdrs\brdrcf2\clbrdrr\brdrs\brdrcf2\clshdng10000\clcfpat1\cellx4970\clbrdrl\brdrs\brdrcf2\clbrdrt\brdrs\brdrcf2\clbrdrb\brdrs\brdrcf2\clbrdrr\brdrs\brdrcf2\clshdng10000\clcfpat1\cellx6280{\trrh270\pard\intbl{\fs20\f2\cf0\cb1 R9902\cell}\pard\intbl{\fs20\f2\cf0\cb1 MCFARLANE\cell}\pard\intbl{\fs20\f2\cf0\cb1 JOSHUA\cell}\pard\intbl{\fs20\f2\cf0\cb1 MOSES, NANCY\cell}\pard\intbl{\fs20\f2\cf0\cb1 LAWRENCE\cell}\pard\intbl\row}}

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
No, I am baffled, because if I save your RTF above as a document and load it into an email using the code above, I get a table. What version of Outlook are you using?
 
XP (10.0)

When I open the document it shows a table with headers in the rtf file.

I only modified your code to include the name of the of my query and changed the HTMLBody. Well if you can think of anything let me know. Thanks for the time.

Code:
Sub RTFBodyX()
Const ForReading = 1, ForWriting = 2, ForAppending = 3

Dim fs, f
Dim RTFBody, strTo
Dim MyApp As Outlook.Application
Dim MyItem As Outlook.MailItem
    
        Set MyApp = CreateObject("Outlook.Application")
        Set MyItem = MyApp.CreateItem(olMailItem)

Kill "C:\Query1.rtf"
DoCmd.OutputTo acOutputQuery, "notificationAPAsigned", acFormatRTF, "C:\Query1.rtf"

Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("C:\Query1.rtf", ForReading)
RTFBody = f.ReadAll
f.Close

Set MyItem = MyApp.CreateItem(olMailItem)
With MyItem
   .To = "lcjohnson@kvc.org"
   .Subject = "txtSubjectLine"
   .HTMLBody = RTFBody
   '.Body = RTFBody
End With
MyItem.Display
End Sub

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
I find it does not work with HTMLBody, only with Body. If you wish to use HTML, you must OutputTo HTML:
DoCmd.OutputTo acOutputQuery, "Query1", acFormatHTML, "Query1.htm
 
It does not work with the Body but when I use HTMLBody and Output to HTML as you suggested.... it works!!!!

You da man! I wish I could give you two stars but I can't. So, here's a thumbs up instead. [thumbsup2]

Thanks again.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
One more thing. I repeated this for two queries. How do I append the second query to the RTFBody string. I tried ...

Code:
RTFBody = RTFBody & f.ReadAll

and also

Code:
RTFBody = RTFBody & vbNewLine & f.ReadAll

... on the second query and it only shows the results of the first one.

Any thoughts?

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Do not read the whole file in (readline). The last two lines are
</BODY>
</HTML>
You must loose them from the first file.
(and thanks for the *)
 
Thanks. I tried but failed. I know really nothing about HTML.

If you don't mind, can you look at the my code to see where I blew it. That's if I haven't consumed most of your day.

Code:
    Const ForReading = 1, ForWriting = 2, ForAppending = 3
    
    Dim fs, f 'file system object and rtf file to hold formatting of query
    Dim outFile As String 'rtf name to hold formatting of query
    Dim RTFBody As String 'query results to be read back into outlook
    Dim RTFBody2 As String 'query results to be read back into outlook
    Dim RTFBody3 As String 'query results to be read back into outlook
    
    Dim strTo As String 'recipiants
    Dim MyApp As New Outlook.Application
    Dim MyItem As Outlook.MailItem
    Dim strSubject As String 'Email subject
    Dim strMsg As String 'Email greeting
    Dim qryName As String 'Name of the query to be used
    Dim cnt As Integer 'Number of recs in the query
    Dim i As Integer 'Iteration variable
    
    strMsg = "Below are some major accomplishments that have occurred in the Permanency department!"
    strSubject = "Daily Good News from KVC Permanency"
    outFile = "c:\QueryOutput.rtf"
    
    Set fs = CreateObject("Scripting.FileSystemObject")
    
'***************** PERMANENCY **************************************************************
    qryName = "*** PERMANENCIES ***"
    
    If Dir(outFile) <> "" Then
        Kill outFile
    End If
    
        DoCmd.RunMacro "CreateTempPermTable"
        DoCmd.OutputTo acOutputQuery, qryName, acFormatHTML, outFile
        
        cnt = DCount("*", qryName)
        
        If cnt > 0 Then
            Set f = fs.OpenTextFile(outFile, ForReading)
            
            Do Until i = cnt + 1
                RTFBody = f.ReadLine
                i = i + 1
            Loop
            f.Close
        End If
        
        RTFBody = RTFBody & vbNewLine
        
        i = 0
    
'***************** RELATIVE AND NON-RELATIVE KIN PLACEMENTS *********************************
    qryName = "*** RELATIVE AND NON-RELATIVE KIN PLACEMENTS ***"
    
    If Dir(outFile) <> "" Then
        Kill outFile
    End If

    DoCmd.OutputTo acOutputQuery, qryName, acFormatHTML, outFile
    
        cnt = DCount("*", qryName)
        
        If cnt > 0 Then
            Set f = fs.OpenTextFile(outFile, ForReading)
            
            Do Until i = cnt + 1
                RTFBody = f.ReadLine
                i = i + 1
            Loop
            f.Close
        End If
        
        RTFBody = RTFBody & vbNewLine
        
        i = 0
        
 '***************** APA SIGNINGS ************************************************************
    qryName = "*** APA SIGNINGS ***"
    
    If Dir(outFile) <> "" Then
        Kill outFile
    End If

    DoCmd.OutputTo acOutputQuery, qryName, acFormatHTML, outFile
    
        cnt = DCount("*", qryName)
        
        If cnt > 0 Then
            Set f = fs.OpenTextFile(outFile, ForReading)
            
            Do Until i = cnt + 1
                RTFBody = f.ReadLine
                i = i + 1
            Loop
            f.Close
        End If
        
        RTFBody = RTFBody & vbNewLine
    
 '******************* SEND MAIL *************************************************************
 
    Set MyItem = MyApp.CreateItem(olMailItem)
    With MyItem
       .To = "lcjohnson@kvc.org"
       .Subject = "MyTestMailForGoodNews"
       .BodyFormat = olFormatRichText
       .HTMLBody = strMsg & RTFBody & "</BODY>" & vbNewLine & "</HTML>"
    End With
    MyItem.Display
    'MyItem.Send

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Have fun.
Code:
Sub Chas_Atlas()
    Dim outFile As String 'rtf name to hold formatting of query
    Dim qryName As String 'Name of the query to be used
    Dim cnt As Integer 'Number of recs in the query
    Dim astrQueries
    Dim i, strBody, strTemp
    
    astrQueries = Split("Query10,Query10,Query10", ",")
    
    For i = 0 To UBound(astrQueries)
        outFile = "c:\" & astrQueries(i) & ".rtf"
        qryName = astrQueries(i)
        
        If Dir(outFile) <> "" Then
            Kill outFile
        End If
    
        DoCmd.OutputTo acOutputQuery, qryName, acFormatHTML, outFile
        
        cnt = DCount("*", qryName)
        If cnt > 0 Then
            strTemp = ReadFile(outFile)
        End If
        
        strBody = strBody & "<P></P><P></P>" & strTemp
    Next
    SendMail (strBody)
End Sub

Sub SendMail(strBodyText)
    Dim strTo As String 'recipiants
    Dim MyApp As New Outlook.Application
    Dim MyItem As Outlook.MailItem
    Dim strSubject As String 'Email subject
    Dim strMsg As String 'Email greeting

    Set MyItem = MyApp.CreateItem(olMailItem)
    
    strMsg = "Below are some major accomplishments that have occurred in the Permanency department!"
    strSubject = "Daily Good News from KVC Permanency"

    With MyItem
       .To = "lcjohnson@kvc.org"
       .Subject = "MyTestMailForGoodNews"
       '.BodyFormat = olFormatRichText
       .HTMLBody = strMsg & strBodyText & "</BODY>" & vbNewLine & "</HTML>"
    End With
    MyItem.Display
    'MyItem.Send
End Sub

Function ReadFile(strFileName)
   Const ForReading = 1, ForWriting = 2
   Dim fso, f, r, i
   Dim intNoLines, intStartLine
   Dim strTemp
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(strFileName, ForReading)
   Do While f.AtEndOfStream <> True
        r = f.Readline
        If Left(r, 7) = "<TABLE " Then intStartLine = f.Line
   Loop
   intNoLines = f.Line
   
   Set f = fso.OpenTextFile(strFileName, ForReading)
   
   i = 1
   Do While i <= intNoLines - 2
      If i >= intStartLine - 1 Then
          strTemp = strTemp & f.Readline
      Else
          f.Readline
      End If
      i = i + 1
   Loop
   f.Close
   ReadFile = strTemp
End Function
 
Oh my, this looks delicious. Let me have at it. I will let you know what happens.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
That truly did it. Thank you Sir. You are a gentleman and definately a scholar.

ProDev, MS Access Applications
Visit me at ==>
May God bless you beyond your imagination!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top