首先我想說我有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