通过互联网后,我仍然遇到了在Excel生成的电子邮件中插入新行的问题。
我已经尝试了以下代码:
Email = "[email protected]"
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 = "[email protected]"
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 = "[email protected]"
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句