View Single Post
 
Old 06-19-2018, 06:15 AM
klutch klutch is offline Windows 7 32bit Office 2016 for Mac
Advanced Beginner
 
Join Date: Jun 2018
Posts: 31
klutch is on a distinguished road
Default

Quote:
Originally Posted by gmayor View Post
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
I am getting an ActiveX can't create object error on this line
Code:
Set wdApp = GetObject(, "Word.Application")
Reply With Quote