【问题标题】:VBA 'User Defined Type Not Defined' Compile Error with OutlookVBA“未定义用户定义类型”与 Outlook 编译错误
【发布时间】:2017-12-14 06:59:03
【问题描述】:

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

由于每个经理都有她/他自己的MS Office 版本,我有一个子程序可以检查他/他的计算机上的版本,并在References 中标记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 版本的引用选择器”,它检查计算机上安装了哪个版本,并在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

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

【问题讨论】:

  • 提前绑定到“Office14”Outlook(最低分母)。如果用户有更高版本,VBA 会自动修复引用。
  • marks V in references ? popes me an error??
  • 或者如果性能没有受到严重影响,考虑后期绑定?无需大量代码。
  • @ashleedawg 我已经努力纠正这个问题,希望 OP 能回来并进一步改进。
  • 而实际的问题似乎不见了!

标签: vba excel outlook


【解决方案1】:

应该在不需要参考的情况下运行:

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

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

【讨论】:

    【解决方案2】:

    在预编译过程中Outlook.Application 无效,因为它没有在Tools\References... 中设置。如果你想保持你的代码正常工作,你需要先运行ini_set,然后再编译sendMail。尝试添加新的子程序以依次调用两者:

    Sub MainSub()
        call ini_set
        call sendMail
    End Sub
    

    为了清楚起见 - 从您的 sendMail 中删除 Call ini_set 并且每次您必须从单独的子例程中调用两者时都这样做。

    重要!使用此解决方案,您可以保留outlook appilcation constants(如olMailItem),而当您切换到Late binding solution 时,这是不可能的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多