这种行为很奇怪,也很难描述,所以我会尽我最大的努力。
因此,由于某种原因,打开Outlook会阻止我多次成功运行以下代码。
下面是创建附件并将附件添加到电子邮件中的代码。
Me.PrintForm1.PrintFileName = Jobpath & "\DrawingChecklist.pdf"
PrintForm1.Print(Me, PowerPacks.Printing.PrintForm.PrintOption.Scrollable)
'Log
My.Computer.FileSystem.WriteAllText(My.Application.Info.DirectoryPath & "\" & "Log.txt", "User: " & Environment.UserName & " Saved: " & txtQuoteNumber.Text & " at: " & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") & Environment.NewLine, True) 'True appends, False overwites
Dim OutlookMessage As Outlook.MailItem
Dim AppOutlook As New Outlook.Application
Try
'Make sure file has been created
Dim xx = 0
Do While Dir(Jobpath & "\DrawingChecklist.pdf") = "" And xx < 10
'MsgBox("File doens't exist." & " " & Jobpath & "\DrawingChecklist.pdf")
xx = xx + 1
Threading.Thread.Sleep(100)
Loop
'Double check file has been created
If Dir(Jobpath & "\DrawingChecklist.pdf") = "" Then
MsgBox("File doens't exist." & " " & Jobpath & "\DrawingChecklist.pdf")
End If
OutlookMessage = AppOutlook.CreateItem(Outlook.OlItemType.olMailItem)
Dim Recipents As Outlook.Recipients = OutlookMessage.Recipients
Recipents.Add(File.ReadAllText(ExePath & "DraftingEmail.txt"))
Recipents.ResolveAll()
OutlookMessage.Subject = txtCompany.Text & " " & txtQuoteNumber.Text & " " & txtJobName.Text
OutlookMessage.Attachments.Add(Jobpath & "\DrawingChecklist.pdf")
OutlookMessage.BodyFormat = Outlook.OlBodyFormat.olFormatHTML
OutlookMessage.Display()
'OutlookMessage.Send()
'MsgBox("Mail sent!")
'Catch ex As Exception
'MessageBox.Show("Mail could Not be sent") 'if you dont want this message, simply delete this line
Finally
OutlookMessage = Nothing
AppOutlook = Nothing
End Try
发布于 2020-07-20 16:52:31
Attachments.Add方法在Attachments
集合中创建一个新的附件。附件的源可以是文件(由具有文件名的完整文件系统路径表示),也可以是构成附件的Outlook项。
文件应该位于本地磁盘上--您需要先在本地驱动器上下载它。
https://stackoverflow.com/questions/62997978
复制相似问题