利用第九章介绍的录制宏的方法录制一段宏,然后打开Visual Basic 编辑器,在工程管理器中选中当前文档的ThisDocument 模块,即可看到录制的宏。 Sub Macro2() ' ' Macro2 Macro ' 宏在 97-9-5 由 gszheng 录制 ' Documents.Open FileName:=" 你好.doc", ConfirmConversions:=False, ReadOnly:= _ False, AddToRecentFiles:=False, PasswordDocument:="", PasswordTemplate:= _ "", Revert:=False, WritePasswordDocument:="", WritePasswordTemplate:="", _ Format:=wdOpenFormatAuto '打开一个名为“你好.doc”的文档,打开文档时有许多参数,具体有:确认版本转换为假,只读为假,加入最近文件列表为假,读取密码文档和密码模板为空,恢复文档为假,写入密码文档和密码模板为空,格式为打开时自动辨认。
Windows("文档 4").Activate '激活“文档4”窗口 If Options.CheckGrammarWithSpelling = True Then '如果拼写和语法检查为真 ActiveDocument.CheckGrammar '对活动文档进行语法检查 Else
ActiveDocument.CheckSpelling '否则,对活动文档进行拼写检查 End If ActiveDocument.Hyperlinks.Add Anchor:=Selection.Range, Address:= _ "http://www.tsinghua.edu.cn/index.htm", SubAddress:="", ScreenTip:= _ "一流大学的网页", TextToDisplay:="清华大学网页" ' 在当前文档中插入一个超级连接, 地址为: "http://www.tsinghua.edu.cn/index.htm,本文档的位置为空,屏幕提示为“一流大学的网页”,文本允疚扒寤笱场薄4? 部分宏中引用了Range 对象,该对象代表文档中的一个范围。每一个 Range 对象由一起始和一终止字符位置定义。和文档中书签的使用方法类似,Visual Basic 使用 Range 对象识别文档的指定部分。然而,其区别在于 Range 对象只在已定义对象的过程正在运行时,才存在。 'Range 对象和选定内容相互独立。也就是说,可定义和复制一个范围而不需改变选定内容。还可在文档中定义多个范围,但每一窗格中只能有一个选定内容。
Selection.TypeParagraph '插入一个空段落 ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, NumColumns:=3, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= wdAutoFitFixed
'插入一个表格,行为2,列为3,默认的表格格式为Word 9(即是Word 2000 格式),为自动适应格式,即如果超过一行,会自动换行。
Selection.MoveDown Unit:=wdLine, Count:=2 '光标向下 移动两行 ActiveDocument.Shapes.AddShape ( msoShapeRectangle, 270#, 72#, 63#, _ 46.8).Select '在文档中绘一个矩形,270#, 72#, 63#,46.8 代表在文档中的位 置。 Selection.ShapeRange.IncrementTop 7.8 '把矩形往下 移动7.8 个单位
Selection.ShapeRange.IncrementLeft -9# '把矩形往左 移动9 个单位 With Selection.Sections(1).Headers(1).PageNumbers .NumberStyle = wdPageNumberStyleArabic .HeadingLevelForChapter = 0 .IncludeChapterNumber = False .ChapterPageSeparator = wdSeparatorHyphen .RestartNumberingAtSection = True .StartingNumber = 1 End With '以代码表示在文档中是插入空页眉 Selection.Sections(1).Footers(1).PageNumbers.Add PageNumberAlignment:= _ wdAlignPageNumberRight, FirstPage:=True '在页脚插入页码,开始页码为1 End Sub Sub Macro3() ' ' Macro3 Macro ' 宏在 99-11-30 由 gszheng 录制 ' ActiveDocument.SaveAs FileName:="good.doc", FileFormat:=wdFormatDocument, _ LockComments:=False, Password:="", AddToRecentFiles:=True, WritePassword _:="", ReadOnlyRecommended:=False, EmbedTrueTypeFonts:=False, _ SaveNativePictureFormat:=False, SaveFormsData:=False, SaveAsAOCELetter:= _ False, '对当前文档以"good.doc"文件名另存,其中包含了一些保存的 选项参数。 Selection.TypeText Text:="地方三分" '选定文字 With Selection.Font .NameFarEast = "宋体" .NameAscii = "Times New Roman" .NameOther = "Times New Roman"
.Name = "宋体" .Size = 10.5 .Bold = True .Italic = False .Underline = wdUnderlineNone .UnderlineColor = wdColorAutomatic .StrikeThrough = False .DoubleStrikeThrough = False .Outline = False .Emboss = False .Shadow = False .Hidden = False .SmallCaps = False .AllCaps = False .Color = wdColorAutomatic .Engrave = False .Superscript = False .Subscript = False
.Spacing = 0 .Scaling = 100 .Position = 0 .Kerning = 1 .Animation = wdAnimationNone .DisableCharacterSpaceGrid = False .EmphasisMark = wdEmphasisMarkNone End With '为选定文字设置字体 Selection.Orientation = wdTextOrientationVerticalFarEast '设置文字方向为垂直方向 Selection.Orientation = wdTextOrientationHorizontal '设置文字方向为水平方向 ActiveWindow.View.Type = wdWebView '设置视图格式为Web 视图格式 End Sub
|