Excel 至 Outlook 联系人,对象不支持此 属性 或方法
Excel to Outlook Contact, Object doesn't support this property or method
此代码删除子文件夹中的所有联系人,然后将更新后的 sheet 从 excel 发送到 outlook 的正确联系人输入值中。
我收到错误:运行 TIME ERROR 438 - OBJECT DOESN'T SUPPORT THIS 属性 OR METHOD at the line:
.FullName = Range("D" & i).Value
所以我显然没有做对。我使用错误的操作吗?我没有将对象引用到正确的库吗?我可以看到 excel 中的值加载到每个项目中,只是没有进入 outlook。我哪里错了?
由于有多个 outlook 版本,因此使用后期绑定,因此不能选择引用对象库。
Sub XL2OLContacts()
Dim olApp As Object 'using late binding to ensure compatibility for all office versions
Dim olItem As Object
Dim olFolder As Object
Dim olConItems As Object
Set olApp = CreateObject("Outlook.Application") 'opens outlook
Set olNamespace = olApp.GetNamespace("MAPI") 'setting MAPI location for contacts
Set activefolder = olNamespace.Folders 'making default user contacts active folder
n = 1 'counter starting
Do Until activefolder.Item(n) = (Environ$("Username")) & "@###.com" 'this says USERNAME@###.com will be the default user profile for contact location
n = n + 1
Loop
Set myfolder = activefolder.Item(n) 'default folder active
Set myfolder2 = myfolder.Folders("Contacts").Folders("Call Observation List") 'setting contacts subfolder to var now
Do
For Each ContactItem In myfolder2.Items
ContactItem.Delete
Next ContactItem
Loop Until myfolder2.Items.Count = 0 'otherwise it would only delete a handful each time it ran for some reason
n = 1
Do Until activefolder.Item(n) = (Environ$("Username")) & "@###.com"
n = n + 1
Loop
Set myfolder = activefolder.Item(n)
Set myfolder2 = myfolder.Folders("Contacts").Folders("Call Observation List")
lastrow = Sheets("CSV Page").Range("A" & Sheets("CSV Page").Rows.Count).End(xlUp).Row
For i = 1 To lastrow
Sheets("CSV Page").Activate
If ActiveSheet.Range("C" & i).Value = "" Then
Set olConItem = olApp.CreateItem(olContactItem)
With olConItem
.FullName = Range("D" & i).Value
.EmailAddress = Range("F" & i).Value
.HomePhone = Range("L" & i).Value
.MobilePhone = Range("N" & i).Value
.JobTitle = Range("Z" & i).Value
.Notes = Range("AC" & i).Value
.Save
End With
End If
Application.StatusBar = "Updating Contacts: " & Format(i / lastrow, "Percent") & " Complete"
Next i
End Sub
如果您延迟绑定代码,则不会定义 Outlook 库中的常量,例如 olContactItem
。由于您的代码顶部没有 Option Explicit
,因此 VBA 假定您要创建一个名为 olContactItem
的新变量,其默认初始值为 0
。
这意味着您实际上是在创建一个 MailItem
,因为那是 olApp.CreateItem(0)
会做的。将此行添加到代码的开头:
Const olContactItem as Long = 2
此代码删除子文件夹中的所有联系人,然后将更新后的 sheet 从 excel 发送到 outlook 的正确联系人输入值中。
我收到错误:运行 TIME ERROR 438 - OBJECT DOESN'T SUPPORT THIS 属性 OR METHOD at the line:
.FullName = Range("D" & i).Value
所以我显然没有做对。我使用错误的操作吗?我没有将对象引用到正确的库吗?我可以看到 excel 中的值加载到每个项目中,只是没有进入 outlook。我哪里错了?
由于有多个 outlook 版本,因此使用后期绑定,因此不能选择引用对象库。
Sub XL2OLContacts()
Dim olApp As Object 'using late binding to ensure compatibility for all office versions
Dim olItem As Object
Dim olFolder As Object
Dim olConItems As Object
Set olApp = CreateObject("Outlook.Application") 'opens outlook
Set olNamespace = olApp.GetNamespace("MAPI") 'setting MAPI location for contacts
Set activefolder = olNamespace.Folders 'making default user contacts active folder
n = 1 'counter starting
Do Until activefolder.Item(n) = (Environ$("Username")) & "@###.com" 'this says USERNAME@###.com will be the default user profile for contact location
n = n + 1
Loop
Set myfolder = activefolder.Item(n) 'default folder active
Set myfolder2 = myfolder.Folders("Contacts").Folders("Call Observation List") 'setting contacts subfolder to var now
Do
For Each ContactItem In myfolder2.Items
ContactItem.Delete
Next ContactItem
Loop Until myfolder2.Items.Count = 0 'otherwise it would only delete a handful each time it ran for some reason
n = 1
Do Until activefolder.Item(n) = (Environ$("Username")) & "@###.com"
n = n + 1
Loop
Set myfolder = activefolder.Item(n)
Set myfolder2 = myfolder.Folders("Contacts").Folders("Call Observation List")
lastrow = Sheets("CSV Page").Range("A" & Sheets("CSV Page").Rows.Count).End(xlUp).Row
For i = 1 To lastrow
Sheets("CSV Page").Activate
If ActiveSheet.Range("C" & i).Value = "" Then
Set olConItem = olApp.CreateItem(olContactItem)
With olConItem
.FullName = Range("D" & i).Value
.EmailAddress = Range("F" & i).Value
.HomePhone = Range("L" & i).Value
.MobilePhone = Range("N" & i).Value
.JobTitle = Range("Z" & i).Value
.Notes = Range("AC" & i).Value
.Save
End With
End If
Application.StatusBar = "Updating Contacts: " & Format(i / lastrow, "Percent") & " Complete"
Next i
End Sub
如果您延迟绑定代码,则不会定义 Outlook 库中的常量,例如 olContactItem
。由于您的代码顶部没有 Option Explicit
,因此 VBA 假定您要创建一个名为 olContactItem
的新变量,其默认初始值为 0
。
这意味着您实际上是在创建一个 MailItem
,因为那是 olApp.CreateItem(0)
会做的。将此行添加到代码的开头:
Const olContactItem as Long = 2