Kontakte aus Excel-Tabelle in Outlook importieren

  • VB.NET

    Kontakte aus Excel-Tabelle in Outlook importieren

    Hi,
    folgendes Problem: Ich habe ein Visual Basic Script erstellt, welches alle in Outlook gespeicherten Kontakte in eine Excel Tabelle schreibt.
    Der Export funktioniert prima und einwandfrei.
    Jedoch stellt sich der Import von der Excel nach Outlook etwas problematisch dar.
    Hat jemand einen Tipp oder sogar die Lösung warum der bisherige Code nicht funktioniert ?

    PS: Ich beschäftige mich noch nicht sehr lange mit Visual Basic, also verzeiht mir bitte solltet ihr blutige Anfängerfehler finden :)

    Hier nun der bisherige Code:

    VB.NET-Quellcode

    1. Imports Outlook = Microsoft.Office.Interop.Outlook
    2. Imports Excel = Microsoft.Office.Interop.Excel
    3. Public Class FExport
    4. Dim olApp As Microsoft.Office.Interop.Outlook.Application
    5. Dim olNameSpace As Microsoft.Office.Interop.Outlook.NameSpace
    6. Dim olContactsFolder As Microsoft.Office.Interop.Outlook.MAPIFolder
    7. Dim olAll As Microsoft.Office.Interop.Outlook.Items
    8. Dim olReal As Microsoft.Office.Interop.Outlook.Items
    9. Dim olContact As Microsoft.Office.Interop.Outlook.ContactItem
    10. Dim syncSuccess As Boolean
    11. Dim AllProcesses() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses()
    12. Dim MyProcess As System.Diagnostics.Process
    13. Dim strContactFilter As String
    14. Dim xlApp As Object
    15. Dim xlWb As Object
    16. Dim xlWs As Object
    17. Dim intRow As Integer
    18. Dim Rows As Object
    19. Dim xlUp As Object
    20. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BStart.Click
    21. If CAuswahl.Text = "Exportieren" Then
    22. 'Outlook-Objekte öffnen
    23. olApp = CreateObject("Outlook.Application")
    24. olNameSpace = olApp.GetNamespace("MAPI")
    25. olContactsFolder = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts)
    26. olAll = olContactsFolder.Items
    27. 'Verteilerlisten herausfiltern,
    28. 'nur 'Richtige Kontakte' verwenden
    29. strContactFilter = "[MessageClass] = 'IPM.Contact'"
    30. olReal = olAll.Restrict(strContactFilter)
    31. 'Excel-Objekte öffnen
    32. xlApp = CreateObject("Excel.Application") 'Neue Excel-Instanz
    33. xlWb = xlApp.Workbooks.Add 'Neues Workbook anlegen
    34. xlWs = xlWb.Sheets(1) 'Erstes Sheet
    35. 'Excel-Worksheet aufbereiten
    36. With xlWs
    37. 'Sheet-Name
    38. .Name = "Outlook-Kontakte"
    39. 'Spaltenüberschriften
    40. .cells(1, 1).Value = "Vorname"
    41. .cells(1, 2).Value = "Nachname"
    42. .cells(1, 3).Value = "E-Mail"
    43. .cells(1, 4).Value = "Strasse"
    44. .cells(1, 5).Value = "PLZ"
    45. .cells(1, 6).Value = "Ort"
    46. .cells(1, 7).Value = "Land"
    47. .cells(1, 8).Value = "Telefon"
    48. 'Spaltenüberschriften fett
    49. .Rows("1:1").Font.Bold = True
    50. 'Outlook-Kontakte nach Excel übertragen
    51. intRow = 2
    52. For Each itmContacts In olReal
    53. .cells(intRow, 1).Value = itmContacts.FirstName
    54. .cells(intRow, 2).Value = itmContacts.LastName
    55. .cells(intRow, 3).Value = itmContacts.Email1Address
    56. .cells(intRow, 4).Value = itmContacts.BusinessAddressStreet
    57. .cells(intRow, 5).Value = itmContacts.BusinessAddressPostalCode
    58. .cells(intRow, 6).Value = itmContacts.BusinessAddressCity
    59. .cells(intRow, 7).Value = itmContacts.BusinessAddressCountry
    60. .cells(intRow, 8).Value = itmContacts.BusinessTelephoneNumber
    61. intRow = intRow + 1
    62. Next itmContacts
    63. 'Optimale Spaltenbreite
    64. .Columns.AutoFit()
    65. syncSuccess = True
    66. CreateContacts_Exit:
    67. If syncSuccess = True Then
    68. MsgBox("Die Synchronisierung war erfolgreich", vbInformation)
    69. Me.Close()
    70. Else
    71. MsgBox("Die Synchronisierung ist fehlgeschlagen", vbInformation)
    72. End If
    73. End With
    74. 'Excel einblenden
    75. xlApp.Visible = True
    76. xlWb.SaveAs("C:\Kontakte\Kontakte " & DateString & ".xlsx")
    77. For Each Process In System.Diagnostics.Process.GetProcessesByName("excel")
    78. Process.Kill()
    79. Next
    80. 'Speicher freigeben
    81. olReal = Nothing
    82. olAll = Nothing
    83. olContactsFolder = Nothing
    84. olNameSpace = Nothing
    85. xlWs = Nothing
    86. xlWb = Nothing
    87. xlApp = Nothing
    88. ElseIf CAuswahl.Text = "Importieren" Then
    89. Dim olApp As Microsoft.Office.Interop.Outlook.Application
    90. Dim olNameSpace As Microsoft.Office.Interop.Outlook.NameSpace
    91. Dim olContactsFolder As Microsoft.Office.Interop.Outlook.MAPIFolder
    92. Dim olAll As Microsoft.Office.Interop.Outlook.Items
    93. Dim olReal As Microsoft.Office.Interop.Outlook.Items
    94. Dim olContact As Microsoft.Office.Interop.Outlook.ContactItem
    95. Dim AllProcesses() As System.Diagnostics.Process = System.Diagnostics.Process.GetProcesses()
    96. Dim MyProcess As System.Diagnostics.Process
    97. Dim xlApp As Microsoft.Office.Interop.Excel.Application
    98. Dim exWb As Microsoft.Office.Interop.Excel.Workbook
    99. Dim exWs As Microsoft.Office.Interop.Excel.Worksheet
    100. Dim intRow As Integer
    101. Dim lngLastRow As Long
    102. Dim lngContactsCount As Long
    103. Dim syncSuccess As Boolean
    104. Dim strContactFilter As String
    105. Dim aktzeile As Integer
    106. Dim zeilennr As Integer
    107. Dim inhalt As Object
    108. Dim FileName As String
    109. Dim ToRangeCounter As Object
    110. Dim xlUp As Long
    111. Dim xlToRight As Long
    112. Dim Contact As Object
    113. xlApp = CreateObject("Excel.Application")
    114. olApp = CreateObject("Outlook.Application")
    115. olNameSpace = olApp.GetNamespace("MAPI")
    116. olContactsFolder = olNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderContacts)
    117. With xlApp
    118. xlApp.Visible = True
    119. xlWb = xlApp.Workbooks.Open("C:\Kontakte\Kontakte " & DateString & ".xlsx")
    120. End With
    121. olContact = olApp.CreateItem(Outlook.OlItemType.olDistributionListItem)
    122. intRow = 0
    123. For Each itmContacts In olReal
    124. itmContacts.FirstName = xlWs.cells(intRow, 1).Value
    125. itmContacts.LastName = xlWs.cells(intRow, 2).Value
    126. itmContacts.Email1Address = xlWs.cells(intRow, 3).Value
    127. itmContacts.BusinessAddressStreet = xlWs.cells(intRow, 4).Value
    128. itmContacts.BusinessAddressPostalCode = xlWs.cells(intRow, 5).Value
    129. itmContacts.BusinessAddressCity = xlWs.cells(intRow, 6).Value
    130. itmContacts.BusinessAddressCountry = xlWs.cells(intRow, 7).Value
    131. itmContacts.BusinessTelephoneNumber = xlWs.cells(intRow, 8).Value
    132. intRow = intRow + 1
    133. itmContacts.Save()
    134. Next
    135. ' .ActiveWorkbook.Save
    136. ' .ActiveWorkbook.Close
    137. ' .Quit
    138. End If
    139. End Sub
    140. Private Sub BExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BExit.Click
    141. Me.Close()
    142. End Sub
    143. End Class


    *Topic verschoben, das ist kein VBScript*

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Marcus Gräfe“ ()