View Single Post
 
Old 06-18-2018, 08:45 PM
gmayor's Avatar
gmayor gmayor is offline Windows 10 Office 2016
Expert
 
Join Date: Aug 2014
Posts: 4,144
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

Assuming your bookmarks d5, d20 & D22 refer to the column and row of the word table then also assuming that the value of 'i' is correctly resolved (we don't have your sheet to test) then the following should work . Depending on what is in Range("E" & i) you don't have to select and copy it. Just write its content to the Word table.


The code uses late binding to Word and so does not need a reference to Word.



Code:
Sub CopyAndPaste()
Dim myfile, wdApp As Object, wdDoc As Object
Dim oRng As Object
Dim i As Integer, iRow As Integer
    'select truck report file
    ChDrive "E:\"
    ChDir "E:\WG\TVAL\"
    
    myfile = Application.GetOpenFilename(, , "Browse for Document")
    'searches for row with "avg" then selects column E(avg of temperature mean) of that row.
    i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)
    'copies the cell - not necessary if you simply write the range
    Range("E" & i).Copy
    
    Set wdApp = GetObject(, "Word.Application")
    If Err Then
        Set wdApp = CreateObject("Word.Application")
    End If
    On Error GoTo 0
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Open(myfile)
    iRow = Range("c2")
    If iRow < 0 Then iRow = Range("c2") * -1
    Set oRng = wdDoc.Tables(1).Cell(iRow, 5).Range
    oRng.End = oRng.End - 1
    oRng.Text = Range("E" & i)
    'or
    'oRng.Paste
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote