在文本框中将单词加粗

时间:2018-06-12 12:58:22

标签: excel excel-vba textbox userform text-formatting vba

Stack Overflow中的新功能,我在使用VBA在Excel中构建宏。基本上我有一个带有多个选项卡的文件,其中包含表格中的信息,这些表格中包含文本,并且该文本中的一些单词是粗体并在每个选项卡中重复(让我们说所有者和流程)。我在一个文本框中显示这些信息,该文本框位于一个表格中,根据他们之前在列表框中选择的表格行,文本显示正确但忽略了文本格式(粗体和斜体)。有没有办法在文本框中显示文本格式,就像在表格中一样?

希望我已经让自己清楚了。

2 个答案:

答案 0 :(得分:2)

Shape文本框(非ActiveX)的典型示例

Sub BoxMaker()
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 217.5, 51#, _
        482.25, 278.25).Select
    Selection.Name = "SPLASH"
    Selection.Characters.Text = "Please Wait for Macro"
    With Selection.Characters(Start:=1, Length:=21).Font
        .Name = "Arial"
        .FontStyle = "Regular"
        .Size = 36
        .Strikethrough = False
        .Superscript = False
        .Subscript = False
        .OutlineFont = False
        .Shadow = False
        .Underline = xlUnderlineStyleNone
        .ColorIndex = xlAutomatic
    End With

    Selection.Characters(Start:=8, Length:=4).Font.Bold = True

    Selection.HorizontalAlignment = xlCenter
    Selection.VerticalAlignment = xlCenter
End Sub

enter image description here

您可以在文本框中设置文本格式,类似于单元格中的文本。

答案 1 :(得分:0)

使用Web浏览器控件在UserForm中可能的解决方法

  

由于你的评论:“......实际上我正在使用 MSForms.TextBox

可能的解决方法是使用b标记创建一个简单的HTML文件(例如, blabla 在普通文本粗体文本 ...中),以获得粗体文本和加载它进入 Webbrowser控件(需要参考Microsoft Internet Controls),例如通过WebBrowser1.Navigate ThisWorkbook.Path& “\ topic.htm”。

由于HTML通常使用 utf-8 编码,我演示了使用系统函数WideCharToMultiByte(API调用)的方法。

使用辅助函数(API调用)的示例调用

' declare and assign simple html string
  Dim htmlstring as String
  htmlstring = "<html><body><div>Normal text <b>bold text</b> further text</div></body></html>"
' write html file via helper procedure  
  writeUtf8 htmlstring, ThisWorkbook.Path & "\topic.htm"
' load html file into WebBrowswer control
  Me.WebBrowser1.Navigate ThisWorkbook.Path & "\topic.htm"

帮助程序

我建议为这些辅助函数编写一个单独的代码模块:

Option Explicit  ' declaration head of separate code module

' Declare API Function
Private Declare Function WideCharToMultiByte Lib "kernel32.dll" ( _
  ByVal CodePage As Long, _
  ByVal dwFlags As Long, _
  ByVal lpWideCharStr As Long, _
  ByVal cchWideChar As Long, _
  ByVal lpMultiByteStr As Long, _
  ByVal cbMultiByte As Long, _
  ByVal lpDefaultChar As Long, _
  ByVal lpUsedDefaultChar As Long) As Long


Public Sub writeUtf8(ByVal s As String, ByVal datei As String)
' Purpose: save HTML String in utf-8 mode 
' Note:    called by  writeUtf8 htmlstring, thisworkbook.path & "\topic.htm"
Dim file As Integer
Dim B() As Byte
  file = FreeFile
  Open datei For Binary Access Write Lock Read Write As #file
  getUtf8 s, B
  Put #file, , B
  Close #file
End Sub

Private Sub getUtf8(ByRef s As String, ByRef B() As Byte)
' Note: called by above helper function; uses API call (see declaration head)
Const CP_UTF8 As Long = 65001
Dim len_s As Long
Dim ptr_s As Long
Dim size As Long
  Erase B
  len_s = Len(s)
  If len_s = 0 Then _
    err.Raise 30030, , "Len(WideChars) = 0"
  ptr_s = StrPtr(s)
  size = WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, 0, 0, 0, 0)
  If size = 0 Then _
    err.Raise 30030, , "WideCharToMultiByte() = 0"
  ReDim B(0 To size - 1)
  If WideCharToMultiByte(CP_UTF8, 0, ptr_s, len_s, VarPtr(B(0)), size, 0, 0) = 0 Then _
    err.Raise 30030, , "WideCharToMultiByte(" & Format$(size) & ") = 0"
End Sub