VBA - Userform - dataform into excel attachment

zardia

New Member
Joined
Aug 7, 2019
Messages
9
Hi all,

Wandering if anyone can point me in the right direction.

I created a vb userform for people to input data.
It is basically a bunch of fields, then the vba macro will create an email with some pre-defined settings, and then put the data in the email as a table and then send to pre-defined email address.

But now what I want to do is that instead of creating a table, I want the data to be stored in an excel, and send that excel as attachment.
Anyone can advice on how to do this? (ideally no need to create / save a temporary workbook)
Thanks in advance.

Below is my original code


VBA Code:
Private Sub CommandButton1_Click()

Dim OL As Object, MailSendItem As Object
Set OL = CreateObject("Outlook.Application")
Set MailSendItem = OL.CreateItem(plMailItem)

With MailSendItem

.Subject = "(Confidential) Request"
.to = [EMAIL]xxxx@xxxx.com[/EMAIL]
.cc = ccemail.Value

.htmlbody = "<html><body>" _
& " From: " & ddlBr.SelText _
& " <br>CC: " & ccemail.Value _
& " <br>Contact Person: " & ContactName.Value _
& " <br>Contact Number: " & ContactNo.Value _
& " <br><br><br><table border=1 cellspacing=0 cellpadding=0><tr><td width=130><p align=center>Date of Document (From)</td><td width=130><p align=center>Date of Document (To)</td><td width=150><p align=center>A/C Name</td><td width=110><p align=center>A/C No.</td><td width=150><p align=center>Document Type</td><td width=150><p align=center>Delivery method</td><td width=150><p align=center>Particulars</td></tr>" _
& " <tr><td><p align=center>" & Trim(date1.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto1.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname1.Value) & "&nbsp;</td><td><p align=center>" & accno1.Value & "&nbsp;</td><td><p align=center>" & ComboBox1.SelText & "&nbsp;</td><td><p align=center>" & ComboBox9.SelText & "&nbsp;</td><td><p align=center>" & part1.Value & "&nbsp;</td></tr>" _
& " <tr><td><p align=center>" & Trim(date2.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto2.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname2.Value) & "&nbsp;</td><td><p align=center>" & accno2.Value & "&nbsp;</td><td><p align=center>" & ComboBox2.SelText & "&nbsp;</td><td><p align=center>" & ComboBox10.SelText & "&nbsp;</td><td><p align=center>" & part2.Value & "&nbsp;</td></tr>" _
& " <tr><td><p align=center>" & Trim(date3.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto3.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname3.Value) & "&nbsp;</td><td><p align=center>" & accno3.Value & "&nbsp;</td><td><p align=center>" & ComboBox3.SelText & "&nbsp;</td><td><p align=center>" & ComboBox11.SelText & "&nbsp;</td><td><p align=center>" & part3.Value & "&nbsp;</td></tr>" _
& " <tr><td><p align=center>" & Trim(date4.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto4.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname4.Value) & "&nbsp;</td><td><p align=center>" & accno4.Value & "&nbsp;</td><td><p align=center>" & ComboBox4.SelText & "&nbsp;</td><td><p align=center>" & ComboBox12.SelText & "&nbsp;</td><td><p align=center>" & part4.Value & "&nbsp;</td></tr>" _
& " <tr><td><p align=center>" & Trim(date5.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto5.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname5.Value) & "&nbsp;</td><td><p align=center>" & accno5.Value & "&nbsp;</td><td><p align=center>" & ComboBox5.SelText & "&nbsp;</td><td><p align=center>" & ComboBox13.SelText & "&nbsp;</td><td><p align=center>" & part5.Value & "&nbsp;</td></tr>" _
& " <tr><td><p align=center>" & Trim(date6.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto6.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname6.Value) & "&nbsp;</td><td><p align=center>" & accno6.Value & "&nbsp;</td><td><p align=center>" & ComboBox6.SelText & "&nbsp;</td><td><p align=center>" & ComboBox14.SelText & "&nbsp;</td><td><p align=center>" & part6.Value & "&nbsp;</td></tr>" _
& " <tr><td><p align=center>" & Trim(date7.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto7.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname7.Value) & "&nbsp;</td><td><p align=center>" & accno7.Value & "&nbsp;</td><td><p align=center>" & ComboBox7.SelText & "&nbsp;</td><td><p align=center>" & ComboBox15.SelText & "&nbsp;</td><td><p align=center>" & part7.Value & "&nbsp;</td></tr>" _
& " <tr><td><p align=center>" & Trim(date8.Value) & "&nbsp;</td><td><p align=center>" & Trim(dateto8.Value) & "&nbsp;</td><td><p align=center>" & Trim(accname8.Value) & "&nbsp;</td><td><p align=center>" & accno8.Value & "&nbsp;</td><td><p align=center>" & ComboBox8.SelText & "&nbsp;</td><td><p align=center>" & ComboBox16.SelText & "&nbsp;</td><td><p align=center>" & part8.Value & "&nbsp;</td></tr>" _
& " </table>" _
& " <BR><table border=1 cellspacing=0 cellpadding=0><td colspan=7 width=1000><p align=left> Details of Document </td></tr>" _
& " <tr><td width=200><p align=center>Teller ID</td><td width=200><p align=center>Txn Br</td><td width=200><p align=center>Txn Amount</td><td width=200><p align=center>Cheque No.</td><td width=200><p align=center>Date of A/C Closure</td></tr>" _
& " <tr><td><p align=center>" & Trim(Textteller.Value) & "&nbsp;</td><td><p align=center>" & Trim(Textbr.Value) & "&nbsp;</td><td><p align=center>" & Textamt.Value & "&nbsp;</td><td><p align=center>" & Textcq.Value & "&nbsp;</td><td><p align=center>" & Textclosedate.Value & "&nbsp;</td></tr>" _
& " </table>" _
& " <br><br><table border=1 cellspacing=0 cellpadding=0><tr><td width=250><p align=center>Ref No.</td><td width=250><p align=center>Date Processed</td><td width=250><p align=center>Processed By</td><td width=250><p align=center>Checked by</td></tr>" _
& " <tr><td width=250 height=50><p align=left>.<p align=left></td><td width=250><p align=left>.</td><td width=250><p align=left>.</td><td width=250><p align=left>.</td></tr>" _
& " </table>" _
& " <BR><BR><BR>" _
& " [Confidential] "

       .send

    End With


MsgBox "Request sent successfully! A copy of this request can be found in your Outlook's Sent Items."

    Unload Me

End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result

Forum statistics

Threads
1,223,885
Messages
6,175,183
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top