【发布时间】:2017-06-07 03:46:26
【问题描述】:
当附件路径是硬编码时,我可以使用 Visual Basic 通过 Thunderbird 从 Excel 发送带有附件的电子邮件,一切正常。
attachment=C:\Users\Desktop2017\Desktop\customer\customerNumber\invoiceNumber.pdf"
但我需要根据单元格 M4 中的内容更改附件的部分文件路径,并根据单元格 J4 中的内容更改文件名。
示例:M4 值当前为 101。J4 值当前为 2000-01。输出应该是“C:\Users\Desktop2017\Desktop\customer\101\2000-01.pdf”
我尝试使用“范围”来获取值并设置字符串,但它不是从单元格或字符串中获取数据,而是输出等号后的任何内容。
我尝试添加和移动引号,但此时没有任何效果。
提前感谢您的帮助,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") _
+ ",<BR><BR>Please see attached.<BR><BR>Thanks, 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
【问题讨论】:
-
... customer\" & Range("M4").Text & "\" & Range("J4").Text & ".pdf ....但我不确定你的代码应该放在哪里,因为代码似乎与问题不匹配。 -
是的,我在测试时发生了一些变化,还需要更改一些内容以在线发布。感谢您的回复。
标签: excel batch-file thunderbird vba