Consulting

Results 1 to 8 of 8

Thread: send email with body from textboxes on user form

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    send email with body from textboxes on user form

    I would like to send an email using form with text boxes on it
    User fills in textboxes and when he press button send the mail is send

    I would like from macro ;
    When user fills in all the textboxes the msgbox should appear with buttons vbYesNo and if user chooses Yes then procedure should be repeated but no to send another mail just add the next textboxes that were filled into e-mail's body

    thnx
    saban

  2. #2
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    Can you post a sample containing your form, the information you want to appear on the email, and confirm whether you want to send using Outlook.

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Why is when I try to put content(with table) of active document which contains table to body of e-mail, the table is not inserted
    [VBA]
    Private Sub CommandButton1_Click()
    Selection.TypeText ("Zahtevek za aliniranje") & vbCrLf & vbCrLf
    Selection.TypeText UserForm2.TextBox1 & vbTab
    Selection.TypeText UserForm2.TextBox2 & vbTab
    Selection.TypeText UserForm2.TextBox3 & vbTab
    Selection.TypeText UserForm2.TextBox4 & vbTab & vbCrLf
    UserForm2.TextBox1 = ""
    UserForm2.TextBox2 = ""
    UserForm2.TextBox3 = ""
    UserForm2.TextBox4 = ""
    UserForm2.TextBox1.SetFocus
    End Sub
    Sub SendMessage(DisplayMsg As Boolean, Optional AttachmentPath)
    ' Dim objOutlook As Outlook.Application
    'Dim objOutlookMsg As Outlook.MailItem
    'Dim objOutlookRecip As Outlook.Recipient
    'Dim objOutlookAttach As Outlook.Attachment
    Dim s As Range

    ' Create the Outlook session.
    Set objOutlook = CreateObject("Outlook.Application")
    ' Create the message.
    Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
    With objOutlookMsg
    ' Add the To recipient(s) to the message.
    Set objOutlookRecip = .Recipients.Add("lpoljak@europarl.eu.int")
    objOutlookRecip.Type = olTo
    ' Add the CC recipient(s) to the message.
    ' Set objOutlookRecip = .Recipients.Add("")
    'objOutlookRecip.Type = olCC
    ' Add the BCC recipient(s) to the message.
    ' Set objOutlookRecip = .Recipients.Add("")
    'objOutlookRecip.Type = olBCC
    Documents.Add
    ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:= _
    5, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
    wdAutoFitFixed
    Set s = ActiveDocument.Content
    ActiveDocument.Content.Copy
    's = Selection
    ' Set the Subject, Body, and Importance of the message.
    .To = "lppoljak@lu.coditel.net"
    .Subject = "Aliniranje"
    .Body = s
    .Importance = olImportanceHigh 'High importance
    .Send
    ' Add attachments to the message.
    'If Not IsMissing(AttachmentPath) Then
    ' Set objOutlookAttach = .Attachments.Add(AttachmentPath)
    ' End If
    ' Resolve each Recipient's name.
    'For Each objOutlookRecip In .Recipients
    ' objOutlookRecip.Resolve
    ' Next
    ' Should we display the message before sending?

    End With
    Set objOutlook = Nothing
    End Sub
    Private Sub CommandButton5_Click()
    'Dim objol As New Outlook.Application
    'Dim objmail As MailItem
    'Set objol = New Outlook.Application
    'Set objmail = objol.CreateItem(olMailItem)
    With objmail
    .To = "lppoljak@lu.coditel.net"
    .Subject = "Who/Me"
    .Body = TextBox1.Text
    .NoAging = True
    '.Attachments.Add "C:\A test.xls"
    .Display
    End With
    Set objmail = Nothing
    Set objol = Nothing
    SendKeys "%{s}", True

    End Sub
    Private Sub CommandButton2_Click()
    Dim bStarted As Boolean
    Dim oOutlookApp As Outlook.Application
    Dim oItem As Outlook.MailItem
    On Error Resume Next
    'Get Outlook if it's running
    Set oOutlookApp = GetObject(, "Outlook.Application")
    If Err <> 0 Then
    'Outlook wasn't running, start it from code
    Set oOutlookApp = CreateObject("Outlook.Application")
    bStarted = True
    End If
    'Create a new mailitem
    Set oItem = oOutlookApp.CreateItem(olMailItem)
    With oItem
    'Set the recipient for the new email
    .To = "lpoljak@europarl.eu.int"
    'Set the recipient for a copy
    ' .CC = "recipient2@mail.com"
    'Set the subject
    .Subject = "Alinirati"
    'The content of the document is used as the body for the email
    .Body = ActiveDocument.Content
    .Send
    End With
    If bStarted Then
    'If we started Outlook from code, then close it
    oOutlookApp.Quit
    End If
    'Clean up
    Set oItem = Nothing
    Set oOutlookApp = Nothing
    ActiveDocument.Close wdDoNotSaveChanges
    UserForm2.TextBox1 = ""
    UserForm2.TextBox2 = ""
    UserForm2.TextBox3 = ""
    UserForm2.TextBox4 = ""
    UserForm2.TextBox1.SetFocus
    End Sub

    Private Sub CommandButton3_Click()
    Unload Me
    End Sub
    Private Sub Frame1_Click()
    End Sub
    Private Sub UserForm_Initialize()
    Documents.Add
    UserForm2.TextBox1.SetFocus
    End Sub

    [/VBA]

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    why is all formatting lost when i put activedocument contwent into body of email

  5. #5
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Is the email in plaintext?

  6. #6
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Yes but why ?

  7. #7
    Moderator VBAX Master geekgirlau's Avatar
    Joined
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    1,464
    Location
    In Outlook, have a look at Tools | Options | Mail Format - if messages are set to be composed in plain text, all formatting will be stripped. Also I'm not sure off the top of my head whether "ActiveDocument.Content" refers to the formatting as well as the text - confirmation anyone?

  8. #8
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    I dont think activedocument.content supports formating
    I guess I should make body of e-mail in HTML format

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •