VBA code to check if Outlook is Setup before sending mail - email

I have a code in Access where it will send an email to the person in charge when the user click on save button. The code will use Outlook.application to send the email.
The code works fine but if outlook is not setup (i.e. fresh install without any user account setup) then the my email code will get stuck until user reactivates Access to acknowledge the error.
Sub Send_Email()
Dim oApp As Outlook.Application
Dim oMail As MailItem
On Error GoTo MailErr
If IsNull(Email) Then
MsgBox "You do not have an email account! No email will be sent!" & vbNewLine & "Email updates will be sent to your supervisor!" Me.Email.Value = DLookup("[Email]", "tblEmployeeList", "EmpName = '" & Me.txtSupName & "'")
Else
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "IT Incident " & Me.ReqID & " has been created."
oMail.Subject = "Alert: New IT Incident"
oMail.to = Forms!MainForm!lblITAdminEmail.Caption
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End If
MailErr:
'MsgBox Err
If Err = 287 Then
AppActivate "Microsoft Access"
MsgBox "Error 287: Mail not sent! Pls contact IT/BI"
ElseIf Err <> 0 Then
MsgBox "Pls contact BI/IT admin! Error " & Err & " occured!"
End If
Set oMail = Nothing
Set oApp = Nothing
End Sub
Is there a way to use VBA to check if Outlook has been setup properly prior to running this code?

Assuming that you are using at least Outlook 2007; take a look at the DefaultProfileName property of your Outlook.Application Object. This will return an empty string if no profile has been created or if there is no default profile.
You could just check this, but I believe that it's possible that an Outlook profile could exist but no actual e-mail accounts are configured within it (for instance if the user aborted the set-up wizard part way through). In this instance you could look at the Accounts Object which includes a Count property. Obviously if this is 0 then you know there are no accounts configured within the profile.
A simple example of how you could implement this.
Dim oApp As Outlook.Application
Set oApp = Outlook.Application
If Not oApp.DefaultProfileName = "" Then
If oApp.Session.Accounts.Count > 0 Then
' Send the e-mail
End If
End If
Set oApp = Nothing

Related

How to generate email from Access form using contents of textbox

I have a main form with a 'help' button which opens a simple form with a textbox that a user can use to submit issues noted with the main form. I would like the contents of what the user types into the textbox to be emailed to myself and a co-worker using a 'send' button.
I found the following code on stackoverflow which works except I can't figure out how to have the body of the email include what the user types into the textbox instead of the static text that's currently in the code.
Here's how the code looks now:
Private Sub SendEmail_Click()
Dim olApp As Object
Dim objMail As Object
Dim Issue As String
strIssue = Me.ContactMessageBox
On Error Resume Next 'Keep going if there is an error
Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open
If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance
End If
'Create e-mail item
Set objMail = olApp.CreateItem(olMailItem)
With objMail
.To = "emailaddress.com"
.Subject = "Form issue"
.Body = "strIssue"
.send
End With
MsgBox "Operation completed successfully"
End Sub
Does anyone have ideas about how to do this?
Change
Dim Issue As String
strIssue = Me.ContactMessageBox
...
.Body = "strIssue"
to
Dim strIssue As String
strIssue = Me.ContactMessageBox
...
.Body = strIssue
If you put your variable between "" then it is read as a string instead of the variable.

sending outlook based email in VBScript - error (sfile as string) line?

im trying to send an email from a VBScript, it will eventually be added into a currently working script as an else if (if this is possible).
im getting an error at line 23 character 32?
Dim outobj, mailobj
Dim strFileText
Dim objFileToRead
Set outobj = CreateObject("Outlook.Application")
Set mailobj = outobj.CreateItem(0)
strFileText = GetText("C:\test\test 2.txt")
With mailobj
.To = "user#user.com"
.Subject = "Testmail"
.Body = strFileText
.Display
End With
Set outobj = Nothing
Set mailobj = Nothing
End Sub
Function GetText(sFile as String) As String
Dim nSourceFile As Integer, sText As String
nSourceFile = FreeFile
Open sFile For Input As #nSourceFile
sText = Input$(LOF(1), 1)
Close
GetText = sText
End Function
what do i need to add to get line 23 to work and the script to finally do what i need it to, i have copied most of this script from elsewhere due to a sincere lack of VBscripting knowledge?
Take a look at the Using Automation to Send a Microsoft Outlook Message article. It provides a sample code and describes all the required steps for sending emails.
Try this: remove the GetText function entirely, and replace the line
strFileText = GetText("C:\test\test 2.txt")
with
Set fso = CreateObject("Scripting.FileSystemObject")
strFileText = fso.OpenTextFile("C:\test\test 2.txt").ReadAll

Access 2007 to Word, Outlook for 2-step Mail Merge

My VBA does the email merge wonderfully ... except, I have to click on "allow" - from Outlook - twice, for each email generated. Is there a way to programatically get around this?
I am running Windows 7 Enterprise, SP1. MS Office 2007. Computer is tightly locked down. HTML format e-mail not supported - only plain text. I do not have local admin rights. I am not able to install additional add-ins or third party software, as I've found suggested numerous times.
Here is my working VBA:
Private Sub Send_Email_Merge()
Dim sDBPath As String
'Word variables
Dim oWD As Word.Application
Dim oDoc As Word.Document
Dim RecCount As Long
'Sanity check on how many e-mails to be sent
RecCount = DLookup("[Email Count]", "qry_EmailMerge_Count")
Debug.Print RecCount
Set oWD = CreateObject("Word.Application")
oWD.Visible = True
Set oDoc = oWD.Documents.Open("C:\MyTemp\MyDocs\MyEmailMerge.docx")
With oDoc.MailMerge
.MainDocumentType = wdFormLetters
sDBPath = "C:\MyTemp\MydBs\My_Engine.accdb"
.OpenDataSource Name:=sDBPath, _
SQLStatement:="SELECT * FROM [qry_E Mail Merge]"
End With
oWD.Activate
oWD.Documents.Parent.Visible = True
oWD.Application.WindowState = 1
oWD.ActiveWindow.WindowState = 1
With oDoc.MailMerge
.Destination = wdSendToEmail
.MailAddressFieldName = "Email Address"
.MailSubject = "Your Action Required"
.MailFormat = wdMailFormatPlainText
.Execute
End With
oWD.Activate
oWD.Documents.Parent.Visible = True
oWD.Application.WindowState = 1
oWD.ActiveWindow.WindowState = 1
oWD.ActiveDocument.Close
oWD.Quit
Set oWD = Nothing
Set oDoc = Nothing
End Sub
Any help is appreciated.
You can automate directly from Access. See How to automate Outlook from another program for more information. It describes all the required steps for automating Outlook from another applications.

excel vba inputbox

I am have the following code for excel vba that will email a range of addresses in my sheet. Howeve, I am looking to maybe use an inputbox to determine what the range is that I would like to email. The trouble i run into is getting the input to become a value that the function mailid understands. any suggestions?
Sub EmailActiveSheetWithOutlook2()
Dim oApp, oMail As Object, _
tWB, cWB As Workbook, _
FileName, FilePath As String
Application.ScreenUpdating = False
'Set email id here, it may be a range in case you have email id on your worksheet
Sheets("Sheet1").Select
mailId = Range("b4:b5").Value
'Write your email message body here , add more lines using & vbLf _ at the end of each line
Body = "Hello, it appears you have not yet filled out the transportation contact information excel sheet. This sheet was emailed to you, please complete this and send to me saved as your firstnamelastname.xls at your earliest convience." & vbLf _
& vbLf _
& "Thanks & Regards" & vbLf _
& vbLf _
& "-Ryan " & vbLf _
'Sending email through outlook
Set oApp = CreateObject("Outlook.Application")
Set oMail = oApp.CreateItem(0)
With oMail
.To = mailid
.Subject = "Transportation Committee Notice"
.Body = Body
'.Attachments.Add tWB.FullName
.send
End With
End Sub
To replicate the effect of your current code use
mailid = Application.InputBox(Prompt:="Select Range", Type:=8)
where Type:=8 specifies a return type of Range. This returns the Value property of the selected range into mailid
Alternatively use
Dim rng as Range
Set rng = Application.InputBox(Prompt:="Select Range", Type:=8)
mailid = rng.Value
rng is then set to the selected range, and can be validated before use
Note that you should add error handling to account for, eg user Cancelling the InputBox
Do not set Application.ScreenUpdating = False before issuing InputBox as this will prevent the user interacting with the screen.
As an aside, your code uses Dim incorrectly: Dim'ing a variable without a As clause declares it as `Variant.
eg
Dim oApp, oMail As Object
actually declares oApp as a Variant, use
Dim oApp As Object, oMail As Object

How can I send a Word document as an attachment to a specific recipient?

I've created a form in Word 2010, and I added a button to send the form.
What i want is that when the button is clicked, the form is attached to an email message, and a specific email address is automaticlly entered in the 'To' field of the message.
I managed to get the button to open an email message and add the form as an attachment, but I can't get it to enter my email address.
It depends on how you are generating the email message. For example, see below for one method.
Private Sub CommandButton1_Click()
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = "test#test.com"
.CC = ""
.BCC = ""
.Subject = "Subject goes here"
.Body = "Text of body goes here."
.Attachments.Add (\file\to\attach.txt)
.Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub