excel-vba:将粗体字体指定给字符串文本中的行

时间:2022-07-25 22:18:01

I have a string element that contains several lines of text. If I use this string as an outlook body text though, they will have no format like bold font to specific lines. I would like to take this string and apply bold font to specific lines (e.g. to all which contain the symbol "@" with a for loop).

我有一个包含几行文本的字符串元素。如果我使用此字符串作为outlook正文文本,它们将没有像特定行的粗体字格式。我想把这个字符串和粗体字应用于特定的行(例如,包含带有for循环的符号“@”的所有行)。

How can I implement that in my code before I incorporate the string as body text of my e-mail? I guess I must create a new object out of it and get that one instead into the body of the e-mail.

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

 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

2 个解决方案

#1


0  

You need to set the HTMLBody property to a properly formatted HTML text:

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

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

#2


0  

Here's the code to bold a string in the active cell if it contains a "@" in it. You'll need to figure out what you need it to do, but this should help get you started. It only works on the active cell as of right now.

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

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

#1


0  

You need to set the HTMLBody property to a properly formatted HTML text:

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

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

#2


0  

Here's the code to bold a string in the active cell if it contains a "@" in it. You'll need to figure out what you need it to do, but this should help get you started. It only works on the active cell as of right now.

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

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