Solved

Simple question, feel free to make fun. What's wrong with my VB.NET code here?

Posted on 2004-08-02
5
320 Views
Last Modified: 2010-05-18
Mkay, this is my application IN IT'S ENTIRETY right now. I just started to program again, so I'm tinkering with small chunks of the larger project I'm working towards.

I've built a small app that takes the string out of two textboxes, and assigns them to the FirstName and LastName fields in Outlook's contacts list.

It works the first time around, but when I try to type in a different name and then click the Add! button again, I get an error. I assume it's not making a new object the 2nd time around or somthing so it messes up. Any help would be appreciated.
0
Comment
Question by:Sootah
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 2

Author Comment

by:Sootah
ID: 11691371
Public Class Form1
    Dim oApp As Outlook.Application = New Outlook.Application()
    Dim oCt As Outlook.ContactItem = oApp.CreateItem(Outlook.OlItemType.olContactItem)
    Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        oCt = oApp.CreateItem(Outlook.OlItemType.olContactItem)
        oCt.FirstName = TxtFirstName.Text
        oCt.LastName = TxtLastName.Text
        oCt.Save()

        oApp = Nothing
        oCt = Nothing
    End Sub
End Class
0
 
LVL 2

Author Comment

by:Sootah
ID: 11691374
The 2nd oCt declaration was something I was tinkering with. It wasn't there before.
0
 

Expert Comment

by:Edeldragon
ID: 11691407
Sootah

I think the problem is that you are setting oApp = Nothing during the button click procedure. If you take out the oApp=Nothing line, then your subroutine should work repeatedly.

HTH

ED
0
 
LVL 8

Accepted Solution

by:
rfgkev earned 250 total points
ID: 11691871
put the 2 lines
    Dim oApp As Outlook.Application = New Outlook.Application()
    Dim oCt As Outlook.ContactItem = oApp.CreateItem(Outlook.OlItemType.olContactItem)

inside your button_click method
0
 
LVL 21

Expert Comment

by:tovvenki
ID: 11691908
Hi Sootach,
ED is correct you should either remove that line or instantaite the objects within the event handler like
Public Class Form1
        Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
        Dim oApp As Outlook.Application = New Outlook.Application()
        Dim oCt As Outlook.ContactItem = oApp.CreateItem(Outlook.OlItemType.olContactItem)

        oCt = oApp.CreateItem(Outlook.OlItemType.olContactItem)
        oCt.FirstName = TxtFirstName.Text
        oCt.LastName = TxtLastName.Text
        oCt.Save()

        oApp = Nothing
        oCt = Nothing
    End Sub
End Class

or


Public Class Form1
       Dim oApp As Outlook.Application = New Outlook.Application()
        Dim oCt As Outlook.ContactItem = oApp.CreateItem(Outlook.OlItemType.olContactItem)

        Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
       
        oCt = oApp.CreateItem(Outlook.OlItemType.olContactItem)
        oCt.FirstName = TxtFirstName.Text
        oCt.LastName = TxtLastName.Text
        oCt.Save()

    End Sub
    when exiting the form call the following
            oApp = Nothing
        oCt = Nothing

   
End Class


Hope that this helps you

Regards,
Venki
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question