用户登录
用户注册

分享至

vbawordparagraph ExcelVBA操作word文件

  • 作者: 老实人妙勇
  • 来源: 51数据库
  • 2020-04-20

在 Word 中,几乎所有的操作都要调用 Document 对象本身或其内容。当您用 VBA 操作 Word 时,Document 对象表示一个打开的文档,而且所有的 Document 对象都是 Application 对象的 Documents 集合的成员。

文档是一个由字符、单词、句子和段落组成的集合,字符组成单词,单词组成句子,句子组成段落,等等。因此,每一个 Document 对象都具有 Characters、Words、Sentences 和 Paragraghs 四个集合。此外,每个文档具有一个包含一个或多个节的 Sections 集合,每一个节都有一个包含该节页眉和页脚的 HeadersFooters 集合。

注意: 您可以在 Microsoft Office 2000 开发人员对象模型指南(英文)中查阅完整的 Word 对象模型。另外,您也可以使用对象浏览器和 Microsoft Word Visual Basic 参考帮助来学习有关具体某个对象、属性、方法和事件的详细内容。

通过 VBA 使用 Word 时,Document 对象处于中心位置。如果您要打开文档或创建新文档,就要创建新的 Document 对象。每个打开或新创建的文档均被添加至 Documents 集合。具有焦点的文档称为活动文档,由 ActiveDocument 属性表示。

Document 对象作为 Documents 集合中的一个成员,您可以通过使用 Document 对象的索引值(Document 对象在 Documents 集合中的位置,1 是集合中的第一个文档)或名称来引用它。另外,您也可以使用 ActiveDocument 属性来引用当前具有焦点的文档。例如,如果名为 Policies.doc 的文档是唯一打开的文档,则以下三个对象变量将全部指向 Policies.doc:

Dim docOne As Word.Document

Dim docTwo As Word.Document

Dim docThree As Word.Document

Set docOne = Documents(1)

Set docTwo = Documents("Policies.doc")

Set docThree = ActiveDocument

一般情况下不要使用 Documents 集合中的索引值来引用文档,因为当其它文档打开或关闭时,某个特定文档的索引值可能会随之改变。通常,您可以通过使用 ActiveDocument 属性或使用 Documents 集合的 Add 方法或 Open 方法创建的 Document 对象变量。以下示例显示了如何使用 ActiveDocument 属性把一个地址添加到当前具有焦点的文档中:

With ActiveDocument

.Envelope.Insert Address:="Office Talk" _

& vbCrLf & "One Microsoft Way" & vbCrLf _

& "Redmond, WA 98052", ReturnAddress:= _

"David Shank" & vbCrLf & _

"77 First Street" & vbCrLf & _

"Any Town, USA 12345"

End With

下面的示例说明如何通过使用 Documents 集合的 Open 方法,实例化 Document 对象变量。

Dim docPolicy As Word.Document

Set docPolicy = Documents.Open("c:\my documents\policies.doc")

最后一个示例显示如何通过使用 Add 方法,为新的空文档创建 Document 对象的实例。

Dim docPolicy As Word.Document

Set docPolicy = Documents.Add

通过使用 Open 方法打开的文档,或者通过使用 Add 方法创建的文档,都将成为用 ActiveDocument 属性表示的当前活动文档。如果您想使 Documents 集合里的其它文档成为活动文档,可使用 Document 对象的 Active 方法。

一旦您获取了要操作的 Document 对象,绝大部分您想通过 VBA 进行的工作将涉及文本的操作。首先要指定文档的一个部分,然后对它进行某些操作。例如,添加或删除文本,或者设置单词或字符的格式。您可以使用 Range 或 Selection 这两个对象来完成很多工作。

Word vba常用语句100句

1、系统参数

(01) Application.ActivePrinter ‘获取当前打印机

(02) Application.Height '当前应用程序文档的高度

(03) Application.Width ‘当前应用程序文档的宽度

(04) Application.Build ‘获取Word版本号和编译序号

(05) Application.Caption ‘当前应用程序名

(06) Application.DefaultSaveFormat '返回空字符串,表示Word文档

(07) Application.DisplayRecentFiles '返回是否显示最近使用的文档的状态

(08) Application.Documents.Count '返回当前打开的文档数

(09) Application.FontNames.Count ‘返回当前可用的字体数

(10) Application.Left ‘返回当前文档的水平位置

(11) Application.MacroContainer.FullName '返回当前文档名,包括所在路径

Application.MacroContainer.pach '返回当前文档路径

Application.ActiveDocument.Path ‘获得文件的相对路径

(12) Application.NormalTemplate.FullName '返回文档标准模板名称及所在位置

(13) Application.RecentFiles.Count '返回最近打开的文档数目

(14) Application.System.CountryRegion '返回应用程序所在的地区代码

(15) Application.System.FreeDiskSpace ‘返回应用程序所在磁盘可用空间

(16) Application.System.HorizontalResolution '返回显示器的水平分辨率

(17) Application.System.VerticalResolution '返回显示器的垂直分辨率

(18) Application.System.LanguageDesignation '返回系统所使用的语言

(19) Application.System.MathCoprocessorInstalled ‘返回系统是否安装了数学协处理器

(20) Application.System.OperatingSystem ‘返回当前操作系统名

(21) Application.System.ProcessorType '返回计算机处理器名

(22) Application.System.Version ‘返回操作系统的版本号

(23) Application.Templates.Count '返回应用程序所使用的模板数

(24) Application.UserName '返回应用程序用户名

(25) Application.Version ‘返回应用程序的版本号

2、Documents/Document对象

(26) ActiveDocument.AttachedTemplate.FullName '返回当前文档采用的模板名及模板所在位置

(27) ActiveDocument.Bookmarks.Count '返回当前文档中的书签数

(28) ActiveDocument.Characters.Count '返回当前文档的字符数

(29) ActiveDocument.CodeName ‘返回当前文档的代码名称

(30) ActiveDocument.Comments.Count ‘ 返回当前文档中的评论数

(31) ActiveDocument.Endnotes.Count '返回当前文档中的尾注数

(32) ActiveDocument.Fields.Count '返回当前文档中的域数目

(33) ActiveDocument.Footnotes.Count ‘返回当前文档中的脚注数

(34) ActiveDocument.FullName '返回当前文档的全名及所在位置

(35) ActiveDocument.HasPassword '当前文档是否有密码保护

(36) ActiveDocument.Hyperlinks.Count '返回当前文档中的链接数

(37) ActiveDocument.Indexes.Count '返回当前文档中的索引数

(38) ActiveDocument.ListParagraphs.Count '返回当前文档中项目编号或项目符号数

(39) ActiveDocument.ListTemplates.Count '返回当前文档中使用的列表模板数

(40) ActiveDocument.Paragraphs.Count '返回当前文档中的段落数

(41) ActiveDocument.Password=XXX '设置打开文件使用的密码

(42) ActiveDocument.ReadOnly '获取当前文档是否为只读属性

(43) ActiveDocument.Saved '当前文档是否被保存

(44) ActiveDocument.Sections.Count '当前文档中的节数

(45) ActiveDocument.Sentences.Count ‘当前文档中的语句数

(46) ActiveDocument.Shapes.Count '当前文档中的形状数 ,图形?

(47) ActiveDocument.Styles.Count '当前文档中的样式数

(48) ActiveDocument.Tables.Count ‘当前文档中的表格数

(49) ActiveDocument.TablesOfAuthorities.Count ‘返回当前文档中的引文目录数

(50) ActiveDocument.TablesOfAuthoritiesCategories.Count ‘返回当前文档中引文目录类别数

(51) ActiveDocument.TablesOfContents.Count ‘返回当前文档中的目录数

(52) ActiveDocument.TablesOfFigures.Count '返回当前文档中的图表目录数

3、Paragraphs/Paragraph对象

(53) Selection.Paragraphs.Count '返回所选区域的段落数

(54) Selection.Paragraphs.First '返回所选区域中的第一段

(55) ActiveDocument.Paragraphs(1).LeftIndent '返回当前文档中第一段的左缩进值

(56) ActiveDocument.Paragraphs(1).LineSpacing '返回当前文档中第一段的行距

(57) ActiveDocument.Paragraphs(1).OutlineLevel ‘返回或设置当前文档中第一段的大纲级别

.OutlineLevel = wdOutlineLevel2 ‘2级

.OutlineLevel = wdOutlineLevel3 ‘3级

(58) ActiveDocument.Paragraphs(1).RightIndent ‘返回当前文档中第一段的右缩进量

(59) ActiveDocument.Paragraphs(1).SpaceBefore '返回当前文档中第一段的段前间距

(60) ActiveDocument.Paragraphs(1).SpaceAfter ‘返回当前文档中第一段的段后间距

(61) ActiveDocument.Paragraphs(1).Range.Text '返回当前文档中第一段的内容

(62) ActiveDocument.Paragraphs(1).Range.Style.NameLocal '返回当前文档中第一段应用的样式名

(63) ActiveDocument.Paragraphs(1).Range.Style.Description '返回当前文档中第一段所应用样式的详细描述

(64) ActiveDocument.Paragraphs(1).Range.Style.Font.Name '返回当前文档中第一段所应用样式的字体名

(65) ActiveDocument.Paragraphs(1).Range.Style.Font.NameFarEast '返回或设置一种东亚字体名

(66) ActiveDocument.Paragraphs(1).Range.Style.Font.Size '返回或设置当前文档中第一段所应用样式的字体大小

(67) ActiveDocument.Paragraphs(1).Range.Style.Font.Spacing '返回或设置字符间距

(68) Selection.Words.Count '所选区域的字数 Sentences对象

(69) Selection.Sentences.Item(1) '所选区域中的第一句的内容 Words对象

(71) ActiveDocument.Words(1).Select '选择当前文档中的第一个词

(72) ActiveDocument.Range.Words(1).InsertAfter "我爱你!" '在当前文档中的第一个词后插入“我爱你”

4、Characters对象

(73) Selection.Characters.Count '当前文档中所选区域的字符数

(74) ActiveDocument.Paragraphs(1).Range.InsertParagraphAfter'在当前文档的第一段之后插入一个新段落

5、Sections/Section对象

(75) ActiveDocument.Sections.First '当前文档的第一节

(76) ActiveDocument.Sections.First.PageSetup.BottomMargin '当前文档第一节所在页的底边距

(77) ActiveDocument.Sections.First.PageSetup.LeftMargin '当前文档第一节所在页的左边距

(78) ActiveDocument.Sections.First.PageSetup.RightMargin '当前文档第一节所在页的右边距

(79) ActiveDocument.Sections.First.PageSetup.TopMargin '当前文档第一节所在页的顶边距

(80) ActiveDocument.Sections.First.PageSetup.PaperSize '返回或设置当前文档第一节所在页的大小

(81) ActiveDocument.Sections.First.PageSetup.PageHeight '返回或设置当前文档第一节所在页的高度

(82) ActiveDocument.Sections.First.PageSetup.PageWidth '返回或设置当前文档第一节所在页的宽度

(83) ActiveDocument.Sections.Add Range:=myRange '在当前文档中添加新节

(84) ActiveDocument.Sections.Item(2) '当前文档中的第二节

(85) ActiveDocument.Sections.Last.Range.InsertAfter "文档结束!" '在当前文档中最后一节的结尾添加文字“文档结束!”

6、Range对象

(86) ActiveDocument.Range(Start:=0, End:=10) '表示当前文档前10个字符所组成的一个Range对象

(87) Set myRange = ActiveDocument.Range(Start:=ActiveDocument.Paragraphs(2).Range.Start, _

End:=ActiveDocument.Paragraphs(4).Range.End) '将当前文档第2段至第4段设置为一个Range对象

(88) ActiveDocument.Paragraphs(1).Range.Copy '复制当前文档中的第一段

(89) Selection.Copy

Documents.Add.Content.Paste '复制所选内容到新文档中

(90) ActiveDocument.Bookmarks("Book1").Copy Name:="Book2" '将Book2书签复制Book1书签标记的位置

(91) Selection.GoTo What:=wdGoToLine, Which:=wdGoToAbsolute, Count:=4 '将所选内容移至文档中的第4行

(92) Selection.GoTo What:=wdGoToTable, Which:=wdGoToNext '将所选内容移至下一个表格的第1个单元格

(93) Selection.Range.AutoFormat '为所选内容套用格式

(94) ActiveDocument.Content.Font.Name = "Arial" '将当前文档的字体设置为斜体

(95) ActiveDocument.Content.Select Selection.Delete '将当前文档中的内容删除其它

(96) Documents.Add '添加一个新文档

(97) Set myTable = ActiveDocument.Tables.Add(Selection.Range, 2, 2) '在当前文档所选区域添加一个2行2列的表格

7、文件读写

(98) Open "C:\my.txt" For Input As #1 '打开一个用于输入的文件并令其编号为1

(99) Line Input #1, TextLine '读取被打开用于输入且编号为1的文件

(100) Close #1 '关闭编号为1的文件

如何在Excel VBA 中读写word文档 步骤

1. 库的配置

在默认情况下,新创建的excel vba中不支持定义word对象。

所以需要先引入word库,操作步骤如下:

1.1 打开excel vba 界面

1.2 选中其中的一个Module

1.3 选择菜单, Tools --> References

在打开的对话框中选择类似 "Microsoft Word 14.0 Object Library".

1.4 点击OK保存配置。

2. 打开文档

Set wordApplication = CreateObject("Word.Application")

wordApplication.Visible = False

Dim hasOpenDoc As Boolean

hasOpenDoc = IsOpen(filePath) ' is a self-defined function to check file is opend

If hasOpenDoc = True then

Set wordDoc = GetObject(filePath)

End if

If hasOpenDoc = False Then

Set wordDoc = wordApplication.Documents.Open(filePath)

End if

wordDoc.Active

With wordApplication

Dim aParagraph As Word.Paragraph

For Each aParagraph In wordDoc.Paragraphs

' do some thing to every paragraph.

Next aParagraph

End with

wordDoc.Close

Set wordDoc = nothing

' 如下这段代码引用某位牛人的,非常感谢他。由于路径丢失,不能给出链接, 抱歉

' 如下的找寻方式,能够正确的找出文件是否被打开

Function IsOpen(fileName As String) As Boolean

IsOpen = False

Dim findFile As Integer

findFile = FreeFile()

On Error GoTo ErrOpen

Open fileName For Binary Lock Read Write As findFile

Close findFile

Exit Function

ErrOpen:

If Err.Number <> 70 Then

Msg = "Error # " & Str(Err.Number) & "was generated by " & Err.Source & Chr(13) & Err.Description

MsgBox Msg, "Error", Err.HelpFile, Err.HelpContext

Else

IsOpen = True

End If

End Function

如何在Excel VBA 中读写word文档 步骤

所以需要先引入word库,操作步骤如下:

1.1 打开excel vba 界面

1.2 选中其中的一个Module

1.3 选择菜单, Tools --> References

在打开的对话框中选择类似 "Microsoft Word 14.0 Object Library".

1.4 点击OK保存配置。

2. 打开文档

Set wordApplication = CreateObject("Word.Application")

wordApplication.Visible = False

Dim hasOpenDoc As Boolean

hasOpenDoc = IsOpen(filePath) ' is a self-defined function to check file is opend

If hasOpenDoc = True then

Set wordDoc = GetObject(filePath)

End if

If hasOpenDoc = False Then

Set wordDoc = wordApplication.Documents.Open(filePath)

End if

wordDoc.Active

With wordApplication

Dim aParagraph As Word.Paragraph

For Each aParagraph In wordDoc.Paragraphs

' do some thing to every paragraph.

Next aParagraph

End with

wordDoc.Close

Set wordDoc = nothing

' 如下这段代码引用某位牛人的,非常感谢他。由于路径丢失,不能给出链接, 抱歉

' 如下的找寻方式,能够正确的找出文件是否被打开

Function IsOpen(fileName As String) As Boolean

IsOpen = False

Dim findFile As Integer

findFile = FreeFile()

On Error GoTo ErrOpen

Open fileName For Binary Lock Read Write As findFile

Close findFile

Exit Function

ErrOpen:

If Err.Number <> 70 Then

Msg = "Error # " & Str(Err.Number) & "was generated by " & Err.Source & Chr(13) & Err.Description

MsgBox Msg, "Error", Err.HelpFile, Err.HelpContext

Else

IsOpen = True

End If

End Function

word vba 困扰我很久了

既然都困扰你很久的事情,为什么要求别人15分钟内回答呢,看来你给人奖励都不太诚心。

我熟悉Excel VBA,对Word VBA不熟悉,但我相信花些时间能解决这问题,所以先占个位置。

问一下标题是2、3行的,每行都有段落符号吗?

没有 但是字体都是3号 麻烦了

代码如下,详见附件。将附件文件放在需提取标题的文档同一文件夹,打开这个文件即可将本文件夹内所以doc文档的标题(第一段)提取放入同一文件夹ABC.xls文件中。

Private Sub Document_Open()

Dim k%, n%, Exna$, Wona$, Mn As Paragraph

Set ex = CreateObject("excel.application")

Exna = Dir(ThisDocument.Path & "\ABC.xls")

If Exna = "" Then

Set exwo = ex.Workbooks.Add

exwo.SaveAs ThisDocument.Path & "\ABC.XLS"

Else

Set exwo = ex.Workbooks.Open(ThisDocument.Path & "\ABC.XLS")

exwo.sheets(1).Columns("A").clearcontents

End If

'ex.Visible = True'显示Excel工作簿

Application.ScreenUpdating = False '关闭屏幕刷新

Wona = Dir(ThisDocument.Path & "\*.doc")

While Wona <> ""

If Wona <> ThisDocument.Name Then

Set mt = Documents.Open(ThisDocument.Path & "\" & Wona)

n = n + 1

exwo.sheets(1).Cells(n, 1) = mt.Paragraphs(1).Range.Text

mt.Close False

End If

Wona = Dir

Wend

exwo.Close True '保存并关闭工作簿文件

ex.Quit '退出Excel程序

Application.ScreenUpdating = True '恢复屏幕刷新

End Sub

Word VBA中,如何根据内容删除一行?

Sub DelBlank()

Dim i as Paragraph, n as Long

Application.ScreenUpdating = False

For Each i In ActiveDocument.Paragraphs

If Len(i.Range) = 1 Then

i.Range.Delete

n = n + 1

End If

Next

MsgBox "共删除空白段落" & n & "个。"

Application.ScreenUpdating = True

End Sub

摘自 EXCELHOME

word vba 判断表格

'一、表格标题一般应该是题注,判断题注内容

Selection.Paragraphs(1).Style="题注"

InStr(1,Selection.Paragraphs(1).Range.Text,"表")<>0'这里方法很多,可以通过域判断,也可以根据其他规则判断

'二、获取题注的结尾

styleEnd=Selection.Paragraphs(1).Range.End

'三、判断表注后面是否为表格

'参考2#

ActiveDocument.Range(styleEnd,styleEnd+1).Information(wdWithInTable)

'四、请尽量少使用Selection

'Selection使用得越少,代码效率越高

VBA怎样读取WORD文档自动生成的目录信息呢如题

OptionBase1

SubTest()

Dimol_Par(),myParAsParagraph,i_OLAsInteger,i_CountAsInteger,i_TempAsInteger,str_TempAsString

IfNotActiveWindow.DocumentMapThen

IfMsgBox("文档结构图没有打开,你要打开它吗?",1)=1Then

ActiveWindow.DocumentMap=True

Else

ExitSub

EndIf

EndIf

ForEachmyParInActiveDocument.Paragraphs

i_OL=myPar.OutlineLevel

Ifi_OL<10Then

i_Count=i_Count+1

ReDimPreserveol_Par(2,i_Count)

ol_Par(1,i_Count)=i_OL

Setol_Par(2,i_Count)=myPar.Range

EndIf

Next

Fori_Temp=1Toi_Count

MsgBox"文档结构图中第"&i_Temp&"行文字为:"&ol_Par(2,i_Temp)&"大纲级别为:"&ol_Par(1,i_Temp)&"级"

str_Temp=str_Temp&ol_Par(2,i_Temp)

Next

MsgBox"文档结构图中的所有文字为:"&str_Temp

EndSub

上面是在CSDN重找的代码,楼主可以参考一下

转载请注明出处51数据库 » vbawordparagraph ExcelVBA操作word文件

软件
前端设计
程序设计
Java相关