pgjava
 1    Dim word As Object
 2    Set word = CreateObject("word.Application")
 3    word.Visible = True \' Word设置为可见
 4    Set doc = word.Documents.Open(Filename:=filePath, ReadOnly:=True) \' filePath为file路径,只读的形式打开
 5         Do While Range("G" & index).value <> "" \' 只要excel的G列不为空,就循环
 6             word.Selection.Find.ClearFormatting
 7             word.Selection.Find.Replacement.ClearFormatting
 8             With word.Selection.Find
 9                 .Text = Range("G" & index).value
10                 .Replacement.Text = Range("H" & index).value
11                 .Forward = True
12                 .Wrap = wdFindContinue
13                 .Format = False
14                 .MatchCase = True
15                 .MatchWholeWord = False
16                 .MatchByte = True
17                 .MatchAllWordForms = False
18                 .MatchSoundsLike = False
19                 .MatchWildcards = False
20                 .MatchFuzzy = False
21             End With
22             
23             If word.Selection.Find.Execute Then \' 如果检索到,就把word文件信息写到excel里面
24                 ActiveCell.Offset(0, 1).value = doc.Name
25                 ActiveCell.value = Replace(Replace(file, DocFolder, ""), doc.Name, "")
26                 ActiveCell.Offset(0, 2).value = Range("G" & index).value
27                 ActiveCell.Offset(1).Select \' excel下移一行
28             End If
29    word.ActiveDocument.Close False \' 不保存关闭
30    word.Quit \' word退出

分类:

技术点:

相关文章: