![](/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.