You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Excel VBA生成Outlook邮件HTML内容时遇Expected Statement错误求助

Excel VBA生成Outlook邮件HTML正文时"Expected Statement"错误的解决办法

错误原因及修复方案

  • 续行符使用错误:VBA中续行符_的前面必须有空格,原代码倒数第二行的&_缺少空格,导致语法解析失败。
  • 字符串内双引号未转义:在VBA字符串中,若要包含双引号,需用两个连续双引号""转义;原代码中HTML链接的href="http://www.google.com"里的双引号会被VBA识别为字符串结束符,引发错误。

修复后的代码

方案一(转义双引号):

strBody = "<BODY>Cell(s) " & Target.Address(False, False) & _
        " were modified on " & _
        Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
        " by " & Environ$("username") & _
        "<br> <br> Select the Invoicing Tab in the <a href=""http://www.google.com"">Workbook</a></BODY>"

方案二(用单引号替代HTML中的双引号,无需转义):

strBody = "<BODY>Cell(s) " & Target.Address(False, False) & _
        " were modified on " & _
        Format$(Now, "mm/dd/yyyy") & " at " & Format$(Now, "hh:mm:ss") & _
        " by " & Environ$("username") & _
        "<br> <br> Select the Invoicing Tab in the <a href='http://www.google.com'>Workbook</a></BODY>"

内容的提问来源于stack exchange,提问作者k1dr0ck

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 07:50:49