Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 05-24-2017, 01:31 PM
cdc99999 cdc99999 is offline Transfer Outlook TextBox Text Into Email Body Windows 7 32bit Transfer Outlook TextBox Text Into Email Body Office 2013
Novice
Transfer Outlook TextBox Text Into Email Body
 
Join Date: May 2017
Location: Canada
Posts: 7
cdc99999 is on a distinguished road
Default Transfer Outlook TextBox Text Into Email Body


Hi all,

I have what I thought was a simple idea but I can' t find any info or get any help, seems like it's complicated if feasible at all. Trying my luck here

I would like for users to simply fill out fields (form maybe?) and the said fields (TextBoxes maybe?) get inserted into the body of an email template. This would avoid users having to copy and paste data into an email template and then have to adjust the font, formatting, etc.

Any way to achieve this? If so, how? Is via an Outlook form a possibility, where it would transfer TextBox data to specific places inside the body of an email?

Thanks in advance
Reply With Quote
  #2  
Old 05-25-2017, 12:23 AM
gmayor's Avatar
gmayor gmayor is offline Transfer Outlook TextBox Text Into Email Body Windows 10 Transfer Outlook TextBox Text Into Email Body Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

This is fairly straightforward using a pre-prepared template a simple userform and a macro to create the message. Here there are four fields, but you can have more or fewer as required and the labels simply reflect the content of the text fields.


The code for the form is:
Code:
Option Explicit

Private Sub CommandButton1_Click()
    Hide
    Tag = 1
End Sub

Private Sub CommandButton2_Click()
    Hide
    Tag = 2
End Sub
The code works in conjunction with an Outlook template here 'C:\Path\TemplateName.oft' which for the purpose of the exercise looks like

Note there are four texts bounded by ##. These are texts that the macro following will replace, and equate to the userform text fields.

The main code goes in a new module in the Outlook project. The line
Const sFindText As String = "#Name#|#Desc#|#Acres#|#Value#"
is a list of the fields/texts separated by the pipe character '|'. Obviously these have to match what you put in your template.

The macro opens the template and replaces the texts with the values entered in the userform then displays the message

Code:
Option Explicit

Sub ReplaceFromUserForm()
Dim olItem As MailItem
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim i As Long, j As Long
Const sFindText As String = "#Name#|#Desc#|#Acres#|#Value#"
Dim sReplaceText As String
Dim vFind As Variant
    With UserForm1
        .Caption = "Complete the fields"
        .CommandButton1.Caption = "Continue"
        .CommandButton2.Caption = "Cancel"
        .Show
        vFind = Split(sFindText, "|")
        Set olItem = CreateItemFromTemplate("C:\Path\TemplateName.oft")
        With olItem
            .BodyFormat = olFormatHTML
            Set olInsp = .GetInspector
            Set wdDoc = olInsp.WordEditor
            .Display
            For j = 0 To UBound(vFind)
                Set oRng = wdDoc.Range
                Select Case j
                    Case 0
                        sReplaceText = UserForm1.TextBox1.Text
                    Case 1
                        sReplaceText = UserForm1.TextBox2.Text
                    Case 2
                        sReplaceText = UserForm1.TextBox3.Text
                    Case 3
                        sReplaceText = UserForm1.TextBox4.Text
                    Case Else
                End Select
                With oRng.Find
                    Do While .Execute(findText:=vFind(j))
                        oRng.Text = sReplaceText
                        oRng.collapse 0
                        DoEvents
                    Loop
                End With
            Next j
            .Display
        End With
    End With
    Unload UserForm1
lbl_Exit:
    Exit Sub
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 05-25-2017, 03:52 AM
cdc99999 cdc99999 is offline Transfer Outlook TextBox Text Into Email Body Windows 7 32bit Transfer Outlook TextBox Text Into Email Body Office 2013
Novice
Transfer Outlook TextBox Text Into Email Body
 
Join Date: May 2017
Location: Canada
Posts: 7
cdc99999 is on a distinguished road
Default

Thanks gmayor, this is great!

I used your data as a reference but created my own template and changed the path in the code.

My form works but the button doesn't. The only way I can get it to work is to:

1) Enter the data in the form (not press any of the buttons)
2) Run the macro from Outlook

Only then does it open my email template with the form fields info in it based on where I placed the various ## references.

Will continue to troubleshoot and hopefully find the fix.

Many thanks, your help is much appreciated
Reply With Quote
  #4  
Old 05-25-2017, 04:33 AM
gmayor's Avatar
gmayor gmayor is offline Transfer Outlook TextBox Text Into Email Body Windows 10 Transfer Outlook TextBox Text Into Email Body Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

There is a line missing from the code.
Immediately after
Code:
.Show
there should be a line
Code:
If .Tag = 0 Then GoTo lbl_Exit
which will allow you to click CommandButton2 to cancel the process.

What 'button'? For running the process, create a button on the ribbon/QAT (Quick Access Toolbar) to run the macro 'ReplaceFromUserForm'
This will open the userform and allow you to enter the data, then the message is created.
You may run into problems with Outlook security - in which case see
http://www.gmayor.com/create_and_employ_a_digital_cert.htm
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #5  
Old 05-25-2017, 05:18 AM
cdc99999 cdc99999 is offline Transfer Outlook TextBox Text Into Email Body Windows 7 32bit Transfer Outlook TextBox Text Into Email Body Office 2013
Novice
Transfer Outlook TextBox Text Into Email Body
 
Join Date: May 2017
Location: Canada
Posts: 7
cdc99999 is on a distinguished road
Default


Thank you Sir!

The button I was referring to was the button on the form (continue button) which doesn't work, it does not automatically get the template and open it with the data entered.

I amended the code but it still does not work. I cannot use the button and when I activate the macro from Outlook it gives me a VB error “Run-time error ‘13”: Type mismatch.

When I press “Debug: it outlines the following code line in yellow: If .Tag = 0 Then

I’m guessing the new code line needs a minor adjustment?

Cheers


I have amended the code to include the include what you mentioned, below is what I now have:

Option Explicit
Sub ReplaceFromUserForm()
Dim olItem As MailItem
Dim olEmail As Outlook.MailItem
Dim olInsp As Outlook.Inspector
Dim wdDoc As Object
Dim oRng As Object
Dim i As Long, j As Long
Const sFindText As String = "#Name#|#Desc#|#Acres#|#Value#"
Dim sReplaceText As String
Dim vFind As Variant
With UserForm1
.Caption = "Complete the fields"
.CommandButton1.Caption = "Continue"
.CommandButton2.Caption = "Cancel"
.Show
If .Tag = 0 Then GoTo lbl_Exit
vFind = Split(sFindText, "|")
Set olItem = CreateItemFromTemplate("U:\Operations Centre\Email Templates\form_test.oft")
With olItem
.BodyFormat = olFormatHTML
Set olInsp = .GetInspector
Set wdDoc = olInsp.WordEditor
.Display
For j = 0 To UBound(vFind)
Set oRng = wdDoc.Range
Select Case j
Case 0
sReplaceText = UserForm1.TextBox1.Text
Case 1
sReplaceText = UserForm1.TextBox2.Text
Case 2
sReplaceText = UserForm1.TextBox3.Text
Case 3
sReplaceText = UserForm1.TextBox4.Text
Case Else
End Select
With oRng.Find
Do While .Execute(findText:=vFind(j))
oRng.Text = sReplaceText
oRng.collapse 0
DoEvents
Loop
End With
Next j
.Display
End With
End With
Unload UserForm1
lbl_Exit:
Exit Sub
End Sub
Reply With Quote
  #6  
Old 05-25-2017, 06:56 AM
gmayor's Avatar
gmayor gmayor is offline Transfer Outlook TextBox Text Into Email Body Windows 10 Transfer Outlook TextBox Text Into Email Body Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

I don't know what I was thinking this morning when I answered the question originally The code for Userform1 should be

Code:
Option Explicit

Private Sub CommandButton1_Click()
    Hide
    Tag = 1
End Sub

Private Sub CommandButton2_Click()
    Hide
    Tag = 0
End Sub
i.e. Tag = 0 is the response from the Cancel button and not '2' as I posted. Apart from that it works as described when I substitute my test template path for yours (and this error shouldn't have caused the process to crash, but would have completed the process whichever button you clicked.)

The buttons on the Userform only work as intended if you run the main macro and not the userform code.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #7  
Old 05-26-2017, 05:04 AM
cdc99999 cdc99999 is offline Transfer Outlook TextBox Text Into Email Body Windows 7 32bit Transfer Outlook TextBox Text Into Email Body Office 2013
Novice
Transfer Outlook TextBox Text Into Email Body
 
Join Date: May 2017
Location: Canada
Posts: 7
cdc99999 is on a distinguished road
Default

Thanks again gmayor, appreciate you time and help.

I feel bad posting again, don't mean to be a bother and I understand if there's no further follow up, more than enough was done to help out.

I did try to run the form via the main macro in Outlook only but it still gives me gives me that "Run-time error ‘13”: Type mismatch".

When I click "Debug" in brings me to the code and the line added:
- Commas show the text that the debug highlights in yellow: "If .Tag = 0 Then" GoTo lbl_Exit

Cheers
Reply With Quote
  #8  
Old 05-26-2017, 05:25 AM
gmayor's Avatar
gmayor gmayor is offline Transfer Outlook TextBox Text Into Email Body Windows 10 Transfer Outlook TextBox Text Into Email Body Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

That shouldn't happen, but you have shown
Code:
"If .Tag = 0 Then" GoTo lbl_Exit
There should be no quotes in that line?

Check VBA > Tools > References and see if you have the following checked.
Obviously if you have a different Office version the office numbered references will be different.

__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #9  
Old 05-26-2017, 05:34 AM
cdc99999 cdc99999 is offline Transfer Outlook TextBox Text Into Email Body Windows 7 32bit Transfer Outlook TextBox Text Into Email Body Office 2013
Novice
Transfer Outlook TextBox Text Into Email Body
 
Join Date: May 2017
Location: Canada
Posts: 7
cdc99999 is on a distinguished road
Default

Sorry, I wrote "commas" but should have wrote "quotes", the text in quotes is not in my code but what is highlighted in yellow from Debug. My code is as you provided, without quotes.

Will check References as suggested, thanks
Reply With Quote
  #10  
Old 05-26-2017, 05:41 AM
cdc99999 cdc99999 is offline Transfer Outlook TextBox Text Into Email Body Windows 7 32bit Transfer Outlook TextBox Text Into Email Body Office 2013
Novice
Transfer Outlook TextBox Text Into Email Body
 
Join Date: May 2017
Location: Canada
Posts: 7
cdc99999 is on a distinguished road
Default

Verified and added checkmark to "Microsoft Scripting Runtime" but still get the Run-time error ‘13”: Type mismatch.

Thanks again for your help, I will try it again from scratch, hopefully I missed something somewhere.


It's too bad, so damn close to it working... arghh!
Reply With Quote
  #11  
Old 05-26-2017, 06:12 AM
gmayor's Avatar
gmayor gmayor is offline Transfer Outlook TextBox Text Into Email Body Windows 10 Transfer Outlook TextBox Text Into Email Body Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,142
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

You can download the test files I used from http://www.gmayor.com/Forum/email_userform.zip

Extract the files from the zip. The template goes in your template folder. Import the userform and macro code to your VBA editor. It will create a new userform (frmExample) and add a module with code that works with the added userform. Delete your existing code and and it should work.

Reboot the PC before testing again.
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #12  
Old 05-26-2017, 07:28 AM
cdc99999 cdc99999 is offline Transfer Outlook TextBox Text Into Email Body Windows 7 32bit Transfer Outlook TextBox Text Into Email Body Office 2013
Novice
Transfer Outlook TextBox Text Into Email Body
 
Join Date: May 2017
Location: Canada
Posts: 7
cdc99999 is on a distinguished road
Default

ROCK ON!

Works like a charm, many thanks!
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Transfer Outlook TextBox Text Into Email Body Show attachments as text in body of email?? thewiseguy31 Outlook 1 07-21-2015 09:07 PM
Transfer Outlook TextBox Text Into Email Body move email based on text in email body megatronixs Outlook 3 02-10-2015 10:58 AM
Finding specific text within body of email Tammfran Outlook 0 03-14-2014 02:32 PM
add hyperlink in body of Outlook email ChuckDrago Word 0 06-27-2013 01:13 PM
Outlook not sending body of email sbertram Outlook 0 08-30-2012 11:50 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:50 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2025, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft