위에서 언급했듯이이 작업을 수행하는 더 좋은 방법은 테이블을 탐색하여 책갈피를 찾는 것입니다. 아래 스크립트는 첫 번째 표의 두 번째 열을 모두 탐색하여 모든 북마크를 찾습니다. 찾은 책갈피는 다른 문서 "Document to Populate.docx"에서 설정 한 책갈피에 해당합니다. 대신 다음 테이블 셀을 찾는 북마크 통해 반복, 나는이 테이블 셀 통해 반복하고 있습니다 :
Sub AutomateQuestionnaire2()
' Prototype 2
' Different approach was used, instead of looping through bookmarks, loop
' through the tables looking for bookmarks. This method is more flexible
' and is better suited for our Word documents which always include tables.
' Limitations: Bookmark needs to be in both documents using the same ID, and
' data must be in a table, column 2.
Dim oRow As Row
Dim oRange As Range
Dim oFindRange As Range
Dim oBookmark As Bookmark
Dim oQuestionnaire As Word.Document
Dim oApp As Word.Application
Dim strFilePath As String
Dim strText As String
strFilePath = ActiveDocument.Path
'Open the second to populate it
Set oApp = New Word.Application
oApp.Visible = True
Set oQuestionnaire = oApp.Documents.Open(strFilePath + "\Document to Populate.docx")
'We'll loop through each row of the table looking for bookmarks, if a bookmark is found
'the text adjacent to that bookmark, in the table cell, will be copied to the same
'bookmark if found in the questionnaire.
For Each oRow In ActiveDocument.Tables(1).Rows
'Limits the range to the middle column as is the case for the ITGC 532 form
Set oRange = oRow.Cells(2).Range
Set oBookmark = oRange.Bookmarks(1)
'VBA will terminate the script if it comes across an error, instead
'let's add some error handling to skip errors.
On Error GoTo SkipToNext
strText = oRange.Text
oQuestionnaire.Bookmarks(oBookmark).Range.Text = strText
'Find the newly inputted text and differentiate it (bold for now)
Set oFindRange = oQuestionnaire.Content
oFindRange.Find.Execute FindText:=strText, Forward:=True
If oFindRange.Find.Found = True Then oFindRange.Font.ColorIndex = wdBlue
SkipToNext:
Next
난 사실이 다른 경로를 가고 해결 : 책갈피가 발견 될 때마다 다음과 같이 ActiveDocument를 데이터는 두 번째 문서로 채워집니다 책갈피를 찾는 것. 이 방법은 매우 쉽습니다. 다른 방법이 가능한지 나는 알 수 없습니다. 이 문제를 겪는 다른 모든 사람들을위한 FYI. – AutoM8R
솔루션을 답변으로 게시하지 않는 이유는 무엇입니까? –
가능한지 모르겠다. 지금 코드를 게시하고 있습니다. – AutoM8R