![](/img/trans.png)
[英]Creating Outlook Item in Excel Application generates error: “Object doesn't support this property or method”
[英]Excel to Outlook Contact, Object doesn't support this property or method
此代码删除子文件夹中的所有联系人,然后将更新的工作表从 excel 发送到 Outlook 的正确联系人输入值中。
我收到错误:运行时错误 438 - 对象不支持此属性或方法行:
.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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.