一些常用的WORD-VBA代码(共3页).doc
精选优质文档-倾情为你奉上这里给大家提供一些比较常用的WORD VBA代码,可以提高大家的办公效率,如果不知道怎么使用这些代码,请自行上网查询WORD如何运行VBA。1、删除空行Sub 删除空行()Dim I As Paragraph, n As IntegerApplication.ScreenUpdating = FalseFor Each I In ActiveDocument.ParagraphsIf Len(Trim(I.Range) = 1 ThenI.Range.Deleten = n + 1End IfNextMsgBox "共删除空白段落" & n & "个"Application.ScreenUpdating = TrueEnd Sub2、奇偶页打印Sub 奇偶页打印()Dim x, j, i As IntegerOn Error Resume Nextx = ExecuteExcel4Macro("Get.Document(50)")For i = 1 To Int(x / 2) + 1ActiveWindow.SelectedSheets.PrintOut From:=2 * i - 1, To:=2 * i - 1Next iIf x = 1 ThenMsgBox "无偶数页"ElseMsgBox "请将打印出的纸张反向装入纸槽中", vbOKOnly, "打印另一面"For j = 1 To Int(x / 2) + 1ActiveWindow.SelectedSheets.PrintOut From:=2 * j, To:=2 * jNext jEnd IfEnd Sub3、中英文标点互换Sub 中英文标点互换()Dim ChineseInterpunction() As Variant, EnglishInterpunction() As VariantDim myArray1() As Variant, myArray2() As Variant, strFind As String, strRep As StringDim msgResult As VbMsgBoxResult, N As Byte'定义一个中文标点的数组对象ChineseInterpunction = Array("、","。", ",", ";", ":", "?", "!", "", "", "", "(", ")", "", "")'定义一个英文标点的数组对象EnglishInterpunction = Array(",",".", ",", "", ":", "?", "!", "", "-", "", "(", ")", "<", ">")'注意这里的英文,转换为了中文、,如果希望将,转换为中文,请自行修改!'提示用户交互的MSGBOX对话框msgResult = MsgBox("您想中英标点互换吗?按Y将中文标点转为英文标点,按N将英文标点转为中文标点!", vbYesNoCancel)Select Case msgResultCase vbCancelExit Sub '如果用户选择了取消按钮,则退出程序运行Case vbYes '如果用户选择了YES,则将中文标点转换为英文标点myArray1 = ChineseInterpunctionmyArray2 = EnglishInterpunctionstrFind = "“(*)”"strRep = """1"""Case vbNo '如果用户选择了NO,则将英文标点转换为中文标点myArray1 = EnglishInterpunctionmyArray2 = ChineseInterpunctionstrFind = """(*)"""strRep = "“1”"End SelectApplication.ScreenUpdating = False '关闭屏幕更新For N = 0 To UBound(ChineseInterpunction) '从数组的下标到上标间作一个循环With ActiveDocument.Content.Find.ClearFormatting '不限定查找格式.MatchWildcards = False '不使用通配符'查找相应的英文标点,替换为对应的中文标点.Execute findtext:=myArray1(N), replacewith:=myArray2(N), Replace:=wdReplaceAllEnd WithNextWith ActiveDocument.Content.Find.ClearFormatting '不限定查找格式.MatchWildcards = True '使用通配符.Execute findtext:=strFind, replacewith:=strRep, Replace:=wdReplaceAllEnd WithApplication.ScreenUpdating = True '恢复屏幕更新End Sub4、任意页插入页码Sub任意页插入页码()Dim p As IntegerOn Error Resume Nextp = InputBox("请输入起始编排页码的页次")With Selection.GoTo What:=wdGoToPage, Count:=p.InsertBreak Type:=wdSectionBreakContinuous.Sections(1).Footers(1).LinkToPrevious = FalseWith .Sections(1).Footers(1).PageNumbers.RestartNumberingAtSection = True.StartingNumber = 1.Add PageNumberAlignment:=wdAlignPageNumberCenter, FirstPage:=TrueEnd WithEnd WithEnd Sub5、实现图形的精确旋转Sub 图形旋转()Dim blnIsInlineShape As BooleanIf Selection.Type = wdSelectionInlineShape ThenblnIsInlineShape = TrueSelection.InlineShapes(1).ConvertToShapeEnd IfDim intTurn As IntegerintTurn = InputBox("请输入图形要旋转的角度值" & vbCrLf & "正数表示顺时针,负数表示逆时针。", "图形旋转", 30)Selection.ShapeRange.IncrementRotation intTurnEnd Sub 注释:上述代码中,首先是将嵌入式的图形转换为可以自由浮动的图形。返回Wo rd窗口之后,选中文档中需要旋转的某幅图形,按下Alt+F8组合键,选中列表框中的“图形旋转”宏,单击“运行”按钮弹出一个对话框,默认的旋转角度是30°,例如设置为“33”,很快就可以完成旋转操作。专心-专注-专业