http://www.mjtnet.com/usergroup/viewtop ... ok+contact
My Script pulls the contact data from a screen and then runs the script below. It checks to see if an existing contact exists in Outlook and then adds the new contact. it works; however, a few minutes later Outlook cannot be opened. When I look in the Task Manager it is still running. If I close Outlook in the Task Manager and then open it again Outlook runs fine and the new Contact is in it. It is as if Macro Scheduler or my code (I'm sure it is my code) does not stop connecting to Outlook and then it causes it to lock up.
Could someone show me where my error is.
Thanks
[/code]
VBSTART
Function FindContact (strInput)
Dim objOutlook
Dim objNameSpace
Dim objFolder
Dim cItem
Dim strOutput
Dim counter
Const olFolderContacts = 10
Const cTextCaseInsensitive = 1
Set objOutlook = CreateObject("Outlook.application")
Set objNameSpace = objOutlook.GetNameSpace("MAPI")
Set objFolder = objNameSpace.GetDefaultFolder(olFolderContacts)
counter = 0
For Each cItem in objFolder.Items
If Instr(1,cItem.FullName,strInput,cTextCaseInsensitive) > 0 Then
counter=counter+1
FindContact = String(45,"~")
' FindContact = String(45,"~") & vbCrLf
FindContact = strOutput & "Full Name:" & vbTab & cItem.FullName & vbCrLf
FindContact = strOutput & strAddress & vbCrLf
FindContact = counter
'Msgbox strOutput, , cItem.Subject
End If
Next
If FindContact = "" Then
'Msgbox "No contacts match search requirements."& vbCrLf & counter, vbInformation,"Contact Search"
End If
' **** Clean up
'
Set objFolder = Nothing
Set objNameSpace = Nothing
set objOutlook = Nothing
END Function
VBEND
VBEval>FindContact("%FullName%"),answer
/MDL>%answer%
CloseDialog>Dialog3
If>%answer%=0
Goto>CreateContact
Endif
If>%answer%=1
MDL>There is already a Contact in Outlook for %FullName% the Program will now Exit
Goto>EOF
Endif
If>%answer%>1
MDL>There is more than one Contact in Outlook for %FullName% the Program will now Exit
Goto>EOF
Endif
Label>CreateContact
VBSTART
Sub AddContact (FullName,EMail,BusinessPhone,HomePhone,MobilePhone,Fax,StreetAddress,City,State,Zip)
Dim objOutlook
Dim itmContact
Dim strMsg
Const olContactItem = 2
Set objOutlook = CreateObject("Outlook.application")
Set itmContact = objOutlook.CreateItem(olContactItem)
itmContact.FullName = FullName
itmContact.EMail1Address = EMail
itmContact.BusinessTelephoneNumber = BusinessPhone
itmContact.HomeTelephoneNumber = HomePhone
itmContact.MobileTelephoneNumber = CellPhone
itmContact.BusinessFaxNumber = Fax
itmContact.HomeAddressStreet = StreetAddress
itmContact.HomeAddressCity = City
itmContact.HomeAddressState = State
itmContact.HomeAddressPostalCode = Zip
'Save and display the Contact
itmContact.Save
itmContact.Display
'Clean up
Set itmContact = Nothing
Set objOutlook = Nothing
End Sub
VBEND
VBRUN>AddContact,%FullName%,%EMail%,%BusinessPhone%,%HomePhone%,%CellPhone%,%Fax%,%StreetAddress%,%City%,%State%,%Zip%
Label>EOF
Code: Select all