itm in itms folder question.

C

Carol G

I am wishing to capture the itm from the collection when it is found and
then overwrite the info using with itm.
If it is not found then I wish to set a new itm and add the info.
As the code to add or overwrite is the same I was hoping to just have it
written once.
I think it won't work this way because once I'm out of the for loop my itm
is gone.
I am very unsure of all this.
Thanks for any suggestions.
Carol

If strDecision = "0" Then
For Each itm In itms
If strNameList = itm.FullName Then
Debug.Print itm.FullName & " is found"
End If 'strNameList = itm.FullName
Exit For
Next
with itm **** Here is the problem***

Else ' if decision is new
Set itm = itms.Add

end if
'Then code for add ing record.
'Standard Contact fields
.CustomerID = strCustomerID
.FirstName = strFName1
.LastName = strLName1
.CompanyName = strCompName1
.User4 = strCompName2
....
 
S

Sue Mosher [MVP-Outlook]

You are missing any code to test whether itm really was found:

If Not itm Is Nothing Then
' update it
Else
'create it
End If

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
C

Carol G

Sorry, Actually I had some code before this that checked that it is there to
find.
My problem is that if I place the With itm inside the for loop. When the
loop closes do I lose my itm.FullName which I wish to use out of the loop
with "With itm"? Hope that my question is making sense.
I just recently bought your book and am finding it helpful but I have a long
way to go.
Thanks

You are missing any code to test whether itm really was found:

If Not itm Is Nothing Then
' update it
Else
'create it
End If

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
S

Sue Mosher [MVP-Outlook]

Sorry, but it doesn't make sense. The itm object should be usable outside the loop. You do have a Dim itm statement in your procedure, right?
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
C

Carol G

OK here goes,

Here's my code ,don't laugh.
This is done in Access to transfer to OUtlook.
My problem is that I thought once the for loop closed that the itm.FullName
that I matched would close also.Down where the With itm code is placed is
where I'm having Trouble. I originally had the with itms code inside the
If strDecision = "0" Then ' User wishes to to Overwrite an itm. loop.

I then got an if without else error message.

When I moved it out of the loop the message went away but I don't think the
code works the way I have it.

Thanks

Again Carol



Private Sub cmdOutlookExport_Click()



'Declare the pfld variable as Public so it can be set in one procedure

'and used in another



Dim fld As Outlook.MAPIFolder

Dim appOutlook As Outlook.Application

Dim nms As Outlook.NameSpace

Dim itms As Outlook.Items

Dim itm As Outlook.ContactItem



Set appOutlook = CreateObject("Outlook.Application")

Set nms = appOutlook.GetNamespace("MAPI")

Set fld = nms.GetDefaultFolder(olFolderContacts) 'Set what kind of folder

Set itms = fld.Items 'Outlook folder item



On Error GoTo ErrorHandler



Dim strCustomerID As String

Dim strFName1 As String

Dim … All the rest of the variables that I wish to transfer to.





strCustomerID = Parent!txtCustomerID

strFName1 = FirstName_Concatenate_For_Outlook()

… assign all the other variables here



'Check Outlook for duplicates



For Each itm In itms



If strLName1 = itm.LastName Then

strNameList = strNameList & ";" & itm.FullName



End If

Next

If strNameList = "" Then

MsgBox "Looped through list" & strNameList & " is strNameList"

End If



If strNameList <> "" Then

DoCmd.OpenForm "frmDuplicateValue", OpenArgs:=strNameList,
WindowMode:=acDialog

'Stop here and wait until form goes away

'MsgBox "This should only be triggered if duplicate value found"



If Not IsLoaded("frmDuplicateValue") Then

MsgBox "Update has been canceled."

Exit Sub

End If 'Not Is Loaded



strDecision = Forms("frmDuplicateValue").Tag



DoCmd.Close acForm, "frmDuplicateValue"

End If 'strNameList<>""

'Separate the option from the FullName



strNameList = Mid(strDecision, 2)

strDecision = Replace(strDecision, Mid(strDecision, 2), "")



If strDecision <> "N" And strDecision <> "0" Then

‘ This means that the user wishes to update the information rather than
make new or overwrite.



For Each itm In itms

'Debug.Print itm.FullName



If strNameList = itm.FullName Then

Debug.Print itm.FullName & " is found"

With itm



If Len(.CustomerID) = 0 Then .CustomerID = strCustomerID

If Len(.FirstName) = 0 Then .FirstName = strFName1

… All the other fields to update here.



.Save

End With



MsgBox "Contact Updated Successfully"

Exit Sub



End If 'strNameList = itm.FullName

Next

End If 'strDecision <> "N" And strDecision <> "0"





If strDecision = "0" Then ' User wishes to to Overwrite an itm.



For Each itm In itms

If strNameList = itm.FullName Then



End If 'strNameList = itm.FullName

Exit For

Next

With itm ' This is where I want to use either this with itm to
overwrite or the one following to add.



Else ' if decision is new



Set itm = itms.Add

With itm

End If 'strDecision = "0"



'Write values from variables to fields in the new Contact item



'Standard Contact fields

.CustomerID = strCustomerID

.FirstName = strFName1

… All the other fields to deal with…

.Close (olSave)



End With

MsgBox " Contact exported! from Form"







ErrorHandlerExit:

Exit Sub



ErrorHandler:

MsgBox "Error No: " & Err.Number & "; Description: " & Err.Description

Resume ErrorHandlerExit



End Sub
 
S

Sue Mosher [MVP-Outlook]

These two statements are in the wrong order:

With itm

End If 'strDecision = "0"
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
C

Carol G

Thanks
I haven't time to try it out until later.
Carol PS thanks for not laughing

These two statements are in the wrong order:

With itm

End If 'strDecision = "0"
--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top