【问题标题】:excel-vba: Assign bold font to lines in string textexcel-vba:将粗体字体分配给字符串文本中的行
【发布时间】:2018-03-02 02:25:31
【问题描述】:

我有一个包含多行文本的字符串元素。但是,如果我将此字符串用作 Outlook 正文文本,则它们将没有像粗体字体这样的格式到特定行。我想使用这个字符串并将粗体字体应用于特定行(例如,所有包含符号“@”和 for 循环的行)。

在将字符串合并为电子邮件正文之前,如何在代码中实现这一点?我想我必须从中创建一个新对象,然后将其放入电子邮件正文中。

 Sub Send_Email()
 Dim OutApp As Object
 Dim OutMail As Object

 Application.ScreenUpdating = False
 Set OutApp = CreateObject("Outlook.Application")
 OutApp.Session.Logon
 Dim iFile As Integer
 Dim strVar As String 'Pre-definition of the input variable

 iFile = FreeFile

 Open "FILE_PATH" For Input As #iFile
 strVar = Input(LOF(iFile), iFile) 'Definition of the input varibale
 Close #iFile

 Set OutMail = OutApp.CreateItem(0)
 On Error Resume Next

 With OutMail
 .To = ""
 .BCC = ""
 .Subject = ""    
 .BodyFormat = olFormatRichText
 .Body = strVar 'Creation of the e-mail text body
 .Display 'Or use .Send
End With
On Error GoTo 0

End Sub

【问题讨论】:

标签: vba excel email outlook


【解决方案1】:

您需要将 HTMLBody 属性设置为格式正确的 HTML 文本:

strvar = "<html><body>some <b>bold</b> text<br>and <i>italic</i> too</body></html>"
...
.HTMLBody = strVar 

【讨论】:

  • 好吧,我的 strVar 已经包含文本片段。我想给他们分配粗体,而不是自己写字符串。字符串文本源自 rtf 文件
  • 如果是RTF文件,为什么不设置RtfBody属性呢?
  • 输出是一样的,我只是尝试过 HTML 是否会保留粗体字体
  • 我不确定你的意思。
【解决方案2】:

如果活动单元格中包含“@”,以下是加粗字符串的代码。你需要弄清楚你需要它做什么,但这应该有助于你开始。它目前仅适用于活动单元格。

If ActiveCell.Value Like "*@*" Then
  ActiveCell.Font.Bold = True
End If

【讨论】:

  • 如何使 ActiveCell 之类的东西适用于字符串?那是一百万美元的问题
  • 我会将字符串粘贴到你的位置,然后加粗
  • 这个带有“@”的字符串是电子邮件还是正文?
  • 在电子邮件的正文中
猜你喜欢
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 1970-01-01
  • 2015-10-17
  • 1970-01-01
相关资源
最近更新 更多