View Single Post
 
Old 06-15-2018, 06:09 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 4,158
Guessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant futureGuessed has a brilliant future
Default

Is the code meant to add a row under the row where the bookmark exists? If not, how do you know there is a cell there and it is empty?
Assuming there is always a cell below the bookmark, I would do the code like this. Read through this carefully because it is considerably different from what you were previously doing and includes some of the testing that is required to deal with the errors that you might encounter
Code:
Sub CopyToWord()
  Dim myfile As String, wdApp As New Word.Application, wdDoc As Word.Document
  Dim i As Integer, dblC22 As Double, wdRng As Word.Range, strAve As String
  
  i = Application.Match("Avg", Sheet1.Range("A1:A20"), 0)
  strAve = Range("E" & i).Value
  dblC22 = Range("c2")
  
  myfile = Application.GetOpenFilename(, , "Browse for Document")
  wdApp.Visible = True
  Set wdDoc = wdApp.Documents.Open(myfile)
  
  Select Case dblC22
    Case 5, 22, -20
      If wdDoc.Bookmarks.Exists("D" & Abs(dblC22)) Then
        Set wdRng = wdDoc.Bookmarks.Exists("D" & Abs(dblC22))
        If wdRng.Information(wdWithInTable) Then
          wdRng.MoveEnd Unit:=wdCell, Count:=4
          wdRng.InsertAfter strAve
        Else
          wdRng.InsertAfter strAve
        End If
      End If
  End Select
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote