MsAccessNL
Member
- Local time
- Today, 13:22
- Joined
- Aug 27, 2022
- Messages
- 196
I made this code to import a csv file with contacts into a Public Outlook Contacts folder. On opening of Outlook the code checks if a new csv file is present and it will import the new contacts. The code was working fine, but now it's giving an unknown error, with the line: Set olFolder = olNS.AddressLists(sAddresListName).GetContactsFolder. Set olNS = Application.GetNamespace("MAPI") is working correctly.
The strange thing is, when I call the function (ImportPublicCsv) from the immediate window, the code runs without the error.
The top part of the used code:
The strange thing is, when I call the function (ImportPublicCsv) from the immediate window, the code runs without the error.
The top part of the used code:
Code:
Dim arrLines As Variant
Dim sFind As String, iNew As Integer, iUpdate As Integer
Dim sAddress As String
Dim olNS As Outlook.NameSpace
Dim olContacts As Outlook.Items
Dim olContact As Outlook.ContactItem
Dim olFolder As Outlook.Folder
Dim sBussiness As String, sBusiness2 As String, sHome As String, sMobile As String
Public Sub ImportPublicCSV()
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' This Application is made by Daniel Sanders info@MsAccess.nl 13-06-2024
' Updated
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
On Error GoTo HandleError
Dim TempTime
Dim sPathFile As String
Dim arrData
Dim sFileContents As String, i As Long
Dim iFile As Integer, iWait As Byte
Dim sContactGroup As String, sAddresListName As String
Dim bUpdate As Boolean
'-------deze gegevens kunnen door client aangepast worden--------------------------
sAddresListName = "Mubion adressen"
iWait = 5 'AANTAL SECONDEN DAT DE CODE WACHT NA OPSTARTEN
sPathFile = "M:\500 Marketing\590 CRM\MKG Contacts\MKG_Outlook_import.csv"
'----------------------------------------------------------------------------------
'
TempTime = DateAdd("S", iWait, Now)
While TempTime > Now
DoEvents
Wend
'sPathFile = "E:\Documenten\DataReader\MKG_Outlook_import.csv"
If Len(Dir(sPathFile)) > 0 Then
If MsgBox("Outlook found a new Contact-File, do you want to import now?", vbYesNo) = vbYes Then
iNew = 0
iUpdate = 0
On Error GoTo HandleError
If olNS Is Nothing Then Set olNS = Application.GetNamespace("MAPI")
'Set olContacts = olNS.GetDefaultFolder(olFolderContacts).Folders(CONTACT_GROUP).Items
'Set olFolder = olNS.Folders(sAccountName).Folders("Contactpersonen").Folders(sContactGroup) '13-6-24 vervangen
Set olFolder = olNS.AddressLists(sAddresListName).GetContactsFolder
Set olContacts = olFolder.Items
If IsEmpty(arrLines) = True Then
iFile = FreeFile
Open sPathFile For Input As iFile
sFileContents = Input(LOF(iFile), iFile)
Close iFile
arrLines = Split(sFileContents, vbNewLine)
End If