在浏览互联网后,我仍然遇到了在 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
您会看到它们产生相同的东西(并且它与您作为屏幕截图放置的内容相匹配)。我的观点是,我认为问题在于您对 body 变量所做的操作,而不是 Excel/VBA 构造字符串的方式。
下面是一些使用 Outlook 创建电子邮件的代码,它给出了预期的结果。如果您不使用 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
关于vba - nextline vba 与 excel 生成电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34882744/