Word Objectsand Macro Examples
2 Document 对象Document Object
文档对象 - Word 文档 Document Object – A Word document
1)活动文档 ActiveDocument:
ActiveDocument.PrintOut
2)激活文档 ActiveDocument
Documents("Example.docx").Activate
3)当前文档 ThisDocument
ThisDocument.PrintOut
4)变量文档 Document Variables
Sub mynzVarExample()
Dim oDoc As Document
Set oDoc = ActiveDocument
oDoc.PrintOut
End Sub
5)打开文档 Open Document
Documents.Open "c:\Users\SomeOne\Desktop\Test PM.docx"
6)打开文档到变量 Open Document to a variable
Dim oDoc as Document
Set oDoc = Documents.Open("c:\Users\SomeOne\Desktop\Test PM.docx")
7)创建文档 Create New Document
Documents.Add
8)基于某个模板创建一个新文档 create a new doc based on some template:
Documents.Add Template:="C:\Program Files\Microsoft Office\Templates\MyTemplate.dotx"
9)基于某个模板创建一个新文档到变量 Create a new document based on a template into a variable
Dim oDoc as Document
Set oDoc = Documents.Add (Template:="C:\Program Files\Microsoft Office\Templates\MyTemplate.dotx")
10) 保存文档 Save Document
ActiveDocument.Save
11) 另存为文档 SaveAs
ActiveDocument.SaveAs FileName:= c:\Users\SomeOne\Desktop\test2.docx", FileFormat:=wdFormatDocument
12)关闭文档 Close Document
ActiveDocument.Close wdSaveChanges
13) 不保存关闭文档 without saving changes
ActiveDocument.Close wdDoNotSaveChanges
14)打印文档 Print Document
ActiveDocument.PrintOut
3 Range对象 Range Object
范围对象 - Word 文档的一部分 Range Object – A part of a Word document
范围可以是文档的任何部分,包括整个文档 Range can be any part of document, including entire document:
Dim oRange As Range
Set oRange = ActiveDocument.Content
一个字符
one character
Dim oRange As Range
Set oRange = ActiveDocument.Range.Words(1)
3)将第二段的第一个单词加粗: In the following example we will make the first word of second paragraph bold:
Dim oRange As Range
Set oRange = ActiveDocument.Paragraphs(2).Range.Words(1)
oRange.Bold = True
4)设置 Range 的文本值: To set the text value of a Range:
Dim oRange As Range
Set oRange = ActiveDocument.Paragraphs(2).Range.Words(1)
oRange.Text = “Hello ”
提示:注意“Hello”后面的空格。因为单词对象包含一个单词后的空格,只有“hello”我们会得到“Hellonext word” Tip: Note the space after “Hello”. Because word object includes space after word, with just “hello” we would get “Hellonext word”
5)更改字体 Change font
oRange.Font.Name = "Arial"
6)在消息框中显示特定范围内的字符数 Display in message box number of characters in particular range
MsgBox oRange.Characters.Count
7)在它前面插入一些文本 Insert some text before it
oRange.InsertBefore "this is inserted text "
8)为范围添加注脚 Add a footnote to range
ActiveDocument.Footnotes.Add Range:=oRange, _
Text:="作者:ningzhe"
9) 将其复制到剪贴板 Copy it to clipboard
oRange.Copy
‘通常,您需要更改特定范围所指的内容。 所以你可以修正它的开始和结束
oRange.Start = 5
oRange.End = 50 【分享成果,随喜正能量】