Want to add code to ThisOutlookSession, but other code already exists

NatDev

Member
Hi,




I want to add the VBA code mentioned in the first part of this link to my Outlook 2003: http://www.outlookcode.com/article.aspx?id=72




The problem that I am running into is that I already have some code in my ThisOutlookSession that I would like to keep and if I just plop this code in before the other code, this new code doesn't work. If I drop the code mentioned in the link above into an empty ThisOutlookSession on another machine, it works fine. I am probably missing some simple step, but do very little with VBA in Outlook and have been unable to find what I am looking for by Googling.




Here is the code that I already have in place. I believe it does something with the setup of my Outlook Contacts. Again, my goal is to have the code below continue to work plus the code in the first part of the link above:




Public Sub ChangeFileAs()
Dim objOL As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim objContact As Outlook.ContactItem
Dim objItems As Outlook.Items
Dim objContactsFolder As Outlook.MAPIFolder
Dim obj As Object
Dim strFirstName As String
Dim strLastName As String
Dim strCompanyName As String
Dim strFileAs As String



On Error Resume Next



Set objOL = CreateObject("Outlook.Application")
Set objNS = objOL.GetNamespace("MAPI")
Set objContactsFolder = objNS.GetDefaultFolder(olFolderContacts)
Set objItems = objContactsFolder.Items



For Each obj In objItems
'Test for contact and not distribution list
If obj.Class = olContact Then
Set objContact = obj



With objContact
strFirstName = .FirstName
strLastName = .LastName
strCompanyName = .CompanyName
strFileAs = strCompanyName & " " & "(" & strLastName & ", " & strFirstName & ")"
.FileAs = strFileAs
.Save
End With
End If



Err.Clear
Next



Set objOL = Nothing
Set objNS = Nothing
Set obj = Nothing
Set objContact = Nothing
Set objItems = Nothing
Set objContactsFolder = Nothing


End Sub




Any help would be appreciate.




Thanks,
 
K

Ken Slovak - [MVP - Outlook]

There's no reason at all why if you used method 1 at your link that it would

interfere with your running that macro to re-order your contact filing. Both

pieces of code can co-exist.

"NatDev" <NatDev.4aliny@invalid> wrote in message

news:NatDev.4aliny@invalid...

> Hi,

> I want to add the VBA code mentioned in the first part of this link to
> my Outlook 2003: http://www.outlookcode.com/article.aspx?id=72

> The problem that I am running into is that I already have some code in
> my ThisOutlookSession that I would like to keep and if I just plop this
> code in before the other code, this new code doesn't work. If I drop
> the code mentioned in the link above into an empty ThisOutlookSession on
> another machine, it works fine. I am probably missing some simple step,
> but do very little with VBA in Outlook and have been unable to find what
> I am looking for by Googling.

> Here is the code that I already have in place. I believe it does
> something with the setup of my Outlook Contacts. Again, my goal is to
> have the code below continue to work plus the code in the first part of
> the link above:

> Public Sub ChangeFileAs()
> Dim objOL As Outlook.Application
> Dim objNS As Outlook.NameSpace
> Dim objContact As Outlook.ContactItem
> Dim objItems As Outlook.Items
> Dim objContactsFolder As Outlook.MAPIFolder
> Dim obj As Object
> Dim strFirstName As String
> Dim strLastName As String
> Dim strCompanyName As String
> Dim strFileAs As String

> On Error Resume Next

> Set objOL = CreateObject("Outlook.Application")
> Set objNS = objOL.GetNamespace("MAPI")
> Set objContactsFolder = objNS.GetDefaultFolder(olFolderContacts)
> Set objItems = objContactsFolder.Items

> For Each obj In objItems
> 'Test for contact and not distribution list
> If obj.Class = olContact Then
> Set objContact = obj

> With objContact
> strFirstName = .FirstName
> strLastName = .LastName
> strCompanyName = .CompanyName
> strFileAs = strCompanyName & " " & "(" & strLastName &
> ", " & strFirstName & ")"
> FileAs = strFileAs
> Save
> End With
> End If

> Err.Clear
> Next

> Set objOL = Nothing
> Set objNS = Nothing
> Set obj = Nothing
> Set objContact = Nothing
> Set objItems = Nothing
> Set objContactsFolder = Nothing
> End Sub

> Any help would be appreciate.

> Thanks,

> > NatDev
> >
 
Top