pasting a chart into word - runtime error 5941 member of collection does not exist

DIMWIT

Board Regular
Joined
Jul 13, 2006
Messages
52
Hey everyone,

I have recently taken over a piece of work which using VBA to open a chart in excel and pastes it into a word document and re sizes it. This code used to work but is now coming up with the run time error code 5941.

The bit where it mentions 'inline shapes' is where it falls over

The person who wrote the code is no longer here :( and no one else in the office - including myself - knows how to fix it. here is the code:
Code:
Public Sub AddIdaciCharts()
Dim IDACIFile As Excel.Workbook

'opens the file based on the school number workbook range
Set IDACIFile = Workbooks.Open(ThisWorkbook.Path & "\IDACI Files\335" & Range("SchoolDfE") & " idaci decile bands 2016.xlsx")

'copies the chart from the School vs LA tab and pastes into the bookmark in the word doc
IDACIFile.Sheets("School vs LA").ChartArea.Copy

wrdApp.Selection.Goto what:=wdGoToBookmark, Name:="IDACI1"
wrdApp.Selection.PasteSpecial DataType:=wdPasteBitmap, Placement:=wdInLine
wrdApp.Selection.MoveLeft unit:=wdCharacter, Count:=1, Extend:=wdExtend
With wrdApp.Selection.InlineShapes(1)
    .Height = 295
    .Width = 480
    .Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
End With

We are truly stuck and need some assistance. I have checked and the bookmarks exist and the excel and charts are fine and in the right location.

Does anyone know what this might be?

Many thanks as always
 
Last edited by a moderator:
Re: resize chart once pasted into word

That line refers to the first inline shape within the document, not necessarily to the one just pasted.
No, it does not! It refers to the first shape in the designated range. Please read the code. In any event, even if it did refer to the first inlineshape in the document, no error would be produced unless the object referred to was not scalable (see my previous remarks about this).

DIMWIT: The code works fine for me (I tested before posting). Try replacing:
Code:
    If .Bookmarks.Exists(wrdBkMk) Then
      Set wrdRng = .Bookmarks(wrdBkMk).Range
      'Delete any inlineshapes in our bookmark's range
      With wrdRng
        While .InlineShapes.Count > 0
          .InlineShapes(1).Delete
        Wend
        
        'Insert and size our inlineshape
        .PasteSpecial Link:=False, DataType:=wdPasteBitmap, _
          Placement:=wdInLine, DisplayAsIcon:=False
        .Start = .Start - 1
        .InlineShapes(1).ScaleHeight = 65
      End With
      
      'Reapply the bookmark
      .Bookmarks.Add Name:=wrdBkMk, Range:=wrdRng
      
      'Format the paragraph
      .Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
    End If
with:
Code:
    If .Bookmarks.Exists(wrdBkMk) Then
      Set wrdRng = .Bookmarks(wrdBkMk).Range
      'Delete any inlineshapes in our bookmark's range
      With wrdRng
        While .InlineShapes.Count > 0
          .InlineShapes(1).Delete
        Wend
        
        'Insert and size our inlineshape
        .PasteSpecial Link:=False, DataType:=wdPasteBitmap, _
          Placement:=wdInLine, DisplayAsIcon:=False
        .Paragraphs(1).Range.InlineShapes(1).ScaleHeight = 65
        
        'Format the paragraph
        .Paragraphs(1).Range.ParagraphFormat.Alignment = wdAlignParagraphCenter
        
        'Reapply the bookmark
        .Bookmarks.Add Name:=wrdBkMk, Range:=.Paragraphs(1).Range.InlineShapes(1).Range
      End With
    End If
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
Re: resize chart once pasted into word

Macropod said:
No, it does not! It refers to the first shape in the designated range.

Actually, I thought the OP was referring to my code...

Code:
With wrdApp.ActiveDocument
    .InlineShapes(.InlineShapes.Count).ScaleHeight = 65
End With

But I see now that he was actually referring to your code. Sorry for any confusion.
 
Upvote 0
Re: resize chart once pasted into word

Conversely, the problem with your code is that it assumes the InlineShape to be resized is the last in the document. That may or may not be the inserted InlineShape, since we have no idea what else the document contains. The index # of an InlineShape isn't related to the order of insertion but to its relative location in the document.
 
Upvote 0
Re: resize chart once pasted into word

Thanks very much for all your help guys.

Macropod - you are a genius!. Thank you so much, the code is working fine and adjusting the size of the charts in the document.

You have saved me again

Really appreciate the help :)
 
Upvote 0
Re: resize chart once pasted into word

Conversely, the problem with your code is that it assumes the InlineShape to be resized is the last in the document. That may or may not be the inserted InlineShape, since we have no idea what else the document contains. The index # of an InlineShape isn't related to the order of insertion but to its relative location in the document.


Oh wow... I didn't realize that an InlineShape is indexed according to its relative location in the document. Now that I think of it, I guess it stands to reason since the shape is "in line" with text.

Thanks for pointing that out!

Cheers!
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,992
Messages
6,122,631
Members
449,095
Latest member
bsb1122

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