web 2.0


How to: VBA email validation

This is a follow up to my post on sending email messages using Windows Vista. You can read the previous post here: http://www.msofficegurus.com/post/How-to-Windows-Vista-SMTP-Server-Using-CDOMessage.aspx

In this post, I discuss email validation using VBA. VBA email validation is a very simple process that does not require much, but you will need to install the Microsoft VBScript Regular Expressions 5.5.

In order to install the reference, follow these steps:

1. Open VBE (Alt+F11)
2. Go to “Tools -->
References…”
3. Search in the reference list for Microsoft VBScript Regular Expressions 5.5, select it and click OK to continue.

The figure below shows the reference installed:


Figure 1: Microsoft VBScript Regular Expressions 5.5 reference installed

NOTE:

                 

Only today, after receiving a comment on this post, I noticed that I was using Excel in Portuguese and not English. Since I am always changing the language settings, I sometimes forget to change back to the language I am writing on. I will leave the image as it is, as I am sure everyone can get the picture (if you will forgive me the pun).

Added: July 3rd, 2009


Now, you can create a boolean user-defined function to test an email string to check whether it is a “regular expression”. In my case, I check for the following pattern:

\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*

There are a number of patterns that you can you and you should be able to find my patterns, especially in sites that teach java or php (check RFC 2822 for info on pattern). You can then borrow one of those patterns and use in your code.

The user-defined function will look like this:

Function ValidateEmail(ByVal sEmail As String) As Boolean
 
    Dim oRegularExpression     As RegExp
 
'   Sets the regular expression object
    Set oRegularExpression = New RegExp

    With oRegularExpression
'   Sets the regular expression pattern
        .Pattern = "\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"
 
'   Ignores case
        .IgnoreCase = True

'       Test email string
        ValidateEmail = .Test(sEmail)
    End With
   
End Function


You can now test the user-defined function with a simple call:

Sub Test_ValidateEmail()
    MsgBox ValidateEmail("my.email@hotmail.com")
    MsgBox ValidateEmail("your.EMAIL@hotmail,com")
End Sub

Tags: , , , ,

Microsoft Excel - VBA | Microsoft Office - VBA | Vista

How to: Office 2007 CustomUI – getVisible attribute

When working with the Office 2007 CustomUI, you may want to toggle visibility of certain tabs or groups, for example. You accomplish this by using the getVisible attribute of the XML UI code and then wrapping it with a callback to handle the visibility.

In this article, I show you how to toggle the Office 2007 CustomUI visibility for a whole group and a group depending on the active worksheet (I will use Excel as the basis for this example). To begin with, we need to add the XML code to our XLSM file:

<customUI xmlns=
"http://schemas.microsoft.com/office/2006/01/customui" onLoad="rx_onLoad">
 <
ribbon
>
  <
tabs
>
   <
tab idMso="TabHome" label="My Home Tab" getVisible="rx_getVisibleTab"
>
    <
group idMso="GroupClipboard" getVisible="rx_getVisibleGroupClipboard"
/>
   </
tab
>
  </
tabs
>
 </
ribbon>

</customUI>

Notice that we have the following callbacks to generate:

·         rx_onLoad

·         rx_getVisibleTab

·         rx_getVisibleGroupClipboard


The first callback handles the setting of our ribbon object and the default values for the tab and group visibility. The second and third callbacks simply pick up the visibility boolean value and return it as the object’s visibility status (either the entire Home tab or the clipboard group or both).

Next, you should add a module where the following code should be added:

Option Explicit

Public rxMyRibbon                                       As IRibbonUI
Public rxblnTabHomeVisible                              As Boolean
Public rxblnGroupClipboardVisible                       As Boolean

Sub rx_onLoad(ribbon As IRibbonUI)
    Set rxMyRibbon = ribbon
    rxblnTabHomeVisible = True
    rxblnGroupClipboardVisible = True
End Sub

'Callback for TabHome getVisible
Sub rx_getVisibleTab(control As IRibbonControl, ByRef returnedVal)
    returnedVal = rxblnTabHomeVisible
End Sub

'Callback for GroupClipboard getVisible
Sub rx_getVisibleGroupClipboard(control As IRibbonControl, ByRef returnedVal)
    returnedVal = rxblnGroupClipboardVisible
End Sub


Finally, the visibility will depend on which sheet is active, thus in the code module for ThisWorkbook, you should add the following code:

Option Explicit
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Dim shIndex                    As Integer
   
    shIndex = Sh.Index
   
    Select Case shIndex
        Case 2
            rxblnTabHomeVisible = True
            rxblnGroupClipboardVisible = False
        Case 3
            rxblnTabHomeVisible = False
        Case Else
            rxblnTabHomeVisible = True
            rxblnGroupClipboardVisible = True
    End Select
   
    rxMyRibbon.Invalidate
End Sub


In the above scenario, if the active sheet has an index equal 2 (two); then the tab should be visible but not the clipboard group. When the index is 3 (three) then the whole tab should be invisible. Otherwise, all should be visible.

You can download the sample file here:
www.msofficegurus.com/uploads/getVisible.xlsm.

For further information on the Ribbon, you can this book:

Tags: , , ,

Microsoft Excel - VBA | Microsoft Office | Microsoft Office - VBA | Ribbon

How to append text files using VBA

If you are working with text files and need to merge or append data; here’s a good way to carry that out using VBA.

In this simple example, you should have two text files placed in the same location where the Excel workbook is located (notice how the file is opened in the code). In file number 2 (I called it “File2.txt”) you should write something (one or two lines of text for testing purposes). File number 1 you can leave blank, as data from file 2 will be appended to it. 

You are now ready. Copy the VBA code below and paste it into your workbook. You can now run it: 

Sub AppendTextFiles()

'   The file that contains the data to be copied over
    Open ThisWorkbook.Path & "\File2.txt" For Input As #2


'   The file where the data will be appended
    Open ThisWorkbook.Path & "\File1.txt" For Append As #1


'   Loop through the data in file number 2 (#2) ...
    Do Until EOF(2)
        Line Input #2, Data


'       ... and append (write) the data into file number 1 (#1)
        Write #1, Data
    Loop


'   Close the files
    Close #2
    Close #1
End Sub
 
  

 

Tags: ,

Microsoft Office | Microsoft Office - VBA

Sending e-mail using WinXP Pro SMTP service

This short tip shows how to use Windows SMTP service to send an e-mail. This will stop the annoying message from Outlook saying someone is trying to access its resources. It applies to MS Office as a whole…

Sub sendMail()

    Dim email As Object 

    Msg = "This is a test message using WinXP Pro SMTP service..."

    Set email = CreateObject("CDO.Message")

    With email
        .AddAttachment ActiveWorkbook.Path & "\fileName.xls"
        .From = "someone@somewhere.com"
        .To = "anyone@anywhere.com"
        '.CC = "noone@nowhere.com"
        '.BCC = "
joe.blog@joeyblogs.au"
        .Subject = "This message was generate on " & Date
        .TextBody = Msg
        .Send
    End With

    Set email = Nothing

End Sub