与excel生成的电子邮件的nextline vba

通过互联网后,我仍然遇到了一个问题,插入到Excel生成的电子邮件的新行。

我已经尝试了以下代码:

Email = "person@email.com" Subj = "Subject" body = "Hello Person," & vbCr & _ "message" & vbCr & _ "Example: " & vbCr & _ "another example" & vbCr & _ "another example" & vbCr & _ "Thank you. " 

  body = "Hello Person," & vbCr & _ body = body & "message" & vbCr & _ etc etc 

我试了

  body = "Hello Person," & vbNewline & _ body = body & "message" & vbNewline & _ etc etc 

第一个例子工作正常,但没有额外的行。 第二个示例抛出一个不匹配的错误

任何想法是什么问题?

在这里输入图像说明 代码来生成电子邮件

  URL = "mailto:" & Email & "?subject=" & Subj & "&body=" & Msg & " " & body ShellExecute 0&, vbNullString, URL, vbNullString, vbNullString, vbNormalFocus Private Declare Function ShellExecute Lib "shell32.dll" _ Alias "ShellExecuteA" (ByVal hwnd As Long, ByVal lpOperation As String, _ ByVal lpFile As String, ByVal lpParameters As String, ByVal lpDirectory As String, _ ByVal nShowCmd As Long) As Long 

这与@Slubee的回答非常相似,但是我认为这有点更完整,希望能帮助你找出问题所在。 试试这个代码:

 Option Explicit Sub test() Dim email As String, _ subj As String, _ body As String email = "Foo@boo.com" subj = "Subject goes here" body = "Hello Person," & vbNewLine & _ "message" & vbNewLine & _ "Example:" & vbNewLine & _ "another example" & vbNewLine & _ "another example" & vbNewLine & _ "Thank you. " MsgBox body body = "Hello Person," & vbCr & _ "message" & vbCr & _ "Example:" & vbCr & _ "another example" & vbCr & _ "another example" & vbCr & _ "Thank you. " MsgBox body End Sub 

你会看到它们产生的是同样的东西(和你的屏幕截图相匹配)。 我的意思是,我认为这个问题是与你正在做什么的身体variables,而不是在Excel / VBA构造string的方式。

以下是一些使用Outlook创build电子邮件的代码,可以给出预期的结果。 如果你不使用Outlook,你将不得不澄清你用来生成电子邮件的程序:

 Option Explicit Sub test() Dim email As String, _ subj As String, _ body As String Dim OutApp As Object, _ OutMail As Object email = "Foo@boo.com" subj = "Subject goes here" body = "Hello Person," & vbCr & _ "message" & vbCr & _ "Example:" & vbCr & _ "another example" & vbCr & _ "another example" & vbCr & _ "Thank you. " Set OutApp = CreateObject("Outlook.Application") Set OutMail = OutApp.CreateItem(0) On Error Resume Next With OutMail .To = email .CC = "" .BCC = "" .Subject = subj .body = body .Display End With On Error GoTo 0 Set OutMail = Nothing Set OutApp = Nothing End Sub 

使用vbNewline添加行。

“你好人”,&Vbnewline&