在某些计算机

时间:2015-12-14 16:17:16

标签: excel vba excel-vba email outlook

我们有一个Excel电子表格,用于为潜在客户创建报价。通过使用VBA,它可以生成PDF并使用Outlook将它们通过电子邮件发送给客户。它从主页上的单元格中获取客户的电子邮件地址,用户填写客户的详细信息。

几周前,它已经停止在Outlook的“收件人”字段中填写客户的电子邮件地址,即使它已填写在主页上。

当我们更改此电子表格中的任何内容(包括代码)时,我们将其保存为新的“修订版”(保留所有以前的修订版。)回过头部版本,我现在发现它们都不起作用。这很奇怪,因为他们之前肯定做过。我正在使用Office 2016(虽然我刚刚升级,但这个问题是最近的。)运行Office 2013的计算机也无法运行。但是,运行Office 2007的计算机可以正常运行。

关于为什么现在这个问题的任何想法,以及为什么它只是某些Office版本的问题?以下是代码片段:

    Private Sub send_as_pdf_Click()
    On Error GoTo ErrMsg
    Dim strPath As String, strFName As String
    Dim OutApp As Object, OutMail As Object

    strPath = Environ$("temp") & "\"

    strFName = Sheets("Quotation").Name & " " & Range("G18") & ".pdf"

    Sheets("Quotation").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        strPath & strFName, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    strPath2 = Environ$("temp") & "\"

    strFName2 = Sheets("Quotation Offer Letter").Name & " " & Range("G18") & ".pdf"

    Sheets("Quotation Offer Letter").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        strPath2 & strFName2, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    strPath3 = Environ$("temp") & "\"

    strFName3 = Sheets("Additional Works Required").Name & " " & Range("G18") & ".pdf"

    Sheets("Additional Works Required").ExportAsFixedFormat Type:=xlTypePDF, Filename:= _
        strPath3 & strFName3, Quality:=xlQualityStandard, _
        IncludeDocProperties:=True, IgnorePrintAreas:=False, OpenAfterPublish:=False

    Set OutApp = CreateObject("Outlook.Application")
    Set OutMail = OutApp.CreateItem(0)

    strbody = "<p style='color:#2C3E50;font-family:Calibri;font-size:11pt;'>Hi " & Range("C9") & ",</p>"

    strbody2 = "<p style='color:#2C3E50;font-family:Calibri;font-size:11pt;'>The content of the email goes here.</p>"

    On Error Resume Next
    With OutMail
        .Display
        .To = Range("C19")
        .CC = ""
        .BCC = "first@person.com" & ";" & "second@person.com" & ";" & Range("I6") & ";"
        .Subject = "Quotation"
        .HTMLBody = strbody & strbody2 & .HTMLBody
        .Attachments.Add strPath & strFName
        .Attachments.Add strPath2 & strFName2
        .Attachments.Add strPath3 & strFName3
        .Attachments.Add ("C:\Terms and Conditions of Business of Our Business.pdf")
        .Attachments.Add ("C:\Warranty Statement of Our Business.pdf")
    End With

    Kill strPath & strFName
    On Error GoTo 0
    Set OutMail = Nothing
    Set OutApp = Nothing
Exit Sub

ErrMsg:
MsgBox ("MUST enter Issue Number, Date & Customer Info."), , "Customer Email Error Message"

End Sub

1 个答案:

答案 0 :(得分:2)

试试这个:

注释此行,以便您可以查看错误是什么并进行调试。

'On Error Resume Next

执行此操作后,您应该能够在行中看到错误:

.To = Range("C19")

enter image description here

在excel中,这将返回范围的值,但是您要将该值发送到期望字符串的另一个应用程序Outlook

我改变了这一行:

.To = Range("C19").Value2

并且有效