欢迎来到科站长!

office激活

当前位置: 主页 > 网站运营 > office激活

vba设置word文档的格式

时间:2025-05-10 15:48:59|栏目:office激活|点击:

①将格式应用于选定内容

下列示例使用Selection属性将字符和段落格式应用于选定文本。使用Font属性获得字体格式的属性和方法,使用ParagraphFormat属性获得段落格式的属性和方法。

Sub FormatSelection()

With Selection.Font

.Name = "Times New Roman"

.Size = 14

.AllCaps = True

End With

With Selection.ParagraphFormat

.LeftIndent = InchesToPoints(0.5)

.Space1

End With

End Sub

②将格式应用于某一区域

下列示例定义了一个Range对象,它引用了活动文档的前三个段落。通过应用Font 和ParagraphFormat对象的属性来设置 Range对象的格式。

Sub FormatRange()

Dim rngFormat As Range

Set rngFormat = ActiveDocument.Range( _

Start:=ActiveDocument.Paragraphs(1).Range.Start, _

End:=ActiveDocument.Paragraphs(3).Range.End)

With rngFormat

.Font.Name = "Arial"

.ParagraphFormat.Alignment = wdAlignParagraphJustify

End With

End Sub

③插入文字并应用字符和段落格式

下列示例在当前文档的上部添加单词 Title。第一段居中对齐,并在该段落之后添加半英寸的间距。将单词 Title 的格式设为 24 磅 Arial 字体。

Sub InsertFormatText()

Dim rngFormat As Range

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

With rngFormat

.InsertAfter Text:="Title"

.InsertParagraphAfter

With .Font

.Name = "Tahoma"

.Size = 24

.Bold = True

End With

End With

With ActiveDocument.Paragraphs(1)

.Alignment = wdAlignParagraphCenter

.SpaceAfter = InchesToPoints(0.5)

End With

End Sub

④在 12 磅和无之间切换段前间距

下列示例切换选定内容中第一段的段前间距。宏将获取当前段前间距的值,如果该值为 12 磅,则删除段前间距格式(将SpaceBefore属性设为零)。如果段前间距的值为除 12 外的其它数值,则将 SpaceBefore属性设为 12 磅。

Sub ToggleParagraphSpace()

With Selection.Paragraphs(1)

If .SpaceBefore <> 0 Then

.SpaceBefore = 0

Else

.SpaceBefore = 6

End If

End With

End Sub

⑤切换加粗格式

下列示例切换选定文本的加粗格式。

Sub ToggleBold()

Selection.Font.Bold = wdToggle

End Sub

⑥将左边距增加 0.5 英寸

下列示例将左边距和右边距增加 0.5 英寸。PageSetup对象包含文档的所有的页面设置属性(左边距、下边距、纸张大小等)。LeftMargin属性用于返回和设置左边距设置。RightMargin属性用于返回和设置右边距设置。

Sub FormatMargins()

With ActiveDocument.PageSetup

.LeftMargin = .LeftMargin + InchesToPoints(0.5)

.RightMargin = .RightMargin + InchesToPoints(0.5)

End With

End Sub


上一篇:VBA操作Word中Documents/Document对象属性介绍

栏    目:office激活

下一篇:如何让WORD的宏随文档的打开自动运行

本文标题:vba设置word文档的格式

本文地址:https://www.fushidao.cc/wangzhanyunying/13950.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 科站长 版权所有冀ICP备14023439号