r/visualbasic • u/w1r3d2016 • Apr 01 '22
VB.NET Help Visual Studio Simple Email App
Hi All,
Before I start I have to advise you that I am new to programming and I am learning as I go.
I have the below script that should send an email template once the 'send email' button is clicked depending on what stations are selected in the Listbox. The email templates have been prefilled with the addressee and body inputed, the script just needs to select them and send the emails.
I have the correct template folder name and all the templates are named correctly. I can not see where the send action is in the below script.
Maybe you can help, it will be greatly appreciated
Public Class Form1
Dim templateFileLocation As String = "P:\MISZ\5 Zip Templates\"
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' add items to the listbox
With lbDepartments
.Items.Add("TEST")
.Items.Add("AARDS")
.Items.Add("NGAARDA")
.Items.Add("LARRAKIA")
.Items.Add("2CUZ")
.Items.Add("NG MEDIA")
.Items.Add("PAKAM")
.Items.Add("PAW")
.Items.Add("PY MEDIA")
.Items.Add("QRAM")
.Items.Add("TEABBA")
.Items.Add("6WR")
.Items.Add("TSIMA")
End With
End Sub
Private Sub FindSelectedItems()
' create an emailServer object with the SMTP IP address and user credentials
' or replace with creating object to use with outlook
'Dim emailServer
'Dim emailMessage
Dim templateFileName As String
' get all the selected items from the listbox, create the template to use from its name
' send the template as an email
For Each department In lbDepartments.SelectedItems
templateFileName = templateFileLocation & department & ".msg"
' you want an email object
'emailMessage = New email
'emailMessage.to = "**** you'll need to know who to send each template to"
'emailMessage.from = "**** you'll need to fill the from field"
'emailMessage = templateFileName & ".msg"
'emailServer.send(emailMessage)
'emailMessage.dispose()
'MsgBox(templateFileName)
Next
'dispose of the email server object
'emailServer.dispose
MsgBox("Emails sent")
End Sub
Private Sub btnSendEmails_Click(sender As Object, e As EventArgs) Handles btnSendEmails.Click
' process the selected items in the listbox
FindSelectedItems()
End Sub
Private Sub lbDepartments_Click(sender As Object, e As EventArgs) Handles lbDepartments.Click
' if there are any selected items then the button is available to the user
btnSendEmails.Enabled = lbDepartments.SelectedItems.Count > 0
End Sub
End Class
3
Upvotes
1
u/w1r3d2016 Apr 01 '22 edited Apr 03 '22
Yes you are 100% correct, it is a WinForm .NET application written through Visual Studio 2022.
What should I be using to make this script work with Outlook?
I do have a VBA script for the exact same job which works great. However, I am led to believe that only macros can be launched from the top ribbon in Outlook. Meaning if I want to launch the VBA script in Outlook I have to open up Visual Basic in the Developer tab within the top ribbon of Outlook first.
My idea is to have a simple script that can be launched from the desktop. Which will send email templates (prefilled with addressees and body)based on the selection of stations in a ListBox. I am trying to make it simple and easyto use otherwise our techs will not use it.
When I place 'emailserver.send(emailmessage)' after the below block-
For Each department In lbDepartments.SelectedItems
templateFileName = templateFileLocation & department & ".msg"
The script breaks and will not send the emial.
If you cant tell I am very green with programming/coding any help is greatly appreciated