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.
Related
I try to coding is sending Rich text field via email , but I find an error that's I think this method for sending email, by following code
Sub Click(Source As Button)
Dim s As New NotesSession
Dim w As New NotesUIWorkspace
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim uidoc As NotesUIDocument
Set uidoc = w.CurrentDocument
Set s = New NotesSession
Set w = New NotesUIWorkspace
Set db = s.CurrentDatabase
Set doc = New NotesDocument (db)
doc.sendTo =s.UserName
doc.Subject = "Employee Information"
Dim rt As NotesRichTextItem
Set rt = New NotesRichTextItem ( doc, "Body" )
'Dim file As Variant 'if I use this code for declare for get value; Error : Type Mismatch
'Set file = doc.GetFirstItem("Body")
Dim rtitem As NotesRichTextItem 'if I use this code for declare for get value ; Error : Missing text object
Set rtitem = doc.GetFirstItem( "Body" )
Call rt.AppendRTItem(rtitem)
doc.Send(False)
End Sub
One thing I notice is that you don't set the form on the mail document you are creating.
You have some code you don't need, since you don't use uidoc anywhere, no need for that or declaring a NotesUIWorkspace object.
I also recommend that you use better variable names, and not to use extended notation when you set field values in a NotesDocument object.
I suggest that you take a look at the articles here:
http://blog.texasswede.com/how-to-write-better-code-in-notesdomino/
Below is the code that I cleaned up:
Option Public
Option Declare
Sub Click(Source As Button)
Dim session As New NotesSession
Dim ws As New NotesUIWorkspace
Dim db As NotesDatabase
Dim mailDoc As NotesDocument
Dim mailBody As NotesRichTextItem
Set db = session.CurrentDatabase
Set mailDoc = New NotesDocument(db)
Call mailDoc.ReplaceItemValue("Form","Memo")
Call mailDoc.ReplaceItemValue("SendTo",session.UserName)
Call mailDoc.ReplaceItemValue("Subject","Employee Information")
Set mailBody = New NotesRichTextItem(mailDoc,"Body" )
'Dim file As Variant 'if I use this code for declare for get value; Error : Type Mismatch
'Set file = doc.GetFirstItem("Body")
Dim rtitem As NotesRichTextItem 'if I use this code for declare for get value ; Error : Missing text object
Set rtitem = doc.GetFirstItem("Body")
Call mailBody.AppendRTItem(rtitem)
Call mailDoc.Send(False)
End Sub
The big question here is where you are getting the rich text field you want to send from? In your original code you are trying to read it from the newly created document (the one I call mailDoc). But that does not make any sense.
Your problem is simply that you are not reading the rich text from anywhere.
If your goal is to send an email, you can use my mail notification class:
http://blog.texasswede.com/updated-mailnotification-class-now-with-html-email-support-and-web-links/
Then your code would look something like this:
Dim session As New NotesSession
Dim mail As NotesMail
' *** Create a mail
Set mail = New NotesMail()
' Set receipient and subject
mail.MailTo = session.CommonUsername
mail.Subject = "Employee Information"
mail.Principal = "noreply#example.com"
' Create body content from rtitem.
' Yes, I should have added a method in the
' class to append RichtText to the mail body...
mail.body.AppendRTItem(rtitem)
Call mail.Send()
The only thing you have to do is to get the rtitem from somewhere. Since your original code declared a NotesUIWorkSpace object and a NotesUIDocument object, I am guessing you want to read it from the currently open document. Then you just add the following to the beginning of the code:
Dim ws As New NotesUIWorkspace
Dim thisdoc As NotesDocument
Dim rtitem as NotesRichTextItem
Set thisdoc = ws.CurrentDocument.Document
Set rtitem = thisdoc.GetFirstItem("Body")
Do you also see how much easier it is to read when you use descriptive variable names?
Hi you did not saved the document. Please be aware the Richtext is not available if the document is not saved.
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
Does anyone know how to create a RichText email body, that has a URL bookmark in it?
For example, here is my code:
Sub SendEmailUsingWord()
Dim olApp As Outlook.Application
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Word.Document
Dim strBody As String
' The body of the message, want to create a hyperlink to (e.g.) http://www.google.com but
' still display HERE.
' In HTML it would be HERE
' But I want to achieve this in a Rich Text email
strBody = "Please click HERE to be fantastic!" & vbNewLine
Set olApp = New Outlook.Application
Set olEmail = olApp.CreateItem(olMailItem)
With olEmail
.BodyFormat = olFormatRichText
.Display
.To = "someone#someone.com"
.Subject = "Email Subject"
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
wdDoc.Range.InsertBefore strBody
'.send
End With
End Sub
Many thanks for your help :)
Dave
After Dmitry pointing me in the right direction, I tried the following code:
wdDoc.Hyperlinks.Add wdDoc.Range, "http://www.google.com", , , "HERE"
and it has solved my problem!
I wasn't considering the word.document object when creating the email body, when I should have been. Hope this helps someone else.
There's small change in your code.
You need to add BODYFORMAT as
.BodyFormat = olFormatHTML
After setting BODYFORMAT, set the HTMLBODY of the Email.
.HTMLBody = "<HTML><H2>The body of this message will appear in HTML.</H2><BODY>Type the message text here. </BODY></HTML>"
Check this link for complete example:
http://msdn.microsoft.com/en-us/library/office/ff869979(v=office.15).aspx
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
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