Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘object’

How to use the Microsoft Outlook Object Library to force a Send/Receive action by using Visual Basic .NET

Symptoms
This article describes how to use the Microsoft Outlook 2002 Object Library or the Microsoft Office Outlook 2003 Object Library to force a Send/Receive action by using Microsoft Visual Basic .NET.
Resolution
To use the Outlook 2002 Object Library or the Outlook 2003 Object Library to force a Send/Receive action in a Visual Basic .NET project, follow these steps: Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects.Under Templates, click Console Application, and then click OK.
By default, Module1.vb is created.Add a reference to either the Outlook 2002 Object Library or the Outlook 2003 Object Library: On the Project menu, click Add Reference.On the COM tab, click Microsoft Outlook 11.0 Object Library if you are using Outlook 2003, or click Microsoft Outlook 10.0 Object Library if you are using Outlook 2002, and then click Select. In the Add References dialog box, click OK to accept your selections. If you receive a prompt to generate wrappers for the libraries that you selected, click Yes.In the code window, replace all the code with:

‘TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line.’Imports Outlook = Microsoft.Office.Interop.OutlookModule Module1Sub Main()’ Create an Outlook application.Dim oApp As Outlook._Application = New Outlook.Application’ Create the name space.Dim oNS As Outlook._NameSpace = oApp.GetNamespace(“mapi”)Dim oSyncs As Outlook.SyncObjectsDim oSync As Outlook.SyncObjectTry’ Reference SyncObjects.oSyncs = oNS.SyncObjectsoSync = oSyncs.Item(“All Accounts”)’ Send and receive.oSync.Start()Catch ex As ExceptionConsole.WriteLine(ex.Message)End Try’ Clean up.oSync = NothingoSyncs = NothingoNS = NothingoApp = NothingEnd SubEnd ModulePress F5 to build and run the program.

How To Implement Visual Basic COM Objects Returning Recordsets

Symptoms
This article describes, by example, how to implement a Visual Basic Component Object Model (COM) Object that returns a recordset to Active Server Pages (ASP).
Implementing this incorrectly can result in memory leaks or one of the following errors:

The operation requested by the application is not allowed if the object is closed. -or-

Type Mismatch -or-

error ‘ASP 0115′ – A trappable error occured in an external object
Resolution
Use the following steps to implement a method that returns a recordset from a Visual Basic COM Object to Active Server Pages: Create a Visual Basic ActiveX DLL project called PassRsPrj.Add a class module and change its name to PassRsObj.Implement the following method:
Note You must change the UID and the PWD values to the ones that are used on your system.

Public Function TestRs() as ADODB.RecordsetDim rsObj As ADODB.RecordsetDim cnObj As ADODB.Connectionset rsObj = New ADODB.Recordsetset cnObj = New ADODB.Connection’Open connection to databasecnObj.Open(“DSN=pubs;uid=<username>;pwd=<strong password>”)’To use disconnected Recordset you must use client side cursorsrsObj.CursorLocation = adUseClientrsObj.Open “select * from authors”, cnObj, adOpenKeyset, _adLockOptimistic, adCmdText’Disconnected recordsetSet rsObj.ActiveConnection = Nothing’Set return valueSet Testrs = rsObj’Clean up resourcescnObj.CloseSet cnObj = NothingEnd Function Create an ASP page with the following code:

<%Dim rsTest, oTestPassRsSet oTestPassRs = Server.CreateObject(“PassRsPrj.PassRsObj”)Set rsTest = oTestPassRs.TestRs()DoResponse.Write ( “Value in Record = ” & rsTest(1) & “<BR>” )rsTest.MoveNextLoop until rsTest.EOFrsTest.CloseSet rsTest = NothingSet oTestPassRs = Nothing%>

FIX: Error When Closing a Program Through the Control Box

Symptoms
An application error occurs when you close a Visual Basic program throughthe control box if the Visual Basic program executes a command after anobject created from a class module is set to nothing. This problem occursonly in the 32-bit edition of Visual Basic 4.0.
Resolution
The workaround to this issue is execute all the code in the event beforesetting the object to nothing. The statement to set the object to nothingshould be the last line in the event procedure.