Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228

    Copy from excel into word table.

    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

  2. #2
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    Okay, I have broke this code when trying to implement the lnglastrow variable. ( I forgot I even attempted it.)

    If I manually define the range it does work..

    This is where the error occurs ("A:2" & "H:" & lngLastRow)

    If I debug.print lngLastRow It does show the correct value.

    Any suggestions?

  3. #3
    Join Date
    Jun 2015
    Location
    Wales. Land of the sheep.
    Posts
    1,228
    sorted: just confused myself with the colons after amending it a few days ago and not finishing what I was doing.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Rename word document during copy/paste
    By Homegrownandy in forum Programming
    Replies: 3
    Last Post: 07-17-2018, 08:15 AM
  2. Replies: 8
    Last Post: 12-31-2015, 12:05 PM
  3. Replies: 4
    Last Post: 08-24-2015, 12:57 PM
  4. Copy Accessdata into Excel tabs
    By baba in forum Access
    Replies: 2
    Last Post: 06-20-2015, 09:49 PM
  5. Copy query design view into excel or word
    By MrAlex in forum Access
    Replies: 4
    Last Post: 01-06-2015, 09:13 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums