Может ли кто-нибудь помочь мне выяснить, как провести oop через ячейки, и каждый раз, когда я вхожу в ячейку, мне нужно возвращать имя заголовка столбца в виде строки. Я использую массив, который содержит закладки из текстового документа. Я сопоставляю экземпляр закладки в слове do c. с именем заголовка столбца, который я пытаюсь сопоставить; позволить программе взять все, что находится в ячейках в i, j, а затем поместить его в выделенную область, где находится закладка в документе word?
Sub Export_Table_Data_Word()
'Name of the existing Word document
Const stWordDocument As String = "test.docx"
'Word objects.
Dim wdApp As Word.Application
Dim wdDoc As Word.Document
Dim wdCell As Object
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim bookmarkFieldDis(0 To 5) As String
Dim wSF As WorksheetFunction
bookmarkFieldDis(0) = "Bkm_Pg1_DateRun"
bookmarkFieldDis(1) = "Bkm_Pg1_LeftHeader_Foot1"
bookmarkFieldDis(2) = "Bkm_Pg1_LeftHeader_Row2"
bookmarkFieldDis(3) = "Bkm_Pg1_LeftHeader_Row3"
bookmarkFieldDis(4) = "Bkm_Pg1_LeftHeader_Row5"
bookmarkFieldDis(5) = "Bkm_Pg1_WkInstHeader"
'Excel objects
Dim wbBook As Workbook
Dim wsSheet As Worksheet
'Count used in a FOR loop to fill the Word table.
Dim lnCountItems As Long
'Variant to hold the data to be exported.
Dim vaData As Variant
'Initialize the Excel objects
Set wbBook = ThisWorkbook
Set wsSheet = wbBook.Worksheets("Sheet1")
Set vaData = wsSheet.Range("A1").CurrentRegion
Set wSF = WorksheetFunction
'Instantiate Word and open the "Table Reports" document.
Set wdApp = New Word.Application
wdApp.Visible = True ' making the Word App Visible
wdApp.Activate ' opening Word App\
Set wdDoc = wdApp.Documents.Open(wbBook.Path & "\" & stWordDocument)
If wdDoc Is Nothing Then
Exit Sub ' We do this if the file does not exist
End If
'Place the data from the variant into the table in the Word doc.
For i = 1 To UBound(vaData.Value, 1) 'row/two dementional array
For j = 1 To UBound(vaData.Value, 2) ' 1 = column
For k = 0 To UBound(bookmarkFieldDis, 1)
If wSF.Index(Range("A1:AD"), 0, wSF.Match(bookmarkFieldDis(k), wSF.vaData.Index(Range("A1:AD")))) = bookmarkFieldDis(k) Then
ActiveDocument.Bookmarks(bookmarkFieldDis(k)).Select
ActiveDocument.Bookmarks(bookmarkFieldDis(k)).Delete
ActiveDocument.Bookmarks.Add (vaData.Range.Cells(i, j).Value)
End If
Next k
Next j
Next i
'Save and close the Word doc.
With wdDoc
.Save
.Close
End With
wdApp.Quit
'Null out the variables.
Set wdCell = Nothing
Set wdDoc = Nothing
Set wdApp = Nothing
MsgBox "The " & stWordDocument & "'s table has successfully " & vbNewLine & _
"been updated!", vbInformation
End Sub