启嘟渡科技商贸有限公司
SEARCH

与我们合作

我们专注提供互联网一站式服务,助力企业品牌宣传多平台多途径导流量。
主营业务:网站建设、移动端微信小程序开发、营销推广、基础网络、品牌形象策划等

您也可通过下列途径与我们取得联系:

微 信: wxyunyingzhe

手 机: 15624122141

邮 箱:

word代码改变文本格式

更新时间:2024-12-23 04:34:16

以前做过的代码找一段出来供参考。

Private Sub CommandButton4_Click() '排版格式

Dim T_WORD As String

Dim RNG As Range

Dim mypra As Paragraph

Set mydic = CreateObject("Scripting.Dictionary") '二级标题字符

mydic.Add "一", 1

mydic.Add "二", 1

mydic.Add "三", 1

mydic.Add "四", 1

mydic.Add "五", 1

mydic.Add "六", 1

mydic.Add "七", 1

mydic.Add "八", 1

mydic.Add "九", 1

mydic.Add "十", 1

T_WORD = TextBox1.Text

导出路径文件名 = ThisDocument.Path & "\" & T_WORD

Set mydoc = Documents.Open(导出路径文件名)

mydoc.Activate

Selection.WholeStory '选中全部

Selection.ClearFormatting '清除全部格式

zz = 0

'For Each mypra In ActiveDocument.Paragraphs

For Each mypra In mydoc.Paragraphs

If Not mypra.Range.Information(wdWithInTable) = True Then '非表格段落

zz = zz + 1

MYSTR = mypra.Range.Text

mychar = Mid(MYSTR, 1, 1)

'mypra.Select

If zz = 1 Or zz = 2 Then '一级标题 黑体 小二 是 居中 1行 1行 固定值18磅 无

With mypra

.Range.Font.Name = "黑体"

.Range.Font.Size = 18 '(小二高度18磅)

.Range.Font.Bold = True

.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

.Range.ParagraphFormat.LineUnitBefore = 1

.Range.ParagraphFormat.LineUnitAfter = 1

'.Range.ParagraphFormat.SpaceBefore = 10.5 '段前 1123修改为5号10.5磅

'.Range.ParagraphFormat.SpaceAfter = 10.5 '段后

.FirstLineIndent = 0 '首行缩进无

.LineSpacingRule = wdLineSpaceExactly

'.LineSpacingRule = wdLineSpace1pt5

.Range.ParagraphFormat.LineSpacing = 18 '行间距18磅

End With

Else

If mydic.Exists(mychar) Then '二级标题 黑体 小四 是 两端对齐 1行 1行 固定值18磅 首行缩进2字符

With mypra

.Range.Font.Name = "黑体"

.Range.Font.Size = 12

.Range.Font.Bold = True

.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify

.Range.ParagraphFormat.LineUnitBefore = 1

.Range.ParagraphFormat.LineUnitAfter = 1

'.Range.ParagraphFormat.SpaceBefore = 10.5 '段前 小四高度=12磅 1123修改为5号10.5磅

'.Range.ParagraphFormat.SpaceAfter = 10.5 '段后

'.FirstLineIndent = CentimetersToPoints(1.27) '首行缩进2字符 0.635*2=1.27

.FirstLineIndent = CentimetersToPoints(0.74) '首行缩进2字符 0.74 和正文相同

' .LineSpacingRule = wdLineSpace1pt5

.LineSpacingRule = wdLineSpaceExactly

.Range.ParagraphFormat.LineSpacing = 18 '行间距18磅

End With

Else '正文 宋体 五号 否 两端对齐 0行 0.5行 固定值18磅 首行缩进2字符

With mypra

.Range.Font.Name = "宋体"

.Range.Font.Size = 10.5

.Range.Font.Bold = False

.Range.ParagraphFormat.Alignment = wdAlignParagraphJustify

'.Range.ParagraphFormat.SpaceBefore = 0 '段前0

'.Range.ParagraphFormat.SpaceAfter = 5.25 '段后0.5*10.5=5.25

.Range.ParagraphFormat.LineUnitBefore = 0

.Range.ParagraphFormat.LineUnitAfter = 0.5

.FirstLineIndent = CentimetersToPoints(0.74) '0.37*2=0.74

.LineSpacingRule = wdLineSpaceExactly

.Range.ParagraphFormat.LineSpacing = 18 '行间距18磅

.Alignment = wdAlignParagraphJustify

End With

If zz = 3 Then '第3段落居中

With mypra

.Range.ParagraphFormat.Alignment = wdAlignParagraphCenter

End With

End If

End If

End If

End If

Next mypra

mydoc.Save

mydoc.Close False '关闭word文档

Set mydoc = Nothing '清空工作簿项目

End Sub

多重随机标签

猜你喜欢文章

QQ客服 电话咨询