用户登录
用户注册

分享至

vba在word中添加文字颜色

  • 作者: 刘村二丫
  • 来源: 51数据库
  • 2020-04-21

1.vba在word文档中如何插入文字并设置相应的文字属性

下面的VBA代码功能,第一步,word当前文档的上面添加文字dzwebs文字。然后,又设置第一段居中对齐,并在该段落之后添加半英寸的间距。将dzwebs的格式设为36磅,隶书字体。

Dim rngFormat As Range

Set rngFormat = ActiveDocument.Range(Start:=0, End:=0)

With rngFormat

.InsertAfter Text:="dzwebs"

.InsertParagraphAfter

With .Font

.Name = "隶书"

.Size = 36

.Bold = True

End With

End With

With ActiveDocument.Paragraphs(1)

.Alignment = wdAlignParagraphCenter

.SpaceAfter = InchesToPoints(0.5)

End With

部分代码解释:

Dim rngFormat As Range 定义变量

Set rngFormat = ActiveDocument.Range(Start:=0, End:=0) 设置rngFormat变量的位置,位于文档的最上面

With rngFormat

里面的代码功能是设置字体字号等属性

End With

With ActiveDocument.Paragraphs(1)

.Alignment = wdAlignParagraphCenter

.SpaceAfter = InchesToPoints(0.5)

End With

该代码代码功能是添加段后距离为0.5英寸

2.VBA输入带文字部分加颜色

Range("D4").Select

ActiveCell.FormulaR1C1 = "1,3,a"

With ActiveCell.Characters(Start:=1, Length:=3).Font

.Name = "宋体"

.FontStyle = "常规"

.Size = 12

.Strikethrough = False

.Superscript = False

.Subscript = False

.OutlineFont = False

.Shadow = False

.Underline = xlUnderlineStyleNone

.ColorIndex = 3

End With

With ActiveCell.Characters(Start:=4, Length:=2).Font

.Name = "宋体"

.FontStyle = "常规"

.Size = 12

.Strikethrough = False

.Superscript = False

.Subscript = False

.OutlineFont = False

.Shadow = False

.Underline = xlUnderlineStyleNone

.ColorIndex = xlAutomatic

End With

Range("D5").Select

3.如何用word vba实现页面设置里的字体设置

With Selection.Font

.NameFarEast = "宋体"

.NameAscii = "Times New Roman"

.NameOther = "Times New Roman"

.Name = "Times New Roman"

.Size = 12

.Bold = False

.Italic = False

.Underline = wdUnderlineNone

.UnderlineColor = wdColorAutomatic

.StrikeThrough = False

.DoubleStrikeThrough = False

.Outline = False

.Emboss = False

.Shadow = False

.Hidden = False

.SmallCaps = False

.AllCaps = False

.Color = wdColorAutomatic

.Engrave = False

.Superscript = False

.Subscript = False

.Spacing = 0

.Scaling = 100

.Position = 0

.Kerning = 1

.Animation = wdAnimationNone

.DisableCharacterSpaceGrid = False

.EmphasisMark = wdEmphasisMarkNone

End With

补充:

wdStyleNormal=-1,试试With ActiveDocument.Styles(-1).Font

4.VBA输入带文字部分加颜色

With Range("A1")

.Value = "1万个"

.Characters(Start:=1, Length:=1).Font.Color = RGB(255, 0, 0)

.Characters(Start:=2, Length:=0).Font.Color = 0

End With

start是开始位置..从1开始..length是长度..0表示从开始位置以后的所有字符.

RGB 三个参数是红绿蓝..0-255

5.在word中VBA去设置表格中的字体,万分感谢

Sub test()

For i = 1 To ActiveDocument.Tables.Count

With ActiveDocument

.Range(.Tables(i).Cell(3, 1).Range.Start, .Tables(i).Cell(5, 3).Range.End).Select

End With

Selection.Font.Size = 8

Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter

Next

End Sub

转载请注明出处51数据库 » vba在word中添加文字颜色

软件
前端设计
程序设计
Java相关