View Single Post
 
Old 06-15-2018, 02:15 PM
d4okeefe d4okeefe is offline Windows 10 Office 2016
Advanced Beginner
 
Join Date: Apr 2013
Posts: 77
d4okeefe is on a distinguished road
Default

There's a lot going on in your original code, so I'll do my best. I can't recreate it perfectly without the original files. Here goes:

Code:
Sub test_ok()
    Dim myfile, wdApp As New Word.Application, wdDoc As Word.Document
    myfile = Application.GetOpenFilename(, , "Browse for Document")
    Dim i As Integer
    i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)
    Range("E" & i).Select
    Dim excel_selection As String
    excel_selection = Selection.Text
     
    wdApp.Visible = True
    Set wdDoc = wdApp.Documents.Open(myfile)
    'select the word range you want to paste into
    
    Dim bk As Bookmark, bk_new As Bookmark
    Dim new_range As Range
    Dim bk_name As String
    If Range("c2") = 22 Then
        Set bk = wdDoc.Bookmarks("d22")
        bk_name = bk.Name
        Set new_range = wdDoc.Range(bk.Start, bk.End)
    End If
    If Range("c2") = 5 Then
        Set bk = wdDoc.Bookmarks("d5")
        bk_name = bk.Name
        Set new_range = wdDoc.Range(bk.Start, bk.End)
    End If
    If Range("c2") = -20 Then
        Set bk = wdDoc.Bookmarks("d20")
        bk_name = bk.Name
        Set new_range = wdDoc.Range(bk.Start, bk.End)
    End If
    
    new_range.InsertAfter (vbCrLf & excel_selection)
    bk.Delete
    Set bk_new = wdDoc.Bookmarks.Add(bk_name, new_range)
End Sub
Reply With Quote