【问题标题】:Email Attachment only working when statically coded - Excel Visual Basic电子邮件附件仅在静态编码时有效 - Excel Visual Basic
【发布时间】: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") _
+ "&#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

【问题讨论】:

  • ... customer\" &amp; Range("M4").Text &amp; "\" &amp; Range("J4").Text &amp; ".pdf .... 但我不确定你的代码应该放在哪里,因为代码似乎与问题不匹配。
  • 是的,我在测试时发生了一些变化,还需要更改一些内容以在线发布。感谢您的回复。

标签: excel batch-file thunderbird vba


【解决方案1】:

在该行之前添加这个

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

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

【讨论】:

    猜你喜欢
    • 2021-11-03
    • 2018-10-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-24
    • 1970-01-01
    • 2023-03-06
    • 2020-06-24
    • 1970-01-01
    相关资源
    最近更新 更多