启明办公

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 72|回复: 1

WORD_VBA中Document对象与Range对象

[复制链接]

3

主题

7

帖子

12

积分

新手上路

Rank: 1

积分
12
发表于 2022-10-14 21:49:35 | 显示全部楼层 |阅读模式
分享成果,随喜正能量】历经岁月,走过世事三千,内心仍为自己留有一块净土,不畏人言,自然坦荡,活得自在,过得心安。。
我给VBA的定义:VBA是个人小型自动化处理的有效工具。利用好了,可以大大提高自己的劳动效率,而且可以提高数据的准确度。我推出的VBA系列教程共十套(本文的最后附有VBA教程目录和VBA工具目录),现在已经全部完成。
如果您VBA是入门阶段,可以打包选择7.1.3.9教程,第7套教程是入门,第1套教程是入门后的提高,第3套教程字典是必备的VBA之精华,第9套教程是实用的典型案例讲解。如果您有了一定的VBA基础可以根据自己的需要,进行教程的选择。教程提供的程序源码文件就如一座大型的代码库支持着大家的工作。同时还有实用的资料送给学员。
VBA是面向对象编程的语言,博大精深。很多朋友咨询英语和VBA的关系,这里我会分享些一些有用的VBA案例:《VBA即用型代码案例(汉英)》,案例中我会用汉语和英语同时发布,学员从中可以更好的领会和掌握VBA中用到的一些英语。今日的内容:WORD_VBA中Document对象与Range对象


第六章  Word对象及示例

Word Objects and 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
【分享成果,随喜正能量】


我20多年的VBA实践经验,全部浓缩在下面的各个教程中:






分享成果,随喜正能量】这世上有两个我,一个躯壳喂日常,一个灵魂补岁月.   一个在文字里白马春衫慢慢行,一个在生活里蝇营狗苟兀穷年。。
回复

使用道具 举报

0

主题

7

帖子

11

积分

新手上路

Rank: 1

积分
11
发表于 2025-4-30 07:07:59 | 显示全部楼层
为保住菊花,这个一定得回复!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|启明办公

Copyright © 2001-2013 Comsenz Inc.Template by Comsenz Inc.All Rights Reserved.

Powered by Discuz!X3.4

快速回复 返回顶部 返回列表