The code shown below was working until today, Now I get the error:
Code:
Method 'Range' of object '_Worksheet' failed
Or, if I step into the code line by line I get this:
Code:
Application-defined or object-defined error
I am on the sheet risks at the time (I have read this can cause issue) I'm unsure what's causing it.
Note: previously this was test data, now its external data from access. I cant find a reliable way to insert rows into a table from access into word without using excel as the middle man. If I do it straight from access I get formatting issues and unwanted column headers.
Code:
Sub ExcelRangeToWord()
Dim objWord As Object
Dim ws As Worksheet
Dim lngLastRow As Long
On Error GoTo Errorcatch
lngLastRow = Sheets("RISKS").Range("A65535").End(xlUp).Row
Debug.Print lngLastRow
Set ws = ThisWorkbook.Sheets("RISKS")
Set objWord = CreateObject("Word.Application")
objWord.Visible = True
'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False
ws.Range("A:2" & "H:" & lngLastRow).Copy
'open the word doc
objWord.Documents.Open "C:\Users\ah\Desktop\RAMS AUTOMATION\Import table test.docx" 'change as required
'pastes the value of cell at the bookmark
With objWord.ActiveDocument
.Bookmarks("RISKS").Range.Paste
End With
'Optimize Code
Set objWord = Nothing
Application.ScreenUpdating = True
Application.EnableEvents = True
'Clear The Clipboard
Application.CutCopyMode = False
Exit Sub
Errorcatch:
MsgBox Err.Description
End Sub