电子邮件附件仅在静态编码时有效 - Excel Visual Basic

Email Attachment only working when statically coded - Excel Visual Basic

当附件路径被硬编码时,通过 Thunderbird 使用 Visual Basic 从 Excel 发送带有附件的电子邮件时一切正常。

attachment=C:\Users\Desktop2017\Desktop\customer\customerNumber\invoiceNumber.pdf"

但我需要根据单元格 M4 中的内容更改附件的部分文件路径,并根据单元格 J4 中的内容更改文件名。

示例:M4 值当前为 101。J4 值当前为 2000-01。输出应该是 "C:\Users\Desktop2017\Desktop\customer100-01.pdf"

我尝试使用 'Range' 来获取值并设置字符串,但不是从单元格或字符串中获取数据,而是输出等号后的任何内容。

我试过添加和移动引号,但目前没有任何效果。

提前感谢您的帮助,Dalton。

PS: 抱歉代码乱七八糟。

Private Sub EmailInvoice_Click()

Dim FileNumber As Integer
Dim retVal As Variant
Dim strName As String
Dim strFile As String
Dim wsCustomer As Worksheet


strName = Range("Q2").Value
strFile = Dir(strFolder & "*.xlsx")
Const MY_FILENAME = "C:\Users\Desktop2017\Dropbox\temp\invoice.BAT"


FileNumber = FreeFile

 'create batch file
Open MY_FILENAME For Output As #FileNumber
Print #FileNumber, "cd ""C:\Program Files (x86)\Mozilla Thunderbird"""
Print #FileNumber, "thunderbird -compose"; _
" to=" + ThisWorkbook.Sheets("hourlyInvoice01").Range("N21") _
+ ",subject=Invoice " + ThisWorkbook.Sheets("hourlyInvoice01").Range("J4") + ",format="; 1; _
",body=""<HTML><BODY>Hello    "; ThisWorkbook.Sheets("hourlyInvoice01").Range("N20") _
+ "&#44<BR><BR>Please see attached.<BR><BR>Thanks&#44 Dalton.<BR><BR><BR>Contact Info Text Line 1<BR>Contact Info Text Line 2<BR>Contact Info Text Line 3</BODY></HTML>"",attachment=C:\Users\Desktop2017\Desktop\test\script\someFile.txt"

Print #FileNumber, "exit"
Close #FileNumber

 'run batch file
retVal = Shell(MY_FILENAME, vbNormalFocus)

 ' NOTE THE BATCH FILE WILL RUN, BUT THE CODE WILL CONTINUE TO RUN.
If retVal = 0 Then
    MsgBox "An Error Occured"
    Close #FileNumber
    End
End If

 'Delete batch file
'Kill MY_FILENAME

End Sub

在该行之前添加这个

Dim FlName As String

FlName = "C:\Users\Desktop2017\Desktop\customer\" & Range("M4").Value & "\" & Range("J4").Value & ".pdf"

然后换行

"&#44<BR><BR>Please see attached.<BR><BR>Thanks&#44 Dalton.<BR><BR><BR>Contact Info Text Line 1<BR>Contact Info Text Line 2<BR>Contact Info Text Line 3</BODY></HTML>"",attachment=C:\Users\Desktop2017\Desktop\test\script\someFile.txt"

"&#44<BR><BR>Please see attached.<BR><BR>Thanks&#44 Dalton.<BR><BR><BR>Contact Info Text Line 1<BR>Contact Info Text Line 2<BR>Contact Info Text Line 3</BODY></HTML>"",attachment=" & FlName

注意:要连接字符串,请使用 & 而不是 +