首先我想说我有 VBA 的基本知识。
我目前正在尝试在 Word 中创建一个宏,我想将其链接到打开的 Excel 文件并从打开的 Word 文件的 A 列中搜索一个单词。然后我想让宏在 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