![](/img/trans.png)
[英]Late binding compile error: User-defined type not defined referencing Outlook mailitem in Excel VBA
[英]VBA 'User Defined Type Not Defined' Compile Error with Outlook
我有一個大的Excel文件,通過命令按鈕發送電子郵件到工作中的經理,然后他們可以按下按鈕,它將文件發送到他們下面的經理。
由於每個經理都有自己的MS Office
版本,我有一個子程序可以檢查他/她的計算機上有哪個版本,並在References
標記為V
當我保存文件時,我將其保存為Outlook Object Library
未標記為V
,並且我有其他人構建的代碼。 代碼貫穿3個子。 第一個sub有一個msgbox
,當你回答它時, 是的 ,它會將你發送到下一個sub。
Public Sub before_send_mail()
answer = MsgBox("Send Email?", vbYesNo + vbQuestion, "Empty Sheet")
If answer = vbYes Then
Call excel_ver
Call sendMail
Call remove_ref
Else
'do nothing
End If
End Sub
然后,我有“辦公室版本的參考選擇器”,它檢查計算機上安裝的版本,並在Outlook
對象的Tools---->References
中自動標記V
那部分似乎也運作良好。
Sub excel_ver()
On Error Resume Next
ver = Application.Version
If ver = 16 Then
tmp_name = "C:\Program Files\Microsoft Office\Office16\MSOUTL.OLB"
Application.VBE.ActiveVBProject.References.AddFromFile tmp_name
Exit Sub
End If
If ver = 15 Then
tmp_name = "C:\Program Files\Microsoft Office\Office15\MSOUTL.OLB"
Application.VBE.ActiveVBProject.References.AddFromFile tmp_name
Exit Sub
End If
If ver = 14 Then
tmp_name = "C:\Program Files\Microsoft Office\Office14\MSOUTL.OLB"
Application.VBE.ActiveVBProject.References.AddFromFile tmp_name
Exit Sub
End If
End Sub
然后我們解決了這個問題。 當我到sub sendMail
它在Dim applOL As Outlook.Application
行上給出了一個錯誤
Public Sub sendMail()
Call ini_set
If mail_msg.Cells(200, 200) = 1 Then
lr = main_dist.Cells(main_dist.Rows.Count, "A").End(xlUp).Row
On Error Resume Next
For i = 2 To lr
Application.DisplayAlerts = False
Dim applOL As Outlook.Application 'Here is the error ---- that line
Dim miOL As Outlook.MailItem
Dim recptOL As Outlook.Recipient
mail_msg.Visible = True
mailSub = mail_msg.Range("B1")
mailBody = mail_msg.Range("B2")
mail_msg.Visible = False
Set applOL = New Outlook.Application
Set miOL = applOL.CreateItem(olMailItem)
Set recptOL = miOL.Recipients.Add(main_dist.Cells(i, 5))
recptOL.Type = olTo
tempPath = ActiveWorkbook.Path & "\" & main_dist.Cells(i, 4) & ".xlsm"
With miOL
.Subject = mailSub
.Body = mailBody
.Attachments.Add tempPath
.send
End With
Set applOL = Nothing
Set miOL = Nothing
Set recptOL = Nothing
Application.DisplayAlerts = True
Next i
End If
End Sub
在預編譯過程中, Outlook.Application
無效,因為它未在Tools\\References...
。 如果你想保持你的代碼工作,你需要先運行ini_set
,然后sendMail
編譯sendMail
。 嘗試添加新的子例程以按順序調用它們:
Sub MainSub()
call ini_set
call sendMail
End Sub
要清楚 - 從sendMail
刪除Call ini_set
,每次必須調用它們都從單獨的子例程中執行。
重要! 使用此解決方案,您可以保留outlook appilcation constants
olMailItem
outlook appilcation constants
(如olMailItem
),這在切換到Late binding solution
時是不可能的。
應運行時無需參考:
Public Sub sendMail()
Dim applOL As Object, miOL As Object, recptOL As Object
Dim i As Long
ini_set
If mail_msg.Cells(200, 200) = 1 Then
Set applOL = CreateObject("Outlook.Application")
For i = 2 To main_dist.Cells(main_dist.Rows.Count, "A").End(xlUp).Row
Set miOL = applOL.CreateItem(0) 'olMailItem=0
Set recptOL = miOL.Recipients.Add(main_dist.Cells(i, 5))
recptOL.Type = 1 ' olTo=1
With miOL
.Subject = mail_msg.Range("B1")
.Body = mail_msg.Range("B2")
.Attachments.Add ActiveWorkbook.Path & "\" & _
main_dist.Cells(i, 4) & ".xlsm"
.send
End With
Next i
Set applOL = Nothing
End If
End Sub
編輯:在上面的代碼中,我刪除了一些“一次性”變量,但這只是我的偏好......
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.