【问题标题】:Create default textbox with formatted text in Excel在 Excel 中使用格式化文本创建默认文本框
【发布时间】:2017-03-23 22:38:56
【问题描述】:

根据我的观点,我遇到了一个非常简单的问题,但是我找不到任何解决方案。

我正在尝试创建一个默认文本框(插入 -> 形状 -> 文本框),具有一定的填充颜色(蓝色,重音 1,浅色 80%)和一定的文本(工作完成:[空段落] 调查结果: [空段]结论:[空段]),文本框内的文字为红色字体颜色,加粗。 我在创建此文本框时尝试录制宏,但是运行宏时总是收到一条错误消息:。 由于我经常需要这个文本框(没有黑色文本,这只是一个示例),因此最好有一个可以附加到自定义功能区的宏。

我发现使用 VBA 在文本框中更改格式非常困难。但是还有人知道如何使用 VBA 来完成我的默认文本框吗?!

代码:

    Sub Textbox()
'
' Textbox Macro
'
' Keyboard Shortcut: Ctrl+Shift+Y
'
    ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 59.25, 48.75, 292.5 _
        , 109.5).Select
    With Selection.ShapeRange.Fill
        .Visible = msoTrue
        .ForeColor.ObjectThemeColor = msoThemeColorText2
        .ForeColor.TintAndShade = 0
        .ForeColor.Brightness = 0.8000000119
        .Transparency = 0
        .Solid
    End With
    DEBUG HERE -> With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).Font.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
        .Solid
    End With
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
        .Solid
    End With
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font.Fill
        .Visible = msoTrue
        .ForeColor.RGB = RGB(255, 0, 0)
        .Transparency = 0
        .Solid
    End With
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _
        "Work Done:" & Chr(13) & "" & Chr(13) & "Findings:" & Chr(13) & "" & Chr(13) & "Conclusion:"
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).ParagraphFormat. _
        FirstLineIndent = 0
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4).Font
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(5, 7).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).ParagraphFormat. _
        FirstLineIndent = 0
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).Font
        .BaselineOffset = 0
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1
        .Fill.ForeColor.TintAndShade = 0
        .Fill.ForeColor.Brightness = 0
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).ParagraphFormat _
        .FirstLineIndent = 0
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).ParagraphFormat. _
        FirstLineIndent = 0
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).Font
        .BaselineOffset = 0
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1
        .Fill.ForeColor.TintAndShade = 0
        .Fill.ForeColor.Brightness = 0
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
    Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).ParagraphFormat _
        .FirstLineIndent = 0
    With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font
        .BaselineOffset = 0
        .Bold = msoTrue
        .NameComplexScript = "+mn-cs"
        .NameFarEast = "+mn-ea"
        .Fill.Visible = msoTrue
        .Fill.ForeColor.RGB = RGB(255, 0, 0)
        .Fill.Transparency = 0
        .Fill.Solid
        .Size = 11
        .Name = "+mn-lt"
    End With
End Sub

【问题讨论】:

  • 您应该包含录制的宏代码。此外,您可以调试它以找出究竟是什么导致了问题,而不是运行宏。要进行调试,您可以在 VBA 编辑器中使用 STEP INTO(通常是 F8)。
  • @AdamVincent:我上传了我的代码并在它调试的地方插入了一条评论。但是我不知道是什么导致了问题。
  • 您的宏正在尝试格式化文本,但此时文本框中没有文本。
  • @Rory:谢谢,你是对的。如果我将Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _ "Work Done:" & Chr(13) & "" & Chr(13) & "Findings:" & Chr(13) & "" & Chr(13) & "Conclusion:" Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).ParagraphFormat. _ FirstLineIndent = 0 插入到ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 59.25, 48.75, 292.5 _ , 109.5).Select 下方,它可以正常工作。再次感谢!

标签: vba excel textbox


【解决方案1】:

试试这个:

   Sub Textbox()
'
' Textbox Macro
'
' Keyboard Shortcut: Ctrl+Shift+Y
'
    With ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 59.25, 48.75, 292.5, 109.5) '<--| add and reference a new shape
        With .Fill '<--| reference referenced shape 'Fill' property
            .Visible = msoTrue
            .ForeColor.ObjectThemeColor = msoThemeColorText2
            .ForeColor.TintAndShade = 0
            .ForeColor.Brightness = 0.8000000119
            .Transparency = 0
            .Solid
        End With
        With .TextFrame2 '<--| reference referenced shape 'TextFrame2' property
            .TextRange.Characters.Text = "Work Done:" & Chr(13) & "" & Chr(13) & "Findings:" & Chr(13) & "" & Chr(13) & "Conclusion:"
            With .TextRange.Characters(1, 11).Font.Fill
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
                .Transparency = 0
                .Solid
            End With
            With .TextRange.Characters(13, 10).Font.Fill
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
                .Transparency = 0
                .Solid
            End With
            With .TextRange.Characters(24, 11).Font.Fill
                .Visible = msoTrue
                .ForeColor.RGB = RGB(255, 0, 0)
                .Transparency = 0
                .Solid
            End With
            .TextRange.Characters(1, 11).ParagraphFormat.FirstLineIndent = 0
            With .TextRange.Characters(1, 4).Font
                .BaselineOffset = 0
                .Bold = msoTrue
                .NameComplexScript = "+mn-cs"
                .NameFarEast = "+mn-ea"
                .Fill.Visible = msoTrue
                .Fill.ForeColor.RGB = RGB(255, 0, 0)
                .Fill.Transparency = 0
                .Fill.Solid
                .Size = 11
                .Name = "+mn-lt"
            End With
            With .TextRange.Characters(5, 7).Font
                .BaselineOffset = 0
                .Bold = msoTrue
                .NameComplexScript = "+mn-cs"
                .NameFarEast = "+mn-ea"
                .Fill.Visible = msoTrue
                .Fill.ForeColor.RGB = RGB(255, 0, 0)
                .Fill.Transparency = 0
                .Fill.Solid
                .Size = 11
                .Name = "+mn-lt"
            End With
        End With
    End With
End Sub

【讨论】:

  • 这也很好用!非常感谢您在这方面的帮助!
【解决方案2】:

这是我最后的代码:

Sub Textbox() ' ' Textbox Macro ' ' Keyboard Shortcut: Ctrl+Shift+Y ' ActiveSheet.Shapes.AddTextbox(msoTextOrientationHorizontal, 59.25, 48.75, 292.5 _ , 109.5).Select Selection.ShapeRange(1).TextFrame2.TextRange.Characters.Text = _ "Work Done:" & Chr(13) & "" & Chr(13) & "Findings:" & Chr(13) & "" & Chr(13) & "Conclusion:" Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).ParagraphFormat. _ FirstLineIndent = 0 With Selection.ShapeRange.Fill .Visible = msoTrue .ForeColor.ObjectThemeColor = msoThemeColorText2 .ForeColor.TintAndShade = 0 .ForeColor.Brightness = 0.8000000119 .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 11).Font.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font.Fill .Visible = msoTrue .ForeColor.RGB = RGB(255, 0, 0) .Transparency = 0 .Solid End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(1, 4).Font .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(5, 7).Font .BaselineOffset = 0 .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).ParagraphFormat. _ FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(12, 1).Font .BaselineOffset = 0 .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1 .Fill.ForeColor.TintAndShade = 0 .Fill.ForeColor.Brightness = 0 .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).ParagraphFormat _ .FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(13, 10).Font .BaselineOffset = 0 .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).ParagraphFormat. _ FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(23, 1).Font .BaselineOffset = 0 .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.ObjectThemeColor = msoThemeColorDark1 .Fill.ForeColor.TintAndShade = 0 .Fill.ForeColor.Brightness = 0 .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).ParagraphFormat _ .FirstLineIndent = 0 With Selection.ShapeRange(1).TextFrame2.TextRange.Characters(24, 11).Font .BaselineOffset = 0 .Bold = msoTrue .NameComplexScript = "+mn-cs" .NameFarEast = "+mn-ea" .Fill.Visible = msoTrue .Fill.ForeColor.RGB = RGB(255, 0, 0) .Fill.Transparency = 0 .Fill.Solid .Size = 11 .Name = "+mn-lt" End With End Sub

【讨论】:

  • 当然你应该分享你的最终代码,但合适的地方是在你的原始问题中添加一个新部分
猜你喜欢
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-03
  • 2011-06-08
  • 1970-01-01
相关资源
最近更新 更多