Share via

Run-time error '4605': This method or property is not available because the Clipboard is empty or not valid.

Anonymous
2017-06-23T09:59:03+00:00

I have a macro that I run from Excel that loops through 53 files. The loop opens a Word template file (.dotx), copies content from two tables in an Excel file and saves them in a new Word document, and closes the template.

Sometimes the macro will loop successfully through all 53 files, but most times it crashes after looping successfully through 5, 10 or 15 files.

The error is:

Run-time error '4605': This method or property is not available because the Clipboard is empty or not valid.

It occurs during the paste.

I have read that deleting the Normal.dotm template can help - I have done this numerous times. It seemed to help the first time I tried it, but not any more - very frustrating!

Word 2010 and Excel 2010.

I suspect this is a problem with RAM, but am not sure - any advice very gratefully received!

Sub GetCPdata_PasteInWord_EN()

Dim wbk As Workbook

Dim Filename As String

Dim Path As String

Dim wdApp As Word.Application

Dim wdDoc As Word.Document

Dim CntName As Range

Dim CNameRange As Word.Range

Dim tbl As Excel.Range

Dim WordTable As Word.Table

Dim lRow, x As Integer

Dim WordDocName As String

Dim DirName As String

Path = "N:\DCE\IRP\2. Influenza EURO\Country profiles\2017\Production\EN_RU"

Filename = Dir(Path & "*.xlsx")

Application.DisplayAlerts = False

Application.ScreenUpdating = False

 Do While Filename <> ""

    Set wdApp = CreateObject("Word.Application")

    wdApp.Visible = True

    Set wdDoc = wdApp.Documents.Open("N:\DCE\IRP\2. Influenza EURO\Country profiles\2017\CountryProfile_EN_landscape.dotx")

    Set wbk = Workbooks.Open(Path & Filename)

    Sheets("User Form").Visible = True

    Set CntName = wbk.Sheets("User Form").Range("B2")

    Set CNameRange = wdDoc.Goto(What:=wdGoToBookmark, Name:="CountryName")

    CNameRange.Text = CntName

    Sheets("Header").Visible = True

    Set tbl = wbk.Worksheets("Header").Range("A1:D1")

    tbl.Copy

    wdDoc.Bookmarks("Header").Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

    Sheets("Table_EN").Visible = True

    Set tbl = wbk.Worksheets("Table_EN").Range("A3:G13") '<--| adjust G cell reference to match final cell

    tbl.Copy

    wdDoc.Bookmarks("CPtable").Range.PasteExcelTable LinkedToExcel:=False, WordFormatting:=False, RTF:=False

    WordDocName = wbk.Sheets("User Form").Range("B2").Value & "_" & wbk.Sheets("User Form").Range("B3").Value & "_EN"

    With wdDoc

        wdDoc.Activate

        wdDoc.SaveAs Filename:="N:\DCE\IRP\2. Influenza EURO\Country profiles\2017\Production\BatchWord_EN" & WordDocName & ".docx", FileFormat:=wdFormatXMLDocument

        wdDoc.Close savechanges:=False

    End With

    wbk.Close savechanges:=False

    wdApp.Quit

    Filename = Dir

 Loop

Application.DisplayAlerts = True

Application.ScreenUpdating = True

End Sub

Microsoft 365 and Office | Word | For home | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

Answer accepted by question author

Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
2017-06-24T09:42:28+00:00

The 4605 error occurs because of a timing problem and can be overcome by using an Or Error - DoEvents -Resume loop

Was this answer helpful?

7 people found this answer helpful.
0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Anonymous
    2017-06-24T11:15:27+00:00

    Thank you, Doug - truly grateful for your expertise and help! And the SOLUTION

    – it now runs like a charm! Your expertise is so way above mine - I'd never have found my way to that!

    Charles

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  2. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2017-06-23T11:43:49+00:00

    If you want to zip up all of the files and upload them to the following folder of my OneDrive

    https://1drv.ms/f/s!AmuIXmFDy-1a8nP3RpgnYHp-5zXK

    and send me an email to the address shown in my signature and I will take a look at the issue.

    Was this answer helpful?

    0 comments No comments
  3. Anonymous
    2017-06-23T10:41:50+00:00

    Thank you for your super quick reply, Doug!

    I made the change and on the first run-through it crashed while creating the 6th Word file.

    Running the macro again, I now get a different error message:

    Occurs when creating the 2nd file at 

        Set CNameRange = wdDoc.Goto(What:=wdGoToBookmark, Name:="CountryName")

    Charles

    Was this answer helpful?

    0 comments No comments
  4. Doug Robbins - MVP - Office Apps and Services 323K Reputation points MVP Volunteer Moderator
    2017-06-23T10:13:18+00:00

    I believe that the issue is that your code is starting Word and quiting it for each file in the N:\DCE\IRP\2. Influenza EURO\Country profiles\2017\Production\EN_RU

    folder, without setting the wdApp variable to nothing

    You could move the 

       Set wdApp = CreateObject("Word.Application")

    and the wdApp.Quit

    outside of the Do While ... Loop

    Was this answer helpful?

    0 comments No comments