Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / October 2005

Tip: Looking for answers? Try searching our database.

VBA Sending Contact from Word to Outlook:  Folder Specific Question

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
audrie magno - 26 Sep 2005 03:13 GMT
In the code below, form field data is leaving Word and going to an Outlook
contact list. Everything works EXCEPT it puts the new contact in my default
contact folder instead of the custom folder... can anyone identify what I
did wrong?

Public Sub AddContact()
  Dim objOutlook As New Outlook.Application

  Dim myNameSpace As NameSpace
  Set myNameSpace = objOutlook.GetNamespace("MAPI")
  Dim oOlFolder As MAPIFolder
  Set oOlFolder = myNameSpace.Folders("Custom
Contacts").Folders("Speakers") 'Here is my folder called "Custom Contacts",
which is really a pst file on it's own, with a subfolder for contact items
called "Speakers"
  Dim objContact As ContactItem
  Set objContact = objOutlook.CreateItem(olContactItem)

  Dim FName As String
  Dim HomeTelephone As String
  Dim BusinessTelephone As String
  Dim MailAddress As String
  Dim aField As FormField
  Dim myCategory As String
  Dim Hour8Training As Integer
  Dim Hour65Training As Integer

   For Each aField In ActiveDocument.FormFields
       If Trim(aField.Result) <> "" Then
           If aField.Name = "chkPtdNewsletter" And aField.Result = "1" Then
               myCategory = "Printed News"
           Else
               myCategory = "eNews"
           End If

           If aField.Name = "chk8HourTrainingYes" Then
               Hour8Training = Val(aField.Result)
           ElseIf aField.Name = "chk65HourTrainingYes" Then
               Hour65Training = Val(aField.Result)
           End If

           If aField.Name = "NameField" Then
               FName = aField.Result
           ElseIf aField.Name = "Phone2Field" Then
               HomeTelephone = aField.Result
           ElseIf aField.Name = "PhoneField" Then
               BusinessTelephone = aField.Result
           ElseIf aField.Name = "MailingAddress" Then
               MailAddress = aField.Result
           End If

       End If

   Next

      With objContact  'How do I direct the code to go into the Custom
Folder?
          .FullName = FName
          .HomeTelephoneNumber = HomeTelephone
          .BusinessTelephoneNumber = BusinessTelephone
          .BusinessAddress = MailAddress
          .Categories = myCategory
          .UserProperties("8HourTraining") = Hour8Training
          .UserProperties("65HourTraining") = Hour65Training
          .Save
      End With
      Set objContact = Nothing
      Set objOutlook = Nothing

  End Sub
Jean-Guy Marcil - 05 Oct 2005 13:55 GMT
audrie magno was telling us:
audrie magno nous racontait que :

> In the code below, form field data is leaving Word and going to an
> Outlook contact list. Everything works EXCEPT it puts the new contact
[quoted text clipped - 13 lines]
>   Dim objContact As ContactItem
>   Set objContact = objOutlook.CreateItem(olContactItem)

CreateItem will only create default Outlook items.

Since you want to add an item to a non-default folder, you have to use the
Add method of the Items collection. As in:

Dim objOutlook As New outlook.Application
Dim myNamespace As outlook.NameSpace
Dim myItems As outlook.Items
Dim oOlFolder As outlook.MAPIFolder
Dim objContact As outlook.ContactItem
Set myNamespace = objOutlook.GetNamespace("MAPI")
Set oOlFolder = _
   myNamespace.Folders("Custom Contacts").Folders("Speakers")
Set myItems = oOlFolder.Items
Set objContact = myItems.Add(olContactItem)

With  objContact  ...

But really, you should post in an Outlook group...

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.