gpt4 book ai didi

VBA 'User Defined Type Not Defined' Outlook 编译错误

转载 作者:行者123 更新时间:2023-12-02 09:44:24 27 4
gpt4 key购买 nike

我有一个很大的 Excel 文件,它通过命令按钮向工作中的经理发送电子邮件,然后他们可以按按钮,将文件发送给他们下面的经理。

由于每个经理都有自己的 MS Office 版本,因此我有一个子程序来检查他/她的计算机上的版本并标记 V > 在引用中。

当我保存文件时,我将其保存为 Outlook Object Library 未标记 V 的状态,并且我有其他人构建的代码。该代码运行了 3 个子程序。第一个子目录有一个 msgbox,当您回答 Yes 时,它会将您发送到下一个子目录。

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

然后,我有“按 Office 版本列出的引用选择器”,它会检查计算机上安装的版本,并在工具---->引用中自动标记 V > 在 Outlook 对象中。这部分似乎也运作良好。

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

然后我们就开始解决问题了。当我到达子 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

最佳答案

应该无需引用即可运行:

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

编辑:在上面的代码中,我删除了一些“一次性”变量,但这只是我的偏好......

关于VBA 'User Defined Type Not Defined' Outlook 编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47807594/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com