2012-09-10 6 views
0

내 북마크가 내 Word 문서에있는 표 셀을 찾으려고합니다. 북마크를 반복해도 문제가 없습니다. 아주 간단합니다. 이제 북마크가있는 표 셀을 식별하려고 시도하지만이 작업을 수행하는 데 어려움을 겪고 있습니다.표 셀 찾기 Bookmark within?

또는 데이터를 북마크에 묶는 방법 (예 : 동봉 된 북마크 사용)이 다른 문서에서 참조되고 복사 될 수 있습니까? 셀의 텍스트를 자주 변경해야하고 사용자가 매번 새 텍스트를 책갈피에 추가하지 않아도되므로 묶인 책갈피를 사용할 수 없습니다.

+0

난 사실이 다른 경로를 가고 해결 : 책갈피가 발견 될 때마다 다음과 같이 ActiveDocument를 데이터는 두 번째 문서로 채워집니다 책갈피를 찾는 것. 이 방법은 매우 쉽습니다. 다른 방법이 가능한지 나는 알 수 없습니다. 이 문제를 겪는 다른 모든 사람들을위한 FYI. – AutoM8R

+0

솔루션을 답변으로 게시하지 않는 이유는 무엇입니까? –

+0

가능한지 모르겠다. 지금 코드를 게시하고 있습니다. – AutoM8R

답변

0

위에서 언급했듯이이 작업을 수행하는 더 좋은 방법은 테이블을 탐색하여 책갈피를 찾는 것입니다. 아래 스크립트는 첫 번째 표의 두 번째 열을 모두 탐색하여 모든 북마크를 찾습니다. 찾은 책갈피는 다른 문서 "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 
+0

일부 코드 주석은 완벽하지 않습니다. 설문지는 내가 채색하려고하는 것입니다. – AutoM8R