如何用VBA设置WORD文档的题目(即第一段)的格式

具体要求:第一段即文档题目,去除段前空格,字体为“黑体”,字号为“小二”,加粗,居中对齐,请用VBA代码,千万不要用菜单命令操作。正文格式不变。

Sub 宏1()
    Dim str As String, i As Integer, j As Integer
    j = 0
    str = Application.ActiveDocument.Paragraphs(1).Range.Text
    For i = 1 To Len(str)
        If Mid(str, i, 1) = " " Then
        Else
            Exit For
        End If
        j = j + 1
    Next
    str = Right(str, Len(str) - j)
    Application.ActiveDocument.Paragraphs(1).Range.Text = str
    Application.ActiveDocument.Paragraphs(1).Range.Select
    Selection.Font.Name = "黑体"
    Selection.Font.Size = 18
    Selection.Font.Bold = wdToggle
    Selection.ParagraphFormat.Alignment = wdAlignParagraphCenter
End Sub

追问

高手!
能不能在您提供的代码后再加上正文设置代码?
正文(即第二段以后)格式:每段首行缩进2字符,字体为“仿宋-GB2312",字号为“小三”,每段首行是:一 二三四五六七八九十或(一)(二)(三)(四)(五)(六)(七)(八)(九)(十)或1 2 3 4 5 6 7 8 9 10等字符要加粗

万分期待

追答

Sub test()
With Application.ActiveDocument
Dim i, pcount As Integer, astr As String
pcount = .Paragraphs.Count
For i = 2 To pcount
astr = Left(.Paragraphs(i).Range.Text, 1)
Select Case astr
Case "1" To "9", "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"
.Paragraphs(i).Range.Select
Selection.MoveLeft (1)
Selection.MoveRight Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Selection.Font.Bold = True
Case "("
If Mid(.Paragraphs(i).Range.Text, 3, 1) = ")" Then
Select Case Mid(ActiveDocument.Paragraphs(i).Range.Text, 2, 1)
Case "一", "二", "三", "四", "五", "六", "七", "八", "九", "十"
.Paragraphs(i).Range.Select
Selection.MoveLeft (1)
Selection.MoveRight Unit:=wdCharacter, Count:=3, Extend:=wdExtend
Selection.Font.Bold = True
End Select
End If
End Select
Next
.Paragraphs(2).Range.Select
End With
With Selection
.MoveLeft (1)
.EndKey Unit:=wdStory, Extend:=wdExtend
.Font.Size = 15
.ParagraphFormat.CharacterUnitFirstLineIndent = 2
.Font.Name = "仿宋-GB2312"
End With
End Sub

追问

您编写的题目格式代码与正文格式代码分开是可以用的,我需要把两段代码合在一起,敬请赐教,十分感谢

追答

去 VBE 里面导入即可。

温馨提示:答案为网友推荐,仅供参考
相似回答