|
* 楼主,请试用下面的宏。如果频繁使用可设置为热键,比如:F8
* 首先,请 楼主 自行将 Excel 中的所有公司名称复制到新建 Word 文档中,另存为到 D 盘,文件名为“正确公司名称”(因为 Excel 代码我不会,高手老师们会,但他们一般时候不来,我常来)。
* 找到的公司标蓝色,未找到标红色。请按 Alt + F8 打开宏名列表,第一个宏就是本宏。
* 如果代码是在 Word 2019 下面使用,请复制代码后粘贴在 Word 的新建文档中,全选,剪切,再粘贴到 VBE 中,否则,可能有乱码。
- Sub a0001_Check_Company_Name()
- '检查公司名称:找到蓝色,未找到红色
- Dim arr, i&, j&, s$
-
- Documents.Open FileName:="D:\正确公司名称.docx"
-
- With Selection
- .Tables(1).Select
- .Rows.ConvertToText Separator:=wdSeparateByParagraphs, NestedTables:=True
- .Next.Delete
- i = .Paragraphs.Count
- End With
-
- ReDim arr(0 To i - 1)
-
- For j = 0 To i - 1
- arr(j) = Replace(ActiveDocument.Paragraphs(j + 1).Range.Text, vbCr, "")
- Next
-
- ActiveDocument.Close SaveChanges:=wdDoNotSaveChanges
-
- '全文查找所有公司名称,标记为红色
- With ActiveDocument
- With .Content
- With .Font
- .NameFarEast = "宋体"
- .NameAscii = "Times New Roman"
- .ColorIndex = wdAuto
- End With
- .InsertBefore Text:=","
- End With
-
- With .Content.Find
- .ClearFormatting
- .Text = "公司"
- .Forward = True
- .MatchWildcards = True
- Do While .Execute
- With .Parent
- .Select
- With Selection
- Do
- .MoveStart 1, -1
- Loop Until .Previous.Text Like "[!一-﨩]"
- .Font.ColorIndex = wdRed
- End With
- End With
- Loop
- End With
-
- '全文查找《正确公司名称》文档里面的公司名称,标记为蓝色
- For j = 0 To i - 1
- With .Content.Find
- .ClearFormatting
- .Text = arr(j)
- .Forward = True
- .MatchWildcards = True
- Do While .Execute
- With .Parent
- .Font.Color = wdColorBlue
- End With
- Loop
- End With
- Next
-
- .Characters.First.Delete
-
- '附加功能:全文查找红色文字(未找到的公司名称)并放到文首
- With .Content.Find
- .ClearFormatting
- .Font.ColorIndex = wdRed
- .Forward = True
- .MatchWildcards = True
- Do While .Execute
- With .Parent
- .Select
- Selection.MoveEnd
- s = s & Selection.Text
-
- Selection.Cut
- End With
- Loop
- End With
-
- .Content.InsertBefore Text:=s & vbCr & vbCr
- With .Content.Find
- .ClearFormatting
- .Text = "^p^p"
- .Forward = True
- .MatchWildcards = False
- Do While .Execute
- .Parent.Select
- Exit Do
- Loop
- End With
-
- With Selection
- .HomeKey 6, 1
- .Font.ColorIndex = wdRed
- .Find.Execute "(公司)?", , , 1, , , , , , "\1^p", 2
- .HomeKey 6
- End With
-
- .Content.InsertBefore Text:="未找到的公司:" & vbCr
-
- With .Paragraphs(1).Range.Font
- .Name = "黑体"
- .Bold = True
- .Underline = wdUnderlineDouble
- End With
- End With
- MsgBox "处理完毕!!!", 0 + 48
- End Sub
复制代码 |
评分
-
2
查看全部评分
-
|