在VBA中格式化文本字符串的特定部分

时间:2015-06-15 13:54:35

标签: excel vba excel-vba outlook format

我正在创建一个宏,它将保存当前工作簿,创建新的Outlook消息并将文件附加到消息中。我的宏做了那个,但我无法根据自己的喜好格式化电子邮件正文中的文本。

<head>
    ...

   <!-- You didn't include jquery -->
   <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

    <script type="text/javascript">
    $(document).ready(function() {
        $("#eventChoose").change(function() {
            // I don't see budget defined anywhere. Is it an id? 
            $('#budget').focus();
        });
    });
    </script>
</head>

我希望在电子邮件中显示以下消息(带格式)。

  

所有

     

请批准下面附带的请求&#34; rtype&#34;。

     

客户: Stackoverflow

所以,&#34;客户&#34;需要大胆。我已经厌倦了多个解决方案,但它们不起作用,因为这是创建Outlook邮件对象。

任何帮助将不胜感激。

**

  

解决方案:要使HTML标记正常工作,请将正文类型更改为html   &#34; .HTMLBody&#34 ;.并且您将能够使用HTML标签。对迪克的称赞   Kusleika

**

2 个答案:

答案 0 :(得分:1)

HTML tags do work. I don't know why you say they don't.

sBody = "All,<br /><br />Please Approve attached request for " & rType & ".<br /><br /><strong>Customer:</strong> " & customer & "<br />"

then instead of the .Body property, use .HTMLBody

.HTMLBody = sBody

答案 1 :(得分:0)

你有几个选择

1)使用HTML就像一些人评论过的

2)将该文本放在隐藏的工作表上并根据需要对其进行格式化,然后将其作为范围引入正文。 .Body = sheet(&#34; hidden_​​Body&#34;)。range(&#34; A1:B10&#34;)

3)你可以尝试使用下面的东西(请注意下面用于在字符串中添加一个wingding字符,需要修改以符合你的目的)

Sub Build_Wingdings(Sh As Worksheet, rng As Range)

Dim cur_L As Integer
cur_L = 1
Sheets("Word_Specifications").Range("BZ9").Copy
Sh.Range(rng.Address).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False

Application.CutCopyMode = False

With Sheets("Word_Specifications")
    .Select
    For Each cell In .Range(.Range("Word_Standard_Start").Address, .Range("Word_Standard_Start").End(xlDown).Address)
        If cell.value = "" Then
        Else
            L = Len(cell.value) + 1
            With Sh.Range(rng.Address)
                With .Characters(start:=cur_L, Length:=L).Font
                    .Name = "Arial"
                    .FontStyle = "Regular"
                    .Size = 9
                    .Bold = False
                    .Strikethrough = False
                    .Superscript = False
                    .Subscript = False
                    .OutlineFont = False
                    .Shadow = False
                    .Underline = xlUnderlineStyleNone
                    .ThemeColor = xlThemeColorLight1
                    .TintAndShade = 0
                    .ThemeFont = xlThemeFontNone
                End With

                cur_L = cur_L + L

                If .value <> "" Then
                    add_Wingdings cur_L, 1, Sh, rng
                    cur_L = cur_L + 2
                End If
            End With
        End If
    Next
End With


End Sub

Sub add_Wingdings(start As Integer, Length As Integer, Sh As Worksheet, rng As Range)
With Sh.Range(rng.Address).Characters(start:=start, Length:=Length).Font
    .Name = "Wingdings 3"
    .FontStyle = "Regular"
    .Size = 9
    .Bold = False
    .Strikethrough = False
    .Superscript = False
    .Subscript = False
    .OutlineFont = False
    .Shadow = False
    .Underline = xlUnderlineStyleNone
    .ThemeColor = xlThemeColorLight1
    .TintAndShade = 0
    .ThemeFont = xlThemeFontNone
End With
End Sub