VBA .body文本太长。

huangapple go评论76阅读模式
英文:

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(&quot;Outlook.Application&quot;)
Set OutMail = OutObj.createitem(0)

With OutMail
    .to = Range(&quot;S29&quot;).Value
    .CC = Range(&quot;S34&quot;).Value
    .BCC = Range(&quot;S36&quot;).Value
    .Subject = &quot;Company - Construction phase Contact Information - &quot; &amp; Range(&quot;E4&quot;)
    .HTMLbody = &quot;&quot;
    .display
End With

End Sub

This is what I'm trying to put in the .HTMLbody = portion of the code.

&quot;Dear &quot; &amp; Range(&quot;S28&quot;) &amp; &quot;,&lt;br/&gt;&lt;br/&gt;Your &quot; &amp; Range(&quot;S30&quot;) &amp; &quot;work (Work Order #:&quot; &amp; Range(&quot;S31&quot;) &amp; &quot;) at &quot; &amp; Range(&quot;S32&quot;) &amp; &quot; 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. &lt;/br&gt;&quot; &amp; Range(&quot;S33&quot;) &amp; &quot; 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&#39;d to this email for your convenience.&lt;br/&gt;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.&lt;br/&gt;The following site readiness conditions are required for us to begin with construction:&lt;br/&gt;1- &quot; &amp; Range(&quot;A1&quot;) &amp; &quot;&lt;br/&gt;2- &quot; &amp; Range(&quot;A2&quot;) &amp; &quot;&lt;br/&gt;3- &quot;&amp; Range(&quot;A3&quot;) &amp; &quot;&lt;br/&gt;4- &quot;&amp;Range(&quot;A4&quot;) &amp; &quot;&lt;br/&gt;Lastly, to ensure we&#39;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.&lt;br/&gt;Thank you so much and please don&#39;t hesitate to reach out to us with any questions or concerns!&lt;br/&gt;Sincerely,&lt;br/&gt;&quot; &amp; Range(&quot;S35&quot;)

This returns a Run-Time Error "1004"

I've tried using"

msg1 = &quot;text long text long etc...&quot;
msg2 = &quot;text long text long etc...&quot;
msg3 = &quot;text long text long etc...&quot;
msg4 = &quot;text long text long etc...&quot;

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 = &quot;Dear &quot; &amp; Range(&quot;S28&quot;) &amp; &quot;,&lt;br/&gt;&lt;br/&gt;Your &quot; &amp; Range(&quot;S30&quot;) _
     &amp; &quot;Work (Work Order #:&quot; &amp; Range(&quot;S31&quot;) &amp; &quot;) at &lt;b&gt;&quot; &amp; Range(&quot;&lt;b&gt;S32&lt;b/&gt;&quot;) &amp; &quot;&lt;b/&gt;&quot; _
     &amp; &quot;will be moving to the construction phase of the Construction Process. &quot; 
(...)

The second is to concatenate the string step by step:

body = &quot;Dear &quot; &amp; Range(&quot;S28&quot;) &amp; &quot;,&lt;br/&gt;&lt;br/&gt;Your &quot; &amp; Range(&quot;S30&quot;)
body = body &amp; &quot;Work (Work Order #:&quot; &amp; Range(&quot;S31&quot;) &amp; &quot;) at &lt;b&gt;&quot; &amp; Range(&quot;&lt;b&gt;S32&lt;b/&gt;&quot;) &amp; &quot;&lt;b/&gt;&quot; 
body = body &amp; &quot;will be moving to the construction phase of the Construction Process. &quot; 
(...)

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(&quot;&lt;b&gt;S32&lt;b/&gt;&quot;) - what you meant is &quot;&lt;b&gt;&quot; &amp; Range(S32) &amp; &quot;&lt;b/&gt;&quot;. 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(&quot;S28&quot;)
    work = .Range(&quot;S30&quot;)
    workOrder = .Range(&quot;S31&quot;)
    workOrderAt = .Range(&quot;S32&quot;)
    (...)
End With

body = &quot;Dear &quot; &amp; customer &amp; &quot;,&lt;br/&gt;&lt;br/&gt;Your &quot; &amp; work
body = body &amp; &quot;Work (Work Order #:&quot; &amp; workOrder &amp; &quot;) at &lt;b&gt;&quot; &amp; workOrderAt &amp; &quot;&lt;b/&gt;&quot; 
body = body &amp; &quot;will be moving to the construction phase of the Construction Process. &quot; 
(...)

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(&quot;Outlook.Application&quot;) Set OutMail = OutObj.createitem(0)

With OutMail
    .to = Range(&quot;S29&quot;).Value
    .CC = Range(&quot;S34&quot;).Value
    .BCC = Range(&quot;S36&quot;).Value
    .Subject = &quot;Company - Construction phase Contact Information - &quot; &amp; Range(&quot;E4&quot;)
    .HTMLbody = _
        &quot;Dear &quot; &amp; Range(&quot;S28&quot;) &amp; &quot;,&lt;br/&gt;&lt;br/&gt;Your &quot; &amp; Range(&quot;S30&quot;) &amp; &quot;work (Work Order #:&quot; &amp; Range(&quot;S31&quot;) &amp; &quot;) at &quot; &amp; Range(&quot;S32&quot;) &amp; _
        &quot; 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&quot; &amp; _
        &quot; construction group and crew. &lt;/br&gt;&quot; &amp; Range(&quot;S33&quot;) &amp; &quot; is the Construction Project Manager assigned to this job.  They will be overseeing the construction process and&quot; &amp; _
        &quot; should be contacted directly if you have questions or concern with your project.  They have been CC&#39;d to this email for your convenience.&lt;br/&gt;Once construction is complete,&quot; &amp; _
        &quot; 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&quot; &amp; _
        &quot; those to get scheduled after we receive your inspection.&lt;br/&gt;The following site readiness conditions are required for us to begin with construction:&lt;br/&gt;1- &quot; &amp; _
        Range(&quot;A1&quot;) &amp; &quot;&lt;br/&gt;2- &quot; &amp; Range(&quot;A2&quot;) &amp; &quot;&lt;br/&gt;3- &quot; &amp; Range(&quot;A3&quot;) &amp; &quot;&lt;br/&gt;4- &quot; &amp; Range(&quot;A4&quot;) &amp; &quot;&lt;br/&gt;Lastly, to ensure we&#39;re including the appropriate representatives for this phase of&quot; &amp; _
        &quot; the project, please notify us if there are different or additional people we should include for construction coordination.  Please either add those individuals to&quot; &amp; _
        &quot; this email thread or send us the best name and contact information for us to reach out to them directly.&lt;br/&gt;Thank you so much and please don&#39;t hesitate to&quot; &amp; _
        &quot; reach out to us with any questions or concerns!&lt;br/&gt;Sincerely,&lt;br/&gt;&quot; &amp; Range(&quot;S35&quot;)
    .display 
End With

End Sub

huangapple
  • 本文由 发表于 2023年7月18日 00:39:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76706517.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定