Copying excel cell range to certain cell range (2,2 to 5,7) in a word table using vba

Dmitry 21 Reputation points
2021-03-05T13:55:23.013+00:00

Hello,

I want to make a code on an excel workbook that updates tables with values from ranges in that workbook. This is my code so far that recognizes the table in a word document that has a specific value in a table. I want to populate this table like I said before, and then move on to the subsequent 9 or 10 tables and populate those from ranges in the excel workbook. I am just having trouble figuring out a way to copy a range from excel (lets say D46:G51 from the first sheet in an excel workbook) to cells in a word table (2,2 to 5,7). And then moving on to the next table and doing the same but with a different range from the same excel workbook.

Sub UpdateTables2()

Do

    Dim documentname As String
    documentname = InputBox("Paste or Type the name of the word document")
     Dim myCells As Range

Loop Until Len(documentname) > 0

Dim myDoc As Word.Document
Set myDoc = Documents(documentname)

Dim myTable As Word.Table
For Each myTable In myDoc.StoryRanges(wdMainTextStory).Tables   ' Loop through each table in the document and stop when the first cell says attribute in it'

    If InStr(myTable.Cell(1, 1).Range.Text, "Attribute") > 0 Then 

        myTable.Select

        Dim myWb As Excel.Workbook
        'Populate this graph and then populate the next few graphs with different excel workbook ranges'


        Set myCells = .Range(Start:=.myTable.Cell(2, 2).Range.Start, _
        End:=.myTable.Cell(5, 7).Range.End)
    myCells.Select

        ThisWorkbook.Sheets(1).Range("D46:G51").Copy
        myCells.Paste




        Exit For

    Else 'If first cell is not Att then end the if statement and loop again'

    End If

Next

End Sub

Word Management
Word Management
Word: A family of Microsoft word processing software products for creating web, email, and print documents.Management: The act or process of organizing, handling, directing or controlling something.
893 questions
{count} votes

Accepted answer
  1. Anonymous
    2021-03-06T05:25:25.69+00:00

    For example:

    Dim xlWs As Excel.Worksheet, wdApp As Word.Application, wdDoc As Word.Document, wdTbl As Word.Table
    Dim r As Long, c As Long
    Set xlWs = Activesheet: Set myDoc = wdApp.Documents(documentname)
    With wdDoc
      For Each wdTbl In .Tables   ' Loop through each table in the document and stop when the first cell says attribute in it'
        With wdTbl
          If InStr(.Cell(1, 1).Range.Text, "Attribute") > 0 Then
            'Populate this table and then populate the next few tables with different excel workbook ranges'
            For r = 2 To 5
              For c = 2 To 7
                .Cell(r, c).Range.Text = xlWs.Range(r + 44, c + 2).Text
              Next
            Next
          ElseIf InStr(.Cell(1, 1).Range.Text, "Quality") > 0 Then
            For r = 2 To 4
              For c = 2 To 9
                .Cell(r, c).Range.Text = xlWs.Range(r + 5, c).Text
              Next
            Next
          End If
        End With
      Next
    End With
    

    Note that I haven't included any of the code for instantiatiating Excel or Word, or for opening the document or workbook. It isn't at all clear from your post which application the code is being run from and, in any event, I assume you already the code for all of that.


1 additional answer

Sort by: Most helpful
  1. Tom van Stiphout 1,621 Reputation points MVP
    2021-03-05T15:36:49.06+00:00

    ican'treadthecodeifitsalltogetherandnotformattedproperly