我试图使用.Attachments.Add中的路径变量发送电子邮件,但是电子邮件不包括附件。当我在.Attachments.Add之后添加实际路径时,没有变量(如下所示),电子邮件包含附件。
.Attachments.Add "C:\Users\id\Desktop\file_name.xlsx"附加工作簿的方法(使用路径变量)曾经起过作用,但现在由于某种原因,工作簿没有附加。如果来自路径变量和实际路径的字符串是相同的,什么会导致工作簿在使用变量时不附加?下面是我的密码..。
这两个参数(title1和title2)是工作簿标题。
Sub Mail_Workbook_Comb1(ByVal title1 As String, ByVal title2 As String)
Dim OutApp As Object
Dim OutMail As Object
Dim id As String
Dim path1 As String
Dim path2 As String
Dim rnge As Range
Dim sht As Excel.Worksheet
Dim wdoc As Word.Document
Dim distroRnge As Range
id = LCase(workbooks("Supplier_Automation.xlsm").Sheets("Home").Range("C3"))
path1 = "C:\Users\" & id & "\Desktop\" & title1 & ".xlsx"
path2 = "C:\Users\" & id & "\Desktop\" & title2 & ".xlsx"
MsgBox path1
MsgBox path2
Set distroRnge = workbooks("Supplier_Automation.xlsm").Sheets("Distros").Range("A29")
Set sht = workbooks("Supplier_Automation.xlsm").Sheets("Email Template")
Set rnge = sht.Range("B1:B19")
rnge.CopyPicture Appearance:=xlScreen, Format:=xlPicture
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
Set wdoc = OutMail.GetInspector.WordEditor
On Error Resume Next
With OutMail
.To = myname@email.com
.CC = ""
.BCC = ""
.Subject = "This is the Subject line"
.body = ""
.Attachments.Add path1
.Attachments.Add path2
wdoc.Range.PasteAndFormat Type:=wdChartPicture
With wdoc
.InlineShapes(1).Height = 345
End With
.display 'or use .Send
End With
With Application
.EnableEvents = True
.ScreenUpdating = True
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
End Sub发布于 2018-05-15 20:49:06
当title1和title2参数传递工作簿标题时,我意识到它们不包括在调用Mail_Workbook_Comb1 Sub之前添加到工作簿中的日期。最后,我在path变量字符串的末尾添加了一个日期变量,这允许路径变量与我的桌面上保存的内容相匹配。
Dim edate As String
Dim today As String
today = Date
edate = Format(DateAdd("d", -1, today), "mmdd")
id = LCase(workbooks("Supplier_Automation.xlsm").Sheets("Home").Range("C3"))
path1 = "C:\Users\" & id & "\Desktop\" & title1 & " " & edate & ".xlsx"
path2 = "C:\Users\" & id & "\Desktop\" & title2 & " " & edate & ".xlsx"
path3 = "C:\Users\" & id & "\Desktop\" & title3 & " " & edate & ".xlsx"https://stackoverflow.com/questions/50358222
复制相似问题