我试图重命名我的报告,以便它可以包含公司名称和创建日期。我的代码无法附加文件,因为找不到文件。有人以前尝试过此方法或有任何建议吗?
Private Sub emailReport_Click()
On Error GoTo ErrorHandler
Dim fileN As String
Dim todaysD As String
Dim oApp As Object
Dim oItem As Object
Const oMailItem As Long = 0
Set oApp = CreateObject("Outlook.Application")
Set oItem = oApp.CreateItem(oMailItem)
todaysD = Format(Date, "DD-MM-YYYY")
fileN = "Quantity used by " & CompanyName & " as of " & todaysD & ".pdf"
DoCmd.OutputTo acOutputReport, "reportQuantityUsed", acFormatPDF, fileN
With oItem
.To = ""
.CC = ""
.BCC = ""
.Subject = "Quantity used report - " & CompanyName
.Body = "Please find attached the quantity of stock used from: " &
CompanyName
.Attachments.Add (fileN)
.Display
End With
exitErrorHandler:
Exit Sub
ErrorHandler: MsgBox Err.Description
GoTo exitErrorHandler
End Sub
答案 0 :(得分:0)
尝试使用条件代码打开Outlook或使用已经打开的实例。我的测试表明,当Shell打开Outlook时,以下用于设置和打开Outlook对象的代码将无法立即使用。在Outlook保持打开状态下重新运行该过程,然后代码可以工作。
Sub EMTest()
Dim oApp As Outlook.Application, OMail As Outlook.MailItem
On Error Resume Next
Set oApp = GetObject(, "Outlook.Application")
If Err.Number <> 0 Then
Shell ("OUTLOOK")
'allow enough time for app to completely open
Dim Start As Double
Start = Timer
While Timer < Start + 10
DoEvents
Wend
EMTest
End If
Set OMail = oApp.CreateItem(0)
With OMail
.To = "email address"
.Subject = "Subject"
.Display 'email must be displayed for the next line to work to include default signature
.HTMLBody = "Message." & vbNewLine & .HTMLBody
'.Send
End With
Set OMail = Nothing
Set oApp = Nothing
End Sub