excel vba 如何检查word文档某段的内容
时间:2008-12-27
来源:互联网
检查某个目录下的所有word类型的文档是否包含标题“2.3.4.1 测试结论”和“2.3.4.2 测试说明”
如果没有,返回无;
如果有,返回“2.3.4.1 测试结论”和“2.3.4.2 测试说明”之间的内容。
请教如何写代码
非常感谢!!!

[ 本帖最后由 sandwich 于 2009-1-4 08:47 编辑 ]
作者: sandwich 发布时间: 2008-12-27
Sub test()
Dim a As String, WdApp As Word.Application, myDoc As Word.Document
Dim i As Integer, myinfo As String, n As Integer, myRange As Word.Range
On Error Resume Next
ChDir "E:\" '暂定测试路径
a = Dir("*.doc")
Set WdApp = CreateObject("Word.Application")
With WdApp
.ScreenUpdating = False
Do While a <> ""
If a Like "[!~]*" Then
Set myDoc = .Documents.Open(Filename:="E:\" & a, ConfirmConversions:=False, NoEncodingDialog:=True)
i = i + 1
myinfo = myinfo & i & vbTab & myDoc.Name & Chr(11)
With myDoc.Content.Find
.Text = "2.3.4.1 测试结论^p"
If .Execute Then
n = n + 1
Set myRange = .Parent.Duplicate
.Parent.SetRange .Parent.End, myDoc.Content.End
If .Execute("2.3.4.2 测试说明^p") Then
myRange.End = .Parent.Start
End If
myinfo = myinfo & myRange
Else
myinfo = myinfo & "无" & vbCrLf
End If
End With
myDoc.Close False
a = Dir
End If
Loop
.Visible = True
.Documents.Add.Content = myinfo
.ScreenUpdating = True
End With
Set WdApp = Nothing
MsgBox "共搜索到" & i & "个文档,其中包含标题“2.3.4.1 测试结论”的有" & n & "个。"
End Sub
[ 本帖最后由 sylun 于 2008-12-27 15:01 编辑 ]
作者: sylun 发布时间: 2008-12-27
用户类型未定义
如何解决?
谢谢。
作者: sandwich 发布时间: 2008-12-29
作者: sylun 发布时间: 2008-12-29
谢谢。
作者: sandwich 发布时间: 2008-12-29
Set WdApp = Nothing
之后,任务管理器中word应用程序仍然在,没有关闭。
如何才能关闭?
谢谢。
作者: sandwich 发布时间: 2008-12-29
如: 2.3.4.1.1 2.3.4.1.2 等,有什么方法可以包括这些标题?
谢谢。
作者: sandwich 发布时间: 2008-12-30
引用:
原帖由 sandwich 于 2008-12-29 16:09 发表再请教一下:
Set WdApp = Nothing
之后,任务管理器中word应用程序仍然在,没有关闭。
如何才能关闭?
谢谢。
引用:
原帖由 sandwich 于 2008-12-30 16:34 发表再请教一下,这个程序从word中获得的内容,标题不包括在内
如: 2.3.4.1.1 2.3.4.1.2 等,有什么方法可以包括这些标题?
谢谢。
作者: sylun 发布时间: 2008-12-31
这个程序从word中获得的内容,这些编号没有包括在内
有什么方法可以包括这些编号?
谢谢。
作者: sandwich 发布时间: 2009-01-01
作者: sylun 发布时间: 2009-01-01
应该如何修改代码?
谢谢!
Set myDoc = .Documents.Open(Filename:=DirStr & "\" & a, ConfirmConversions:=False, NoEncodingDialog:=True)
myDoc.Content.ListFormat.ConvertNumbersToText
With myDoc.Content.Find
.Text = "2.3.4.1 测试结论^p"
If .Execute Then
作者: sandwich 发布时间: 2009-01-01
楼主可检查实际文档中编号后面跟什么字符,再作相应改动,也可试试将查找字符代码修改为:"2.3.4.1[^9^32 ]@测试结论^p",另一个查找代码也作相应改动。
作者: sylun 发布时间: 2009-01-01
.Text = "2.3.4.1 * 测试结论^p"
也查找失败。
如何编写代码能达到类似通配符的方法?
作者: sandwich 发布时间: 2009-01-01
With myDoc.Content.Find
.MatchWildcards = True
.Text = "2.3.4.1[^9^32 ]@测试结论^13"
后一个.text字符串类似。
作者: sylun 发布时间: 2009-01-01
.Text = "2.3.4.1[^9^32 ]@测试结论^13"
而不用
.Text = "2.3.4.1*测试结论^13"
谢谢。
作者: sandwich 发布时间: 2009-01-03
不论.Text设置为什么,及时文档中不存在的字符,
If .Execute Then
都是返回true。
作者: sandwich 发布时间: 2009-01-03
请楼主提供实际文档样本,否则我只能猜,这样讨论效率不高。
作者: sylun 发布时间: 2009-01-03
需要提取测试结论和测试说明间的内容,
两篇文档的“4.4.6.1 测试结论” 中“4.4.6.1”和“测试结论”间据观察应该是tab字符和空格字符或两者的组合,
请帮忙看看如何写代码。
谢谢。
作者: sandwich 发布时间: 2009-01-04
Sub test()
Dim dirStr As String, a As String, WdApp As Word.Application, myDoc As Word.Document
Dim i As Integer, myinfo As String, n As Integer, myRange As Word.Range
dirStr = "E:\" '暂定测试路径
ChDrive dirStr
ChDir dirStr
a = Dir("*.doc")
Set WdApp = CreateObject("Word.Application")
With WdApp
.ScreenUpdating = False
Do While a <> ""
If a Like "[!~]*" Then
Set myDoc = .Documents.Open(Filename:=dirStr & a, ConfirmConversions:=False, NoEncodingDialog:=True)
i = i + 1
myinfo = myinfo & i & vbTab & myDoc.Name & Chr(11)
myDoc.Content.ListFormat.ConvertNumbersToText
With myDoc.Content.Find
.MatchWildcards = True
.Text = "4.4.6.1[^9^32 ]@测试结论^13"
If .Execute Then
n = n + 1
Set myRange = .Parent.Duplicate
.Parent.SetRange .Parent.End, myDoc.Content.End
If .Execute("4.4.6.2[^9^32 ]@测试说明^13") Then
myRange.End = .Parent.Start
End If
myinfo = myinfo & myRange
Else
myinfo = myinfo & "无" & vbCrLf
End If
End With
myDoc.Close False
a = Dir
End If
Loop
.Visible = True
.Documents.Add.Content = myinfo
.ScreenUpdating = True
End With
Set WdApp = Nothing
MsgBox "共搜索到" & i & "个文档,其中包含标题“4.4.6.1 测试结论”的有" & n & "个。"
End Sub
[ 本帖最后由 sylun 于 2009-1-4 11:40 编辑 ]
作者: sylun 发布时间: 2009-01-04
再请教一下:
在VBE工具菜单“引用”项列表选中引用相应的MicroSoft Word对象库
能否用代码实现这个功能?
作者: sandwich 发布时间: 2009-01-04
[^9^32 ]是查找^9和^32之间的任意一个字符还是可以多个?
为什么后面加一个@?
谢谢。
作者: sandwich 发布时间: 2009-01-05
[ 本帖最后由 sandwich 于 2009-1-5 15:36 编辑 ]
作者: sandwich 发布时间: 2009-01-05
[ 本帖最后由 sylun 于 2009-1-5 15:45 编辑 ]
作者: sylun 发布时间: 2009-01-05
作者: yjm36 发布时间: 2011-06-12
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28