Consulting

Results 1 to 3 of 3

Thread: Phone message macro - need help to copy textbox value into mail body

  1. #1
    VBAX Regular
    Joined
    Jan 2015
    Posts
    25
    Location

    Phone message macro - need help to copy textbox value into mail body

    Hi,

    our receptionist sends emails daily to a user as he/she was not available to take a call.
    She just types in the subject - like ** Call Peter, from Microsoft, phone +45 22 33 44 55

    We want to create a simple macro for her

    I have the form ready - see attached picture

    The code is almost ready

    Please can you help me to finish it. I am not sure how to insert spaces in the subject, and I cannot get the txtComment.Value into the mail body
    Please let me know if you need any more info or if some of the code is not correct
    Private Sub cmdCancel_Click()
        Me.Hide
    End Sub
    
    Private Sub cmdSubmit_Click()
    
    Dim oFSO As Object
        
        Dim objOutlook As Outlook.Application
        Dim objMail As Outlook.MailItem
    
        Set objMail = Outlook.CreateItem(olMailItem)
        strBody = objMail.HTMLBody
        
        'Insert Recipient
        objMail.To = txtTo.Value
        
        'Spaces missing
        objMail.Subject = "** Ring til" + txtContact.Value + txtOrg.Value + txtPhone.Value
            
        
        ' Set email body format to HTML
        objMail.BodyFormat = olFormatHTML
        
        ' Doesn't work
        objMail.HTMLBody = txtComment.Value
        
        ' Insert email text and signatur to email.
        objMail.HTMLBody = "<body font-size=11pt>" & sText & strBody & "</body>"
         
    
        Me.Hide
        objMail.Display
         
        Set oFSO = Nothing
        Set oFS = Nothing
        Set objMail = Nothing
        
    End Sub
    
    Private Sub UserForm_Activate()
            
        txtTo.Value = ""
        txtContact.Value = ""
        txtOrg.Value = ""
        txtPhone.Value = ""
        txtComment.Value = ""
        
    End Sub
    Attached Images Attached Images

  2. #2
    VBAX Regular
    Joined
    Jan 2015
    Posts
    25
    Location
    Attached the image twice - removed it here

  3. #3
    The following should work for you. Note that as you are running from Outlook you don't have to create an Outlook item, you already have one that you are using!

    Private Sub cmdSubmit_Click()
    
    Dim objMail As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    
        Me.Hide
        Set objMail = objOutlook.CreateItem(0)
        With objMail
            .To = Me.txtTo.Text
            'Spaces missing - Chr(32) is a space or " "
            .Subject = "** Ring til " & Me.txtContact.Text & Chr(32) & _
                       Me.txtOrg.Text & Chr(32) & Me.txtPhone.Text
            ' Set email body format to HTML
            .BodyFormat = 2
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            Set oRng = wdDoc.Range(0, 0)
            .Display
            oRng.Text = Me.txtComment.Text
            oRng.Font.Size = 11
        End With
        Set objMail = Nothing
    lbl_Exit:
        Set olInsp = Nothing
        Set objMail = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    If you unload the form in the calling macro you won't need to reset the field values. e.g.

    Option Explicit
                Sub ShowMyForm()
                UserForm1.Show
                Unload UserForm1
                End Sub
    Last edited by gmayor; 05-30-2015 at 03:57 AM.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

Posting Permissions

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