应老大的指点改正并贴代码如下: Option Explicit Private Sub CommandButton1_Click() Dim asection As Section Dim aleft As Long, atop As Long Dim awidth As Long, ahight As Long Dim i As Long, ashape As Shape Dim acolor As String Application.ScreenUpdating = False On Error GoTo handler: '取颜色 acolor = ComboBox1.Text Select Case acolor Case "兰色" acolor = wdColorBlue Case "灰色" acolor = RGB(11, 11, 11) Case "黑色" acolor = wdColorBlack Case "红色" acolor = wdColorRed Case "绿色" acolor = wdColorGreen Case Else MsgBox "重新填写直线颜色" ComboBox1.Text = "" ComboBox1.SetFocus End Select '进入页眉 For Each asection In ActiveDocument.Sections '在每节中循环 With asection.Headers(wdHeaderFooterPrimary) '在页眉页脚中循环 aleft = asection.PageSetup.LeftMargin atop = asection.PageSetup.TopMargin awidth = asection.PageSetup.PageWidth ahight = asection.PageSetup.PageHeight i = 1 Dim a As Shape Do While atop + (TextBox1.Text) * i < ahight - atop Set a = .Shapes.AddLine(aleft, atop + (TextBox1.Text) * i, awidth - aleft, atop + (TextBox1.Text) * i) a.ZOrder msoSendToBack '放到最后 i = i + 1 Loop For Each ashape In .Shapes If ashape.Type = msoLine Then ashape.Line.ForeColor.RGB = acolor End If Next End With Next Application.ScreenRefresh Application.ScreenUpdating = True 复位窗体 Exit Sub handler: MsgBox "错误" Unload Me Application.ScreenUpdating = True End Sub Private Sub CommandButton2_Click() Unload Me End Sub Private Sub CommandButton3_Click() Dim asection As Section Dim ashape As Shape Application.ScreenUpdating = False On Error GoTo handler: For Each asection In ActiveDocument.Sections '在每节中循环 With asection.Headers(wdHeaderFooterPrimary) '在页眉页脚中循环 For Each ashape In .Shapes If ashape.Type = msoLine Then ashape.Delete End If Next End With Next Application.ScreenRefresh Application.ScreenUpdating = True 复位窗体 Exit Sub handler: MsgBox "错误" Unload Me Application.ScreenUpdating = True End Sub Private Sub TextBox2_Change() End Sub Private Sub UserForm_Initialize() Dim a Dim atop Dim aheigh Dim abottom '复合框中设置 复位窗体 With ActiveDocument.PageSetup a = .LinesPage atop = .TopMargin abottom = .BottomMargin aheigh = .PageHeight a = Format((aheigh - atop - abottom) / a, "00.00") End With '默认值的设置 TextBox1.Text = a End Sub Sub 复位窗体() ComboBox1.Clear ComboBox1.AddItem "黑色" ComboBox1.AddItem "红色" ComboBox1.AddItem "兰色" ComboBox1.AddItem "绿色" ComboBox1.AddItem "灰色" ComboBox1.Text = "黑色" End Sub
|