【发布时间】: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 能回来并进一步改进。
-
而实际的问题似乎不见了!