VBA 格式化字符串

VBA formatting strings

截至目前,一切都是红色的。我需要冒号后的所有内容都是黑色而不是斜体。

您可以更改此行以使文本变黑:

.Color.RGB = RGB(216, 3, 33)

这行要删除斜体:

.Italic = msoFasle

编辑:

如果您有单元格位置,稍后可以对字符串执行此操作:

With Cells(1, 1).Characters(9, 21).Font
     .Color = vbBlack
     .Italic = False
     .Name = "Corbel"
     .Size = 20

This 显示如何使用字符

编辑:

要更改文本框的部分文本,您可以声明两个包含格式不同的字符串的变量,并将它们附加到文本框。

它将遵循以下形式:

MyTextBox.Value = MyStringVariable

或者您的情况:

MyTextBox.Value = MyStringVariable1 & MyStringVariable2

宏录制器发现以下内容:

Sub test()
    ActiveSheet.Shapes.Range(Array("txtbox1")).Select
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
        "Choisir: wefhweufhwef 344tr saefaefa" 'Entering some text'
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 5).Font 'Selecting some font and changing some settings
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(0, 0, 255)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
End Sub

如果不先选择文本框,我还没有设法做到这一点(??也许有人可以提供帮助,通常这并不难)。您可以输入带有字符串的文本,您可以使用

在其中找出冒号的位置
dim MyString as string
dim intColon as integer
Mystring = "cwsvws:wifvwhivw"
Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = MyString
intColon = instr(MyString, ":")

并以此为字符数来插入宏录制器的代码。然后您可以根据需要格式化不同的文本块。

With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(intColon, 5).Font
             .Fill.ForeColor.RGB = RGB(0, 0, 255)
             .Fill.Transparency = 0
             .Fill.Solid
             .Size = 11
             .Name = "+mn-lt"
End With

希望对您有所帮助。

您只需要将冒号后面的字符隔离开,并根据需要设置颜色即可:

编辑:忘记了斜体位……

Sub test()

    sCallOut = "ACTION: [Insert Callout Here]"
    Set oShp = pptSld.Shapes.AddTextbox(msoTextOrientationHorizontal, 110, 100, 557.28, 94.32)
    oShp.Line.Visible = msoFalse
    oShp.TextFrame.TextRange.Text = sCallOut
    oShp.TextFrame.TextRange.ParagraphFormat.Alignment = ppAlignCenter

    With oShp.TextFrame.TextRange.Font
       .Name = "Corbel"
       .Italic = msoTrue
       .Size = 20
       .Color.RGB = RGB(216, 3, 33)
       .Bold = msoTrue
    End With

    With oShp.TextFrame.TextRange
        With .Characters(InStr(.Characters, ":") + 1, .Length).Font
            .Color.RGB = RGB(0, 0, 0)
            .Italic = msoFalse
        End With
    End With

    Set oShp = Nothing

End Sub