View Single Post
 
Old 06-18-2018, 12:18 PM
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 Guessed View Post
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
I am getting a mismatch error on line
Code:
Set wdRng = wdDoc.Bookmarks.Exists("D" & Abs(dblC22))
Reply With Quote