Sending email from VB

September 2nd, 2007

It's easy to send an email from VB. Create a new console application project in Visual Basic Express. Then paste this code in the Module1.vb file. The code demonstrates the basic steps in constructing a simple email message and sending it.

Imports System.Net.Mail

Module Module1

    Sub Main()
        Dim msg As New MailMessage
        Dim smtp As New SmtpClient

        'define smtp client parameters
        smtp.Host = "your.smtp.server"
        smtp.Port = 587
        smtp.Credentials = New Net.NetworkCredential("your_username", "your_password")

        'define the message
        msg.From = New MailAddress("your_email@server.com", "Your Name")
        msg.To.Add(New MailAddress("someone@somewhere.com", "Someone"))
        msg.Subject = "Test eMail"
        msg.Body = "This is a test email."

        'Send it!
        smtp.Send(msg)

    End Sub

End Module

Posted in Visual Basic | No Comments




Create a Master Sheet with Hyperlinks

August 11th, 2007

Sometimes it's a good idea to have a master worksheet that would have hyperlinks to the other sheets in the workbook. Here's a sub procedure that would create a worksheet named "Index" and create the hyperlinks.

Sub CreateIndexSheet()
    Dim ws As Worksheet
    Dim wsIndex As Worksheet
    Dim lRow As Long

    With ActiveWorkbook
        Set wsIndex = .Worksheets.Add(Before:=.Worksheets(1))
        wsIndex.Name = "Index"
    End With

    With wsIndex.Range("A1")
        .Value = "Index"
        .Font.Bold = True
    End With

    lRow = 2
    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Index" Then
            With wsIndex
                .Range("A" & lRow).Value = ws.Name
                .Hyperlinks.Add Anchor:=.Range("A" & lRow), Address:="", SubAddress:=ws.Name & "!A1"
                lRow = lRow + 1
            End With
        End If
    Next ws
    wsIndex.Columns("A").AutoFit
End Sub

Posted in Excel, How To, VBA | No Comments




Disable the Right-Click Menu in a Worksheet

July 22nd, 2007

If for some reason, you need to disable the right click or shortcut menu in a worksheet, then you can place the code below in the code module of a worksheet.

Private Sub Worksheet_BeforeRightClick(ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    MsgBox "Sorry, right-click menus are disabled!"

End Sub

Posted in Excel, How To, VBA | 2 Comments






« Previous PageNext Page »

(C) by Virgilio Adriano. All rights reserved. Powered by WordPress.
Entries and comments feeds.
It took 0.310 seconds to load this page.