Error 5941

TPFKAS

Board Regular
Joined
Mar 1, 2010
Messages
62
Hi all,

I use the following piece of code to copy a bunch of named tables in my Excel-file to a Word document.
The tables are named "Question1" etc which are located on a sheet called "Tables". myDoc is an opened Word document

Code:
For i = 1 To NumberOfQuestions
    iParCount = myDoc.Paragraphs.Count
    TableName = "Question" & i
    Set tbl = ThisWorkbook.Worksheets("Tables").ListObjects(TableName).Range
    tbl.Copy
[COLOR=#ff0000]    myDoc.Paragraphs(iParCount).Range.PasteExcelTable _[/COLOR]
[COLOR=#ff0000]        LinkedToExcel:=False, _[/COLOR]
[COLOR=#ff0000]        WordFormatting:=False, _[/COLOR]
[COLOR=#ff0000]        RTF:=True[/COLOR]
    myDoc.Paragraphs.Add
Next i

From time to time the script stops and returns an Error 5941 with the statement "Method Item of object Paragraph failed" (Could be a bit different because I use a Dutch version and this is the litteral translation).
The script stops at the instruction in red above.
The behaviour is random. Sometimes it stops at i =10 or so, sometimes at i = 4 and sometimes it completes all the way until NumberOfQuestions. When it fails and I just retry it may work or stop at another i.

Any ideas?
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).

Forum statistics

Threads
1,223,214
Messages
6,170,771
Members
452,353
Latest member
strainu

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top