【发布时间】: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下方,它可以正常工作。再次感谢!