![](/img/trans.png)
[英]How to fix Compile Error: User-defined type not defined when using Excel VBA from Outlook?
[英]User defined type not defined on Outlook.Namespace from Excel VBA?
我正在嘗試從 Excel 中搜索具有特定主題的 Outlook 電子郵件:
Sub Work_with_Outlook()
Set olApp = CreateObject("Outlook.Application")
Dim olNs As Outlook.Namespace
Dim Fldr As Outlook.MAPIFolder
Dim olMail As Variant
Dim sir() As String
Set olApp = New Outlook.Application
Set olNs = olApp.GetNamespace("MAPI")
Set Fldr = olNs.GetDefaultFolder(olFolderInbox)
Set myTasks = Fldr.Items
Set olMail = myTasks.Find("[Subject] = ""*desired subject*""")
If Not (olMail Is Nothing) Then
sir = Split(olMail.Body, vbCrLf)
For i = 1 To UBound(sir)
ActiveWorkbook.Sheets("Sheet1").Cells(i, 1).Value = sir(i)
Next i
olMail.Delete
End If
End Sub
我在 Excel 中收到一個錯誤說
用戶定義類型未定義
在這一行:
Dim olNs As Outlook.Namespace
您需要在 VBA 項目中添加對 Outlook 的引用。
在 VBA 編輯器菜單欄中單擊工具 -> 參考並檢查 Microsoft Outlook 14.0 對象庫(版本號可能與 14.0 不同,具體取決於您的 MS Office 版本)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.