Hi,
I am trying to email the results of a query on a click command but I don't know how to add the results of the query to the body of the email. The query is:
SELECT Count(*)AS Expr1 FROM assets WHERE Assets.AssetCategoryID = 1 AND assets.StatusID = 1
My email code is:
Public Sub SendMailOutlook()
Dim objApp As Outlook.Application
Dim objMailItem As Outlook.MailItem
Dim objNs As Outlook.NameSpace
Dim objFldr As Outlook.MAPIFolder
Dim strTo As String
' Instantiate objects
Set objApp = New Outlook.Application
Set objNs = objApp.GetNamespace("MAPI") ' open the MAPI Namespace
strTo = "email@email.com"
With objNs
Set objFldr = .GetDefaultFolder(olFolderOutbox)
Set objMailItem = objFldr.Items.Add(olMailItem)
With objMailItem
.To = strTo
.Subject = "Enter Subject Here"
.Body = "Enter message here" This is where I want the results to display
'.Display
.Send
End With
End With
' Destroy objects in the opposite order in which they were instantiated.
Set objMailItem = Nothing
Set objNs = Nothing
Set objApp = Nothing
End Sub
I'm pretty new to VBA so please be as detailed as possible.
Thanks,
Dev
I am trying to email the results of a query on a click command but I don't know how to add the results of the query to the body of the email. The query is:
SELECT Count(*)AS Expr1 FROM assets WHERE Assets.AssetCategoryID = 1 AND assets.StatusID = 1
My email code is:
Public Sub SendMailOutlook()
Dim objApp As Outlook.Application
Dim objMailItem As Outlook.MailItem
Dim objNs As Outlook.NameSpace
Dim objFldr As Outlook.MAPIFolder
Dim strTo As String
' Instantiate objects
Set objApp = New Outlook.Application
Set objNs = objApp.GetNamespace("MAPI") ' open the MAPI Namespace
strTo = "email@email.com"
With objNs
Set objFldr = .GetDefaultFolder(olFolderOutbox)
Set objMailItem = objFldr.Items.Add(olMailItem)
With objMailItem
.To = strTo
.Subject = "Enter Subject Here"
.Body = "Enter message here" This is where I want the results to display
'.Display
.Send
End With
End With
' Destroy objects in the opposite order in which they were instantiated.
Set objMailItem = Nothing
Set objNs = Nothing
Set objApp = Nothing
End Sub
I'm pretty new to VBA so please be as detailed as possible.
Thanks,
Dev