【问题标题】:Trying to assign color to string variable (Excel VBA)尝试将颜色分配给字符串变量(Excel VBA)
【发布时间】:2018-03-21 23:41:48
【问题描述】:

我正在使用 excel 来收集、格式化和通过电子邮件将信息发送给其他用户。我想用不同的颜色格式化此代码生成的信息,以使其脱颖而出。我知道“.Font.colorindex = ??”,但对变量类型的内部结构不够熟悉,无法获得我想要实现的功能......

例子:

Private Sub CommandButton1_Click()

Dim outlookApp As Outlook.Application
Dim outlookMail As Outlook.MailItem
Dim ExtractedText As String

Set outlookApp = New Outlook.Application
Set outlookMail = outlookApp.CreateItem(olMailItem)

With outlookMail

    .To = "person@company.com"
    .CC = ""
    .BCC = ""

    .Subject = "This is a test email automated by excel"
    .BodyFormat = olFormatHTML
    .HTMLBody = "This is an automated message generated from an Excel macro.<p>Info 1 " & Sheet1.Cells(2, 1) & " Info 2: " & Sheet1.Cells(2, 2) & " Info 3 " & Sheet1.Cells(2, 3) & ".  Please see attached."
    .Attachments.Add "C:\Users\xyz\Desktop\Test Attachment.xlsx"
    .Importance = olImportanceHigh
    .Send

End With

Set outlookMail = Nothing
Set outlookApp = Nothing

End Sub

我想将“ExtractedText”定义为 Cell(2, 1),为其设置颜色并将其插入到电子邮件正文中,以代替上面使用的“Sheet1.Cells(2, 1)”。我花了几个小时搜索和尝试不同的方法来展示它。在等待我订购的 VBA 编码书籍到货时,我想我应该问问社区。​​p>

提前致谢!

【问题讨论】:

  • 所以,如果您知道应该使用.font.colorindex = ##,那么只需查找调色板(例如:msdn.microsoft.com/en-us/vba/excel-vba/articles/…)。另外,您是否尝试在将单元格中的值放入电子邮件之前或之后为它们着色?

标签: excel string vba variables colors


【解决方案1】:

您只需要生成一个简单的 HTML 元素来包装该值,然后将整个内容插入到文本字符串中。

dim strExtractedText as string

strExtractedText = "<font style='color:red'>" & Sheet1.Cells(2, 1).value & "</font>"

...
.HTMLBody = "This is an automated message generated from an Excel macro.<p>Info 1 " & strExtractedText & " Info 2: " & Sheet1.Cells(2, 2) & " Info 3 " & Sheet1.Cells(2, 3) & ".  Please see attached."
...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-10-17
    • 2022-01-14
    • 2018-11-08
    • 2021-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    相关资源
    最近更新 更多