検索する単語を含むファイルがあります。アクティブなドキュメントでそれらの単語を強調したいと思います。
例:style.docx:上記のアグロコマーシャルいくつかの単語物理学学校
アクティブなドキュメント:
上記や学校などの言葉を説明する行です。これで行の終わりです。
期待される出力:上記のいくつかの単語は、アクティブなドキュメントで強調表示する必要があります。
私は以下のコードを試しました:
Dim docTitle As Document
Dim docStyle As Document
Set docTitle = Documents.Open(FileName:="C:\Documents and Settings\quads\Desktop\stylesheet.docx", ConfirmConversions:=True)
Set docStyle = Documents.Open(FileName:="C:\Documents and Settings\quads\Desktop\files\Albala D-ed.doc", ConfirmConversions:=True)
Dim char As Long
Dim x As Long
Dim count As Integer
Dim Para As Paragraph
For Each Para In docTitle.Paragraphs
If Len(Para.Range.Text) > 0 Then
ActiveDocument.Range(0, 0).Select
Selection.Find.ClearFormatting
With Selection.Find
.Text = Left(Para.Range.Text, Len(Para.Range.Text) - 1)
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = True
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
End If
ActiveDocument.Range(0, 0).Select
Next Para
また、ファイル名を指定せずに、特定のフォルダー(「ファイル」)内のすべてのファイルを検索したいと思います。
複数の検索された単語(他のファイルから)を黄色で強調表示する回答を貼り付けました。検索単語のファイルと強調表示するファイルは、ファイルダイアログオブジェクトを使用して選択できます。
Dim filepath As String
Dim filename As FileDialog
Dim stylepath As String
Dim stylename As FileDialog
MsgBox ("Please choose Style File Name")
Set stylename = Application.FileDialog(filedialogtype:=msoFileDialogFilePicker)
If stylename.Show Then
stylepath = stylename.SelectedItems(1)
End If
Set filename = Application.FileDialog(filedialogtype:=msoFileDialogFilePicker)
If filename.Show Then
filepath = filename.SelectedItems(1)
End If
Dim range As range
Dim i As Long
Dim arr() As String
Dim docTitle As Document
Dim docStyle As Document
Set docTitle = Documents.Open(stylepath)
Set docStyle = Documents.Open(filepath)
arr = Split(docTitle.Content.Text, Chr(13))
For i = 0 To UBound(arr)
Set range = docStyle.range
With range.Find
.Text = arr(i)
.Format = True
.MatchCase = True
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
Do While .Execute(Forward:=True) = True
range.HighlightColorIndex = wdYellow
Loop
End With
Next
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加