在某些计算机上,Excel VBA中的空白“收件人”字段为空

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

几个星期前,即使在主表单上填写了客户的电子邮件地址,也不会填写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 

尝试这个:

注释这一行,以便您可以看到错误和debugging。

 'On Error Resume Next 

这样做后,你应该能够看到行内的错误:

 .To = Range("C19") 

在这里输入图像说明

在Excel中,这将返回范围的值,但是您将该值发送给期待string的另一个应用程序Outlook

我改变了这一行:

 .To = Range("C19").Value2 

它的工作