[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 324
  • Last Modified:

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

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
Sootah
Asked:
Sootah
1 Solution
 
SootahAuthor Commented:
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
 
SootahAuthor Commented:
The 2nd oCt declaration was something I was tinkering with. It wasn't there before.
0
 
EdeldragonCommented:
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
 
rfgkevCommented:
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
 
tovvenkiCommented:
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now