英文:
VBA .body text is too long
问题
I am trying to design a spreadsheet for our department that will take information from the filled-in cells and draft an email to send to the customer and construction contact. The problem I'm running into is that the body of my email in VBA is too long. Please help.
My current code for the button is simply:
Private Sub CommandButton1_Click()
Dim OutObj As Object, OutMail As Object
Set OutObj = CreateObject("Outlook.Application")
Set OutMail = OutObj.createitem(0)
With OutMail
.to = Range("S29").Value
.CC = Range("S34").Value
.BCC = Range("S36").Value
.Subject = "Company - Construction phase Contact Information - " & Range("E4")
.HTMLbody = ""
.display
End With
End Sub
This is what I'm trying to put in the .HTMLbody = portion of the code.
"Dear " & Range("S28") & "<br/><br/>Your " & Range("S30") & " work (Work Order #:" & Range("S31") & ") at " & Range("S32") & " will be moving to the construction phase of the Construction Process. The job is currently with the scheduling department to be assigned to a specific construction group and crew. </br>" & Range("S33") & " is the Construction Project Manager assigned to this job. They will be overseeing the construction process and should be contacted directly if you have questions or concerns with your project. They have been CC'd to this email for your convenience.<br/>Once construction is complete, if additional support is needed for your final meter set(s), please contact me, and I will be happy to assist you. Keep in mind that it typically takes a few days for those to get scheduled after we receive your inspection.<br/>The following site readiness conditions are required for us to begin with construction:<br/>1- " & Range("A1") & "<br/>2- " & Range("A2") & "<br/>3- " & Range("A3") & "<br/>4- " & Range("A4") & "<br/>Lastly, to ensure we're including the appropriate representatives for this phase of the project, please notify us if there are different or additional people we should include for construction coordination. Please either add those individuals to this email thread or send us the best name and contact information for us to reach out to them directly.<br/>Thank you so much, and please don't hesitate to reach out to us with any questions or concerns!<br/>Sincerely,<br/>" & Range("S35")
This returns a Run-Time Error "1004"
I've tried using:
msg1 = "text long text long etc..."
msg2 = "text long text long etc..."
msg3 = "text long text long etc..."
msg4 = "text long text long etc..."
But either I'm doing this wrong or it doesn't work for me.
英文:
I am trying to design a spreadsheet for our department that will take information from the filled in cells and draft an email to send to the customer and construction contact. The problem I'm running into is that the body of my email in VBA is too long. It has to be formatted a certain way for the email. Please help.
My current code for the button is simply:
Private Sub CommandButton1_Click()
Dim OutObj As Object, OutMail As Object
Set OutObj = CreateObject("Outlook.Application")
Set OutMail = OutObj.createitem(0)
With OutMail
.to = Range("S29").Value
.CC = Range("S34").Value
.BCC = Range("S36").Value
.Subject = "Company - Construction phase Contact Information - " & Range("E4")
.HTMLbody = ""
.display
End With
End Sub
This is what I'm trying to put in the .HTMLbody = portion of the code.
"Dear " & Range("S28") & ",<br/><br/>Your " & Range("S30") & "work (Work Order #:" & Range("S31") & ") at " & Range("S32") & " will be moving to the construction phase of the Construction Process. The job is currently with the scheduling department to be assigned to a specific construction group and crew. </br>" & Range("S33") & " is the Construction Project Manager assigned to this job. They will be overseeing the construction process and should be contacted directly if you have questions or concerns with your project. They have been CC'd to this email for your convenience.<br/>Once construction is complete, if additional support is needed for your final meter set(s), please contact me and I will be happy to assist you. Keep in mind that it typically takes a few days for those to get scheduled after we receive your inspection.<br/>The following site readiness conditions are required for us to begin with construction:<br/>1- " & Range("A1") & "<br/>2- " & Range("A2") & "<br/>3- "& Range("A3") & "<br/>4- "&Range("A4") & "<br/>Lastly, to ensure we're including the appropriate representatives for this phase of the project, please notify us if there are different or additional people we should include for construction coordination. Please either add those individuals to this email thread or send us the best name and contact information for us to reach out to them directly.<br/>Thank you so much and please don't hesitate to reach out to us with any questions or concerns!<br/>Sincerely,<br/>" & Range("S35")
This returns a Run-Time Error "1004"
I've tried using"
msg1 = "text long text long etc..."
msg2 = "text long text long etc..."
msg3 = "text long text long etc..."
msg4 = "text long text long etc..."
But either I'm doing this wrong or it doesn't work for me.
答案1
得分: 3
拼接长行可能很麻烦。我的建议是a)将长文本分成较小的部分,b)使用中间变量存储要放入文本中的值。
如这里所述,你基本上有两种方法来创建长文本行。第一种是使用行继续语法(在行尾加上 _
)来告诉VBA编译器下一行是语句的一部分:
Dim body As String
body = "Dear " & Range("S28") & "<br/><br/>Your " & Range("S30") _
& "Work (Work Order #:" & Range("S31") & ") at <b>" & Range("<b>S32<b/>") & "<b/>" _
& "will be moving to the construction phase of the Construction Process. "
(...)
第二种方法是逐步连接字符串:
body = "Dear " & Range("S28") & "<br/><br/>Your " & Range("S30")
body = body & "Work (Work Order #:" & Range("S31") & ") at <b>" & Range("<b>S32<b/>") & "<b/>"
body = body & "will be moving to the construction phase of the Construction Process. "
(...)
第二种方法的缺点是稍微慢一些,但不值得担心。优点是你可以使用调试器逐行检查变量中写入的内容。此外,如果在创建语句时出现语法错误(例如缺少引号),很容易找到错误位置。
正如CDP1802在评论中指出的,你的错误是Range("<b>S32<b/>")
- 你的意思是"<b>" & Range(S32) & "<b/>"
。编译器无法捕获此问题,所以你仍然会得到运行时错误,但至少你知道问题出在哪里。
这些错误难以避免,但使用变量可以有所帮助:
Dim body As String
Dim customer As String, work As String, workOrder As String, workOrderAt As String
With ActiveSheet
customer = .Range("S28")
work = .Range("S30")
workOrder = .Range("S31")
workOrderAt = .Range("S32")
(...)
End With
body = "Dear " & customer & "<br/><br/>Your " & work
body = body & "Work (Work Order #:" & workOrder & ") at <b>" & workOrderAt & "<b/>"
body = body & "will be moving to the construction phase of the Construction Process. "
(...)
这有一些优点:
- 你可以使用调试器检查变量的内容。
- 当表格中的某些内容发生变化时,你只需更改变量的填充方式,而不需要更改主体内容的创建方式。
- 语句更易读。
- 你不再会犯你之前犯的错误(因为HTML格式设置与表格内容的填充独立设置)。
最后但并非最不重要的是,正如BigBen指出的,你需要将文本分配给HTMLBody
。使用Body
不会格式化你的文本(而是会在文本中保留这些标签)。
英文:
Concatenating long lines can be a pain in the ass. My advice is a) to break the long text into smaller pieces and b) use intermediate variables to store the values you want to put into the text.
As described here, you have basically 2 ways to create long text lines. The first is to use the line continue syntax (put _
at the end of a line) to signal the VBA compiler that the next line is part of the statement:
Dim body As String
body = "Dear " & Range("S28") & ",<br/><br/>Your " & Range("S30") _
& "Work (Work Order #:" & Range("S31") & ") at <b>" & Range("<b>S32<b/>") & "<b/>" _
& "will be moving to the construction phase of the Construction Process. "
(...)
The second is to concatenate the string step by step:
body = "Dear " & Range("S28") & ",<br/><br/>Your " & Range("S30")
body = body & "Work (Work Order #:" & Range("S31") & ") at <b>" & Range("<b>S32<b/>") & "<b/>"
body = body & "will be moving to the construction phase of the Construction Process. "
(...)
Disadvantage of the second method is that it is slightly slower - but nothing you should be worried about. Advantage is that you can check row by row what was written into the variable using the debugger. Also, if you have a syntax error when creating the statement (eg missing quotes), it is easy to locate.
As CDP1802 pointed in the comments, your error is Range("<b>S32<b/>")
- what you meant is "<b>" & Range(S32) & "<b/>"
. The compiler cannot catch this problem, so you would still get a runtime error, but at least you get an idea where the problem is.
These kind of mistakes are hard to avoid, however, it can help to use variables:
Dim body As String
Dim customer As String, work As String, workOrder As String, workOrderAt As String
With ActiveSheet
customer = .Range("S28")
work = .Range("S30")
workOrder = .Range("S31")
workOrderAt = .Range("S32")
(...)
End With
body = "Dear " & customer & ",<br/><br/>Your " & work
body = body & "Work (Work Order #:" & workOrder & ") at <b>" & workOrderAt & "<b/>"
body = body & "will be moving to the construction phase of the Construction Process. "
(...)
This has some advantages:
- You can check with the debugger the content of the variables.
- When something changes in your sheet, you just have to change how the variables are filled, but the creating of the body-content is not changed.
- The statement is easier to read.
- The mistake you made cannot happen any more (because the Html-format is set independent of filling the content of the sheet).
And last but not least, as BigBen pointed out, you need to assign the text to HTMLBody
. Using Body
will not format your text (instead you will have those tags within your text).
答案2
得分: 0
Range("")将导致运行时错误1004,请使用任何空单元格,如Range("ZZ1")。还可以使用下划线字符来换行 -
CDP1802
谢谢!
Private Sub CommandButton1_Click()
Dim OutObj As Object, OutMail As Object Set OutObj =
CreateObject("Outlook.Application") Set OutMail = OutObj.createitem(0)
With OutMail
.to = Range("S29").Value
.CC = Range("S34").Value
.BCC = Range("S36").Value
.Subject = "Company - Construction phase Contact Information - " & Range("E4")
.HTMLbody = _
"Dear " & Range("S28") & ",<br/><br/>Your " & Range("S30") & "work (Work Order #:" & Range("S31") & ") at " & Range("S32") & _
" will be moving to the construction phase of the Construction Process. The job is currently with the scheduling department to be assigned to a specific" & _
" construction group and crew.<br/>" & Range("S33") & " is the Construction Project Manager assigned to this job. They will be overseeing the construction process and" & _
" should be contacted directly if you have questions or concern with your project. They have been CC'd to this email for your convenience.<br/>Once construction is complete," & _
" if additional support is needed for your final meter set(s), please contact me and I will be happy to assist you. Keep in mind that it typically takes a few days for" & _
" those to get scheduled after we receive your inspection.<br/>The following site readiness conditions are required for us to begin with construction:<br/>1- " & _
Range("A1") & "<br/>2- " & Range("A2") & "<br/>3- " & Range("A3") & "<br/>4- " & Range("A4") & "<br/>Lastly, to ensure we're including the appropriate representatives for this phase of" & _
" the project, please notify us if there are different or additional people we should include for construction coordination. Please either add those individuals to" & _
" this email thread or send us the best name and contact information for us to reach out to them directly.<br/>Thank you so much and please don't hesitate to" & _
" reach out to us with any questions or concerns!<br/>Sincerely,<br/>" & Range("S35")
.display
End With
End Sub
英文:
Range("") will give you a Runtime Error 1004, use any blank cell like Range("ZZ1"). Also break the lines with an underscore character –
CDP1802
Thanks!
The answer for this was:
Private Sub CommandButton1_Click()
Dim OutObj As Object, OutMail As Object Set OutObj =
CreateObject("Outlook.Application") Set OutMail = OutObj.createitem(0)
With OutMail
.to = Range("S29").Value
.CC = Range("S34").Value
.BCC = Range("S36").Value
.Subject = "Company - Construction phase Contact Information - " & Range("E4")
.HTMLbody = _
"Dear " & Range("S28") & ",<br/><br/>Your " & Range("S30") & "work (Work Order #:" & Range("S31") & ") at " & Range("S32") & _
" will be moving to the construction phase of the Construction Process. The job is currently with the scheduling department to be assigned to a specific" & _
" construction group and crew. </br>" & Range("S33") & " is the Construction Project Manager assigned to this job. They will be overseeing the construction process and" & _
" should be contacted directly if you have questions or concern with your project. They have been CC'd to this email for your convenience.<br/>Once construction is complete," & _
" if additional support is needed for your final meter set(s), please contact me and I will be happy to assist you. Keep in mind that it typically takes a few days for" & _
" those to get scheduled after we receive your inspection.<br/>The following site readiness conditions are required for us to begin with construction:<br/>1- " & _
Range("A1") & "<br/>2- " & Range("A2") & "<br/>3- " & Range("A3") & "<br/>4- " & Range("A4") & "<br/>Lastly, to ensure we're including the appropriate representatives for this phase of" & _
" the project, please notify us if there are different or additional people we should include for construction coordination. Please either add those individuals to" & _
" this email thread or send us the best name and contact information for us to reach out to them directly.<br/>Thank you so much and please don't hesitate to" & _
" reach out to us with any questions or concerns!<br/>Sincerely,<br/>" & Range("S35")
.display
End With
End Sub
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论