Pratheek P Manangi
Pratheek P Manangi

Reputation: 341

how to update table in word?

I am working on the bearing report. I have to copy and find the relevant bearing data from the excel file and paste it in the word table. I have figured out the codes to

  1. To go to the relavant location in the word file and paste some data in desired word document.

    Sub CreateNewWordDoc()
        Dim wrdApp As Word.Application
        Dim wrdDoc As Word.Document
        Dim i As Integer
        Dim arr(12)
        'Bearing numbers I need to search
        arr(0) = "(249_L), 38,7 %"
        arr(1) = "(248_R), 38,7 %"
        arr(2) = "(249_M), 38,7 "
        arr(3) = "(3560), 38,7 "
        arr(4) = "(3550), 38,7 %"
        arr(5) = "(349_), 38,7 %"
        arr(6) = "(348_), 38,7 %"
        arr(7) = "(451), 38,7 %"
        arr(8) = "(450L), 38,7 "
        arr(9) = "(450R), 38,7 "
        arr(10) = "(151), 38,7 %"
        arr(11) = "(150L), 38,7 %"
        arr(12) = "(150R), 38,7 %"
        Set wrdApp = CreateObject("Word.Application")
        wrdApp.Visible = True 
        'location of my word document
    
        Set wrdDoc = wrdApp.Documents.Open("E:\ShareDrive_Ruehl\full-flexible-MBS-models_report\example-report\FullFlexibleGearbox - Copy (2).docx")
        wrdDoc.Activate
    
        wrdApp.Selection.HomeKey unit:=wdStory
        'for loop to reach all bearing location
        For i = 0 To 12
            With wrdApp.Selection
                With .Find
                    .ClearFormatting
                    .MatchWildcards = False
                    .MatchWholeWord = False
                    .Text = arr(i)
                    .Execute
                End With
                ' Here is where I need to paste my copied data.
    
                .InsertAfter "I can just paste this shit"
                .HomeKey unit:=wdStory
            End With
        Next   
    End Sub
    
  2. Go to the location in the excel file, find the relevant data and copy the data related to that and here is the code for that.

    Sub CopyToWord()
        'Copy the range Which you want to paste in a New Word Document
        Cells.Find(What:=arr(0), After:=ActiveCell, LookIn:=xlFormulas _
          , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
          MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset(2, 0).Range("A1:g8").Select
        Selection.Copy
    End Sub
    

I have written both these codes to be operated from Excel VBA. But Now I have to combine both and paste the copied data from 2nd code to the table which is located in the 1st code (The location of the place is not just after the location where i find the word. To go to that location I know the code and can be better understood by the pics given below.).

This is the code for me to select the data in the word where I need to replace. I need to write similar in word and replace that with the copied data

Sub pasting()
    Dim sSample, rResult As String
    sSample = "(450R), 38,7 % "

    Set rRange = ActiveDocument.Content
    Selection.Find.Execute FindText:=sSample, _
      Forward:=True, Wrap:=wdFindStop
    Selection.MoveDown unit:=wdLine, Count:=1
    Selection.EndKey unit:=wdLine
    Selection.MoveRight unit:=wdCharacter, Count:=1
    Selection.EndKey unit:=wdLine
    Selection.MoveDown unit:=wdLine, Count:=1

    Selection.MoveDown unit:=wdLine, Count:=5, Extend:=wdExtend
    Selection.MoveLeft unit:=wdCharacter, Count:=5, Extend:=wdExtend
    Selection.PasteAndFormat (wdPasteDefault)
End Sub

Unfortunately, Though I have copied the data what I want I am not able to arrive at the solution. I don't know how to paste data in the existing table.

This picture explains better. I need to search data of bearing 248_R in excel and paste that in word. This is the Word file

enter image description here

this is the Excel file

enter image description here

Upvotes: 1

Views: 2118

Answers (2)

Pratheek P Manangi
Pratheek P Manangi

Reputation: 341

Sub CreateNewWordDoc()
    Dim wrdApp As Word.Application
    Dim wrdDoc As Word.Document



    Dim arr(12)
    'Bearing numbers I need to search
    arr(0) = "(249_L), 38,7 %"
    arr(1) = "(248_R), 38,7 %"
    arr(2) = "(249_M), 38,7 "
    arr(3) = "(3560), 38,7 "
    arr(4) = "(3550), 38,7 %"
    arr(5) = "(349_), 38,7 %"
    arr(6) = "(348_), 38,7 %"
    arr(7) = "(451), 38,7 %"
    arr(8) = "(450L), 38,7 %"
    arr(9) = "(450R), 38,7 %"
    arr(10) = "(151), 38,7 %"
    arr(11) = "(150L), 38,7 %"
    arr(12) = "(150R), 38,7 %"

    range2 = 6


    Set wrdApp = CreateObject("Word.Application")
    wrdApp.Visible = True
    'location of my word document

    Set wrdDoc = wrdApp.Documents.Open("E:\Siemens\FullFlexibleGearbox.docx")
    wrdDoc.Activate

    wrdApp.Selection.HomeKey Unit:=wdStory
    'for loop to reach all bearing location
    For i = 0 To 12
             Cells.Find(What:=arr(i), After:=ActiveCell, LookIn:=xlFormulas, _
             LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
                MatchCase:=False, SearchFormat:=False).Activate
                ActiveCell.Offset(2, 0).Range("A1:G8").Select
                Application.CutCopyMode = False
                Selection.Copy

        With wrdApp.Selection
            With .Find
                .ClearFormatting
                .MatchWildcards = False
                .MatchWholeWord = False
                .Text = arr(i)
                .Execute
            End With
            .MoveRight Unit:=wdCharacter, Count:=2
            .MoveDown Unit:=wdLine, Count:=1
            .MoveDown Unit:=wdLine, Count:=6, Extend:=wdExtend
            .MoveLeft Unit:=wdCharacter, Count:=6, Extend:=wdExtend
            .Paste
            .HomeKey Unit:=wdStory



        End With
    Next
    End Sub

Thanks for your support people. :)

Upvotes: 0

Harassed Dad
Harassed Dad

Reputation: 4704

Forget copying and pasting. Instead once you have found your data (in 2) assign the range to a variable of type variant. it will now be an array inside your variable Now you can loop through this assigning each element to a cell within your table I'm at work so I can't see your images, but remember that cells in a Word table are referred to as Cell(row,column) - so you can write

 with Wrdapp.documents(1).tables(1)
      For x = 0 to ubound(v,1)
          for y = 0 to ubound(v,2)
            .cell(x + 1,y + 1).range.text = v(x,y)
          next y
      next x

 end with

to copy an array V into the the first table in a document (The +1 in cell is because arrays count from zero, but Word tables run from one so v(0,0) needs to go to cell(1,1)

Hopefully that should get you started

Upvotes: 2

Related Questions