web 2.0


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
 

Copying and Moving files using VBA

This tip shows how to use VBA to copy of move files in Windows directories. You can apply this tip to any of the office suite (Word, Excel, Outlook, Access, etc).

In order for this tip to work using the code below, you need to ensure that the references to Windows Script Host Object Model are installed.

COPY FILE

Sub CopyFileToAnotherLocation()

    Dim FSObj       As FileSystemObject
    Dim answer      As Integer

    Set FSObj = New FileSystemObject

    If FSObj.FileExists("C:\New Location\Filename.txt") Then
        answer = MsgBox("File already exists in this location. " _
            & "Are you sure you want to continue? If you continue " _
            & "the file at destination will be deletedo!", _
            vbInformation + vbYesNo)

             If Not answer = vbYes Then Exit Sub
    End If

    FSObj.CopyFile "C:\Old Location\Filename.txt", _
        "C:\New Location\Filename.txt", True

End Sub


MOVE FILE

Sub MoveFileToAnotherLocation() 

    Dim FSObj       As FileSystemObject
    Dim answer      As Integer 

    Set FSObj = New FileSystemObject 

    If FSObj.FileExists("C:\New Location\Filename.txt") Then
        answer = MsgBox("File already exists in this location. " _
            & "Are you sure you want to continue? If you continue " _
            & "the file at destination will be deletedo!", _
            vbInformation + vbYesNo) 

             If Not answer = vbYes Then Exit Sub
    End If 

    On Error Resume Next
    Kill "C:\New Location\Filename.txt" 

    FSObj.MoveFile "C:\Old Location\Filename.txt", _
        "C:\New Location\Filename.txt"

End Sub

Tags: , , , , , ,

Microsoft Office | Microsoft Office - VBA

Delete Windows XP/Vista Folder using VBA

Have you ever needed to delete a folder and its contents using VBA? This tip shows you how this can be done. The first thing you will need to do is to install the references to the Windows Script Host Model. To do so, open VBE (Alt+F11); then click on Tools --> References. Look for Windows Script Host Model select it and install it.

Then you can use the following code to get the job done:

Sub TestFunction()
   MsgBox DeleteFolder("C:\TestFolder")
End Sub

Function DeleteFolder(ByVal strFolderPath As String) As Boolean
   Dim fsoObj As New FileSystemObject
   Dim delFolder As Folder

   On Error Resume Next
   Set delFolder = fsoObj.GetFolder(strFolderPath)

   If delFolder Is Nothing Then GoTo ExitFunction

   delFolder.Delete True
   DeleteFolder = True

ExitFunction:
   Set fsoObj =
Nothing
End Function

Tags: , , , , , , , ,

Microsoft Office | Microsoft Office - VBA