vba设置word文档的格式
①将格式应用于选定内容
下列示例使用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激活
本文标题:vba设置word文档的格式
本文地址:https://www.fushidao.cc/wangzhanyunying/13950.html
您可能感兴趣的文章
- 07-01word中使用黑白打印机打印彩色字体颜色太浅怎么办?
- 07-01word如何制作漂亮的封面
- 07-01Word数字序号如何设置自动调整
- 07-01如何设置不打开word也能预览文档内容
- 07-01在word中如何插入图片并随意放置
- 07-01在word中怎样使用格式刷
- 07-01如何删除word中的宏
- 07-01word中怎么编辑输入复杂公式
- 07-01怎样解决Word设置的行数与实际不同
- 07-01如何把word的英文和汉语字体分别设置


阅读排行
推荐教程
- 11-30正版office2021永久激活密钥
- 11-25怎么激活office2019最新版?office 2019激活秘钥+激活工具推荐
- 11-22office怎么免费永久激活 office产品密钥永久激活码
- 11-25office2010 产品密钥 永久密钥最新分享
- 11-22Office2016激活密钥专业增强版(神Key)Office2016永久激活密钥[202
- 11-30全新 Office 2013 激活密钥 Office 2013 激活工具推荐
- 11-25正版 office 产品密钥 office 密钥 office2019 永久激活
- 11-22office2020破解版(附永久密钥) 免费完整版
- 11-302023 全新 office 2010 标准版/专业版密钥
- 03-262024最新最新office365激活密钥激活码永久有效