Site hosted by Angelfire.com: Build your free website today!

Categories - Web/Internet

E-mail Creation Through VB.
Asynchronous Processing Between Client And ActiveX Server Component.
Strip attachments from email in Outlook.
Read text box and Launch Default Browser.
WinSock Control provides Internet chat functionality.
Accessing a control's properties, methods and events when used with HTML.
Display animated GIFs in VB.

E-mail Creation Through VB. Back To Top

Private Sub cmdSend_Click()

    Dim i As Integer, strSendString As String

    lblFeedback.Caption = "Establishing session..."
    DoEvents

    With Me.MAPISession1
        .UserName = strUserName
        .Password = strPassword
        .NewSession = False
        lblFeedback.Caption = "Signing on..."
        DoEvents
        .SignOn
    End With

    DoEvents

    With Me.MAPIMessages1
        .SessionID = Me.MAPISession1.SessionID
        lblFeedback.Caption = "Building message..."
        DoEvents
        .Compose
        .RecipAddress = txtSendTo.Text
        .MsgSubject = txtSubject
        .MsgNoteText = txtMessage
        .AttachmentIndex = 0
        .AttachmentPathName = Me.strFile
        .AttachmentName = FileComponent(Me.strFile)
        .AttachmentPosition = 0
        .AttachmentType = mapData
        lblFeedback.Caption = "Preparing to send..."
        DoEvents
        .Send
    End With

    DoEvents

    With Me.MAPISession1
        lblFeedback.Caption = "Signing off..."
        DoEvents
        .SignOff
    End With

End Sub

Otherwise, try the shellexecute method:-

strSendString = "mailto: " + txtSendTo.Text
strSendString = strSendString + "?Attach=" + Chr(34) + strFile + Chr(34)
strSendString = strSendString + "&Subject=" + txtSubject
strSendString = strSendString + "&Body=" + txtMessage

Call ShellExecute(Me.hWnd, "open", strSendString, vbNullString, vbNullString, 1)

Asynchronous Processing Between Client And ActiveX Server Component. Back To Top
You are developing an application that requires asynchronous processing between the client and ActiveX server component. What requirements would prompt you to use an Event mechanism in the component for notification?

A. The notification can be broadcast anonymously.
B. The order in which clients receive the notification is not important
C. The server must get control back after all clients have received the notification.
D. Optional arguments are required.

Answer: A, B & C

A component can notify clients using callbacks or events. Which one to use depends on the nature of the notification. Events are used when all of the following conditions exist:
1. The notification can be broadcast as a raised event to all rather than calling a procedure in the client object.
2. The order in which clients receive the notification can be random rather than directed one at a time to specific clients.
3. It is OK for the component to get control back after all clients have received the notification.
4. When the notification involves receiving back data via arguments, it is OK to receive back only the value set by the last client responding to the event.
5 The component does not need to if an error occurred in the client's event procedure. If any one of these conditions does not apply, then providing the notification using callback methods would be preferable.

Strip attachments from email in Outlook. Back To Top
To be used in 'stripping' attachments from e-mails in your inbox and save them to a hardcoded path. This bit of code will strip attachments and save them into the path '"C:\temp\Outlook Attachments", it has no error handling so be warned!

Place the following code under a command button:

Dim oApp As Outlook.Application
Dim oNameSpace As NameSpace
Dim oFolder As MAPIFolder
Dim oMailItem As Object
Dim sMessage As String

Set oApp = New Outlook.Application
Set oNameSpace = oApp.GetNamespace("MAPI")
Set oFolder = oNameSpace.GetDefaultFolder(olFolderInbox)

For Each oMailItem In oFolder.Items
    With oMailItem
        If oMailItem.Attachments.Count > 0 Then '?
           oMailItem.Attachments.Item(1).SaveAsFile "C:\Temp\Outlook Attachments\" _
               & oMailItem.Attachments.Item(1).filename
           MsgBox oMailItem.Attachments.Item(1).DisplayName & " was saved as " _
               & oMailItem.Attachments.Item(1).filename
        End If
    End With
Next oMailItem

Set oMailItem = Nothing
Set oFolder = Nothing
Set oNameSpace = Nothing
Set oApp = Nothing

Read text box and Launch Default Browser. Back To Top
Here's some code to read a text box and start the default browser to go to the site.

OBJECTS to create for this code:
- a text box named txtWeb with the text set to http://www.
- a button named cmdWeb

Place this in a module:

Dim success As Integer
Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hwnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _ 
    ByVal lpParameters As String, ByVal lpDirectory As String, ByVal nShowCmd As Long) As Long

Here's the button code:

Private Sub cmdWeb_Click()
    Dim success As Integer
    If Trim(txtWeb.Text) = "http://www." Or Trim(txtWeb.Text) = "" Then 
        ' I have the txtWeb.text set to http://www. 
        MsgBox "You don't have a web address entered for this entry", vbCritical, _ 
        "Missing Data"
    ElseIf Left(Trim(txtWeb.Text), 11) = "http://www." Then
        Site = Trim(txtWeb.Text)
    ElseIf Left(Trim(txtWeb.Text), 4) = "www." Then
        Site = "http://" & Trim(txtWeb.Text)
    Else
        Site = "http://www." & Trim(txtWeb.Text)
    End If
    success% = ShellToBrowser(Me, Site, 0)
End Sub

Here's the function code:

Function ShellToBrowser%(Frm As Form, ByVal URL$, ByVal WindowStyle%)
    
    Dim api%
    api% = ShellExecute(Frm.hwnd, "open", URL$, "", App.Path, WindowStyle%)
 
    'Check return value
    If api% <31 Then
        'error code - see api help for more info
        MsgBox App.Title & " had a problem running your web browser. & _
          "You should check that your browser is correctly installed." & _
          (Error" & Format$(api%) & ")", 48, "Browser Unavailable"
        ShellToBrowser% = False
    ElseIf api% = 32 Then
        'no file association
        MsgBox App.Title & " could not find a file association for " & _
          URL$ & " on your system. You should check that your browser" & _
          "is correctly installed and associated with this type of file.", 48, "Browser Unavailable"
        ShellToBrowser% = False
    Else
        'It worked!
        ShellToBrowser% = True
 
    End If
    
End Function

If you want to create an email address in a text box and start your email program, add this code and txtEmail and cmdEmail to the same page it uses the same function.

Here's the button code:

Private Sub cmdEmail_Click()
    Dim success As Integer
    If Trim(txtEmail.Text) = "" Then  ' Give an error if nothing is in the text box
        MsgBox "You don't have an email address entered for this entry", vbCritical, "Missing Data"
    Else
        Site = "mailto:" & Trim(txtEmail.Text)
        success% = ShellToBrowser(Me, Site, 0)
    End If
End Sub

WinSock Control provides Internet chat functionality. Back To Top
A WinSock control provides for connecting to remote machines and exchanging data. The communication can use either UDP or TCP. Use this control to:

· Create client applications that collects user information before sending it to a central server.
· Create server applications that functions as a central collection point for data from several users.
· Create a "chat" application.

Accessing a control's properties, methods and events when used with HTML. Back To Top
When using an ActiveX control with HTML, a VBScript <Script> tag is used to set the control's properties, invoke methods, and respond to the control's events.

Display animated GIFs in VB. Back To Top
While the Picture ActiveX control offers a great way to display graphics, it only shows the first image in an animated GIF. To display a fully animated GIF, without rebuilding the entire graphic frame by frame, you can use the WebBrowser control (just keep in mind that this control isn't available to machines without IE 3.0 or greater). To do so, select the Microsoft Internet Controls component. When you do, the WebBrowser control appears on Visual Basic's toolbar. Drop the control onto a form, then in the form's Load() event place the following code:

WebBrowser1.Navigate "C:\Internet\anim.gif"

Where the filespec points to a valid animated GIF path or URL. When you run the program, Visual Basic displays the GIF.

Unfortunately, the WebBrowser also displays a right-hand scroll bar--probably not what you want for a decorative image. Believe or not, you can turn this scrollbar off just like you would normally via HTML, as in:

WebBrowser1.Navigate "about:<html><body scroll='no'><img src='D:\Internet\anim.gif'></img></body></html>"

Now when you run the form, Visual Basic displays the image sans scrollbar.


This web site is purely for programmer reference and no copyright infringements were intended.