vba - 当我尝试在 Outlook 中发送 excel 时,vba 代码错误 400
问题描述
因此,当我尝试运行此代码以发送我的 excel 并在 Outlook 中附加 pdf 时,我遇到了错误 400。不太确定这里出了什么问题,希望能得到一些帮助。谢谢
在下面包括我的代码:
Sub Send_Doc()
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Set Sourcewb = ActiveWorkbook
'Copy the sheet to a new workbook
Sheets("xxxx").Copy
Set Destwb = ActiveWorkbook
Range("A4:Z10").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
TempFilePath = Environ$("temp") & "\"
TempFileName = "Daily xxx"
Range("A4:Z10").Select
Selection.CopyPicture xlScreen, xlBitmap
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With Destwb
.SaveAs TempFilePath & TempFileName & ".pdf"
On Error Resume Next
With OutMail
.To = "xxxx@xxx.com; xxx2@xxx.com"
.CC = "xxxx3@xxx.com; xxx4@xxx.com"
.BCC = ""
.Subject = "Daily xxx " & Format(Now(), "dd mmm yy")
OutMail.GetInspector.WordEditor.Range.Paste
.Attachments.Add Destwb.FullName
.display
End With
'OutMail.display
On Error GoTo 0
.Close SaveChanges:=False
End With
'Delete the file you have send
'Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
解决方案
我已经对其进行了编辑,似乎可以工作,但可以缩短。
Sub SendFxPosition()
Dim Sourcewb As Workbook
Dim Destwb As Workbook
Dim TempFilePath As String
Dim TempFileName As String
Dim OutApp As Object
Dim OutMail As Object
Dim rng As Range
Set Sourcewb = ActiveWorkbook
'ADD THIS:
' Select range from B instead of A so that you can type Dear Sir.... in Cell A
Sheets("SHEET1").Range("B4:U32").ExportAsFixedFormat Type:=xlTypePDF, _
Filename:="C:\Users\xxx\Downloads\test\DOCUMENT1.pdf", _
Quality:= xlQualityStandard, IncludeDocProperties:=True, _
IgnorePrintAreas:=False, OpenAfterPublish:=False
'END
'Copy the sheet to a new workbook:
' Copy from A so that when you paste into outlook you will have Dear Sir....
Sheets("SHEET1").Copy
Set Destwb = ActiveWorkbook
Range("A4:U32").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Application.CutCopyMode = False
'If Temp file doesn't work for you,
' you can use a designated folder like the one below
TempFilePath = "C:\Users\xxxx\Downloads\test\"
TempFileName = "DOCUMENT1"
Range("A4:U32").Select
Selection.Copy
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
With OutMail
OutMail.GetInspector.WordEditor.Range.PasteSpecial xlPasteValuesAndNumberFormats
End With
With Destwb
.SaveAs TempFilePath & TempFileName & ".xls"
On Error Resume Next
With OutMail
.To = "xxxx@xxx.com; xxxx2@xxx.com"
.CC = ""
.BCC = ""
.Subject = "Daily XXX " & Format(Now(), "dd mmm yy")
OutMail.GetInspector.WordEditor.Range.Paste
'ADD THIS
DestwbPDF = "C:\Users\xxxx\Downloads\test\DOCUMENT1.pdf"
.Attachments.Add DestwbPDF
'END
.display
End With
'OutMail.display
On Error GoTo 0
.Close SaveChanges:=False
End With
'Delete the file you have send
'Kill TempFilePath & TempFileName & FileExtStr
Set OutMail = Nothing
Set OutApp = Nothing
End Sub
推荐阅读
- python - 如何在 jupyter notebook 中设置日期时间列的格式
- javascript - 在构造函数中定义自定义元素的生命周期回调
- python - 我可以通过带有 LinearRegressor 的钩子记录训练损失吗?
- c# - 如何在 Visual Studio 中的对象初始化的“新”行上有 {
- php - 如果文本中使用了某个单词,您可以更改文本颜色吗?
- avfoundation - AVFoundation Recording Audio Feature,如何更改语言?
- java - java - 如何在Java Spring Boot Web应用程序中分配目录级用户访问权限
- android - 如何修复这个浮动按钮?
- javascript - JavaScript 中的 forEach 无法收集对象值
- html - 元素之间的间距,如选项卡功能