%20%E3%82%92%E6%A4%9C%E7%B4%A2%E3%81%97%E3%80%81%E5%88%97%20B%20%E3%81%AE%E5%86%85%E5%AE%B9%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%E3%81%9D%E3%81%AE%E5%8D%98%E8%AA%9E%E3%81%AB%E3%82%B3%E3%83%A1%E3%83%B3%E3%83%88%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B%E3%81%AB%E3%81%AF%E3%81%A9%E3%81%86%E3%81%99%E3%82%8C%E3%81%B0%E3%82%88%E3%81%84%E3%81%A7%E3%81%97%E3%82%87%E3%81%86%E3%81%8B%E3%80%82.png)
まず、私は VBA の基本的な知識を持っていることを述べたいと思います。
現在、開いている Excel ファイルにリンクし、開いている Word ファイルの列 A から単語を検索するマクロを Word で作成しようとしています。次に、マクロで Word のその単語にコメントを配置し、Excel ファイルの列 B の内容を挿入するようにしたいと思います。
異なるソースからの異なるコード行を組み合わせようとしましたが、うまくいかないようです。
これを行う方法を知っている人はここにいますか?
よろしくお願いします!
コメントを追加するための次のコードがあります (ただし、Excel ファイルにはリンクされていません)。
Selection.Find.ClearFormatting
Dim range As range
Set range = ActiveDocument.Content
Do While range.Find.Execute("XXXXXX") = True
ActiveDocument.Comments.Add range, "YYYYYY"
Loop
次のコードは、列 A の内容を検索し、それを列 B の内容に置き換えます。
Dim ws As Activesheet, msWord As Object, itm As range
Set ws = Activesheet
Set msWord = ActiveDocument.Content
With msWord ' cant figure out how to change this so it uses the currently open Word file.
.Visible = True
.Documents.Open "F:\Test folder\TestFolder\Test.docx"
.Activate
With .ActiveDocument.Content.Find
.ClearFormatting
.Replacement.ClearFormatting
For Each itm In ws.UsedRange.Columns("A").Cells
.Text = itm.Value2 'Find all strings in col A
.Replacement.Text = itm.Offset(, 1).Value2 'Replacements from col B
.MatchCase = False
.MatchWholeWord = False
.Execute Replace:=2 'I guess this should be replaced with the code that places the text YYYYYY as a comment on the text XXXXXX?
Next
End With
.Quit SaveChanges:=True
End With