Results 1 to 17 of 17

Thread: [Resolved] Export addresses from Excel to Outlook

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Resolved [Resolved] Export addresses from Excel to Outlook

    Im writing a small function in VBA in Excell to create an Address book in Outlook 2003 from the list of staff names & addresses in an Excell spreadsheet.

    It needs to be done this way as the software in use to store the staff details can only output to a .csv file.

    Im thinking i have to do somthing along the lines of:

    VB Code:
    1. Dim oApp As Outlook.Application
    2. Dim oAddress As Outlook.AddressList 'Is this correct?
    3.  
    4. Set oApp = New Outlook.Application
    5. Set oAddress = 'Not entirely sure of how to assign this

    And then read through each line of the spreadsheet creating entries?
    Last edited by c03cg; Mar 7th, 2006 at 11:03 AM. Reason: [Resolved]

  2. #2

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Export addresses from Excel to Outlook

    ok, so sofar i can add a new contact with the needed details using:

    VB Code:
    1. Dim oApp As Outlook.Application
    2.     Dim oNS As Outlook.Namespace
    3.     Dim iOne As Integer
    4.    
    5.     Set oApp = New Outlook.Application
    6.     Set oNS = oApp.GetNamespace("MAPI")
    7.    
    8.     Dim oContact As Outlook.ContactItem
    9.    
    10.     'Set oContact = oNS.GetItemFromID(Range("B2") & " " & Range("A2"))
    11.     iOne = 1
    12.    
    13.     Do While iOne < 200
    14.         Range("E" & iOne + 1).Select
    15.         ActiveCell.FormulaR1C1 = "=R[-1]C+1"
    16.         iOne = iOne + 1
    17.     Loop
    18.    
    19.     Columns("E:E").Select
    20.     Selection.NumberFormat = "000"
    21.    
    22.     Application.Cursor = xlWait
    23.     Set oContact = oApp.CreateItem(olContactItem)
    24.     oContact.CompanyName = "President Kennedy School"
    25.     oContact.Email1Address = Range("D2").Text
    26.     oContact.Email1AddressType = "Business"
    27.     oContact.Email1DisplayName = Range("B2").Text & " " & Range("A2").Text
    28.     oContact.FileAs = Range("A2").Text & ", " & Range("B2").Text
    29.     oContact.FirstName = Range("B2").Text
    30.     oContact.FullName = Range("B2").Text & " " & Range("A2").Text
    31.     oContact.LastName = Range("A2").Text
    32.     oContact.UserProperties("PKSID") = oContact.User1
    33.     oContact.User1 = "PKS" & Range("E2")
    34.     oContact.Save
    35.     Application.Cursor = xlDefault

    I beleive ive set up 'User1' to add a field called PKSID and fill it with a 3 digit number..... how can i search the contacts by this so i dont duplicate?

  3. #3
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Export addresses from Excel to Outlook

    Use the Restrict or Find methods. One of them is for use with custom properties. Cant remember right now.

    Also, if you save the .EntryID property of the contact item that would be your unique ID throughout your Outlook store.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  4. #4

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Export addresses from Excel to Outlook

    Cheers for the advice Rob..... searching them atm..... but what outlook property do i have to search within?

  5. #5
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Export addresses from Excel to Outlook

    The .Items collection of the MAPIFolder your creating or storing the contact items in.

    If you use the .EntryID property you can also use the .GetItemFromID method of the NameSpace object class.
    [/color]
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  6. #6

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Export addresses from Excel to Outlook

    Cool.... its working, cheers RobDog, my code now looks like this:

    VB Code:
    1. Sub Create_Address_Book()
    2.  
    3.     Dim oApp As Outlook.Application
    4.     Dim oNS As Outlook.Namespace
    5.     Dim oCF As Outlook.MAPIFolder
    6.     Dim iOne As Integer
    7.    
    8.     Set oApp = New Outlook.Application
    9.     Set oNS = oApp.GetNamespace("MAPI")
    10.    
    11.     Dim oContact As Outlook.ContactItem
    12.    
    13.     Set oCF = oNS.Folders(1).Folders("Contacts")
    14.    
    15.     iOne = 2
    16.    
    17.     Application.Cursor = xlWait
    18.    
    19.     Do While Not Range("A" & iOne) = ""
    20.         Set oContact = oCF.Items.Find("[FullName] = " & Range("b" & iOne) & " " & Range("a" & iOne) & "")
    21.        
    22.         If TypeName(oContact) = "Nothing" Then
    23.             Set oContact = oApp.CreateItem(olContactItem)
    24.             oContact.CompanyName = "President Kennedy School"
    25.             oContact.Email1Address = Range("D" & iOne).Text
    26.             oContact.Email1AddressType = "Business"
    27.             oContact.Email1DisplayName = Range("B" & iOne).Text & " " & Range("A" & iOne).Text
    28.             oContact.FileAs = Range("A" & iOne).Text & ", " & Range("B" & iOne).Text
    29.             oContact.FirstName = Range("B" & iOne).Text
    30.             oContact.FullName = Range("B" & iOne).Text & " " & Range("A" & iOne).Text
    31.             oContact.LastName = Range("A" & iOne).Text
    32.             oContact.Save
    33.         Else
    34.             Application.Cursor = xlDefault
    35.             MsgBox "The contact '" & oContact & "' already exists"
    36.             Application.Cursor = xlWait
    37.         End If
    38.            
    39.         iOne = iOne + 1
    40.     Loop
    41.    
    42.     Application.Cursor = xlDefault
    43.    
    44. End Sub

    How do i change the Contacts view programaitically?

  7. #7
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Export addresses from Excel to Outlook

    VB Code:
    1. Set oApp.ActiveExplorer.CurrentFolder = oCF
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  8. #8

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Export addresses from Excel to Outlook

    Cheers... but, being the doofus i am, didn't explain what i meant!

    How do i set the Contacts folder to 'View by Company' programatically?

  9. #9
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Export addresses from Excel to Outlook

    Oh, . You need to modify the View manually. Then use the View object and Views collection for that folder to save it out to an xml or text file. Then when you run your app you can read in the file and set the view of your folder to that read-in saved xml view.

    You can just change the xml element using the xml parse class but its more code then its worth when you can do it this was with just a few lines of code and no references.
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  10. #10

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: Export addresses from Excel to Outlook

    Spot on.... cheers Rob, im going to mark the matter resolved, however if i get stuck with it, ill post again and PM you

    Many thanks

  11. #11
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Export addresses from Excel to Outlook

    My inbox is almost full so as long as you post back to this thread I will get it since I subscribe to all threads I post to.

    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [Resolved] Export addresses from Excel to Outlook

    RD.......

    ive got this:

    VB Code:
    1. Sub viewCheck()
    2.    
    3.     Dim oApp As Outlook.Application
    4.     Dim oNS As Outlook.NameSpace
    5.     Dim oCF As Outlook.MAPIFolder
    6.     Dim oView As Outlook.View
    7.     Dim oviews As Outlook.Views
    8.    
    9.     Set oApp = New Outlook.Application
    10.     Set oNS = oApp.GetNamespace("MAPI")
    11.     Set oCF = oNS.Folders(1).Folders("Contacts")
    12.     Set oView = oCF.CurrentView
    13.     Set oviews = oCF.Views
    14.    
    15.     MsgBox oView.XML
    16.    
    17. End Sub

    which obvioulsy displays the XML in a msgbox, how do i get Excell to set outlook to this view? The view name is 'By Company' and its a standard outlook one

    Any ideas?

  13. #13
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Resolved] Export addresses from Excel to Outlook

    You mean to just change the view or customize it?
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  14. #14

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [Resolved] Export addresses from Excel to Outlook

    Just change it from the default view to the 'By Company' view

  15. #15
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Resolved] Export addresses from Excel to Outlook

    Oh, ok. Thats easier. I think this is it.
    VB Code:
    1. '...
    2.     '...
    3.     Set oViews = oTaskBox.Views
    4.     Set oView = oViews.Item("By Company")
    5.     'Set the view to the current view
    6.     oView.Save
    7.     oView.Apply
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  16. #16

    Thread Starter
    Member
    Join Date
    Nov 2004
    Posts
    48

    Re: [Resolved] Export addresses from Excel to Outlook

    Good lad! Worked like a charm........ set oCF (current folder) to the contacts folder, then did:

    VB Code:
    1. Set oViews = oCF.Views
    2.     Set oView = oViews.Item("By Company")
    3.     oView.Save
    4.     oView.Apply

    Many thanks again!

  17. #17
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: [Resolved] Export addresses from Excel to Outlook

    No prob.

    I guess I still got it as I did it from memory and typed it into the reply box without testing (whew, glad it worked on first try lol).
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width