PDA

View Full Version : Insert InlineShapes stored in an array into a document



gmaxey
04-21-2013, 01:21 PM
I need to collect the inlineshapes from one or more documents and store them in an array. Then I need to insert (or add) one or more of the stored inlineshapes into a new document. I think I've worked out how to store the ILS into the array, but not how to put them in a new document. Thanks:

Sub SCRATCHMACRO()
Dim oDoc As Word.Document
Dim arrILS(0) As Variant, oILS As InlineShape
'Loading an InlineShape from one document into an array.
Set arrILS(0) = ActiveDocument.InlineShapes(1)
'Trying to insert that array stored ILS into a new document.
Set oDoc = Documents.Add
'Add the ILS
Set oILS = ActiveDocument.InlineShapes.New(Selection.Range)
'How to insert the array stored ILS into this new document?
'oILS = arrILS(0) ??????????????????
'Thanks.
End Sub

fumei
04-21-2013, 01:39 PM
Could you post the code for creating the array.

gmaxey
04-21-2013, 02:45 PM
Gerry,

It is already posted.

Dim arrILS(0) as Variant

fumei
04-21-2013, 06:18 PM
Oh, just the one. OK. hmmmm.

gmaxey
04-21-2013, 06:37 PM
Gerry,

Yes just the one in this simple example. In practice I'll want to store multiple inlineshapes in an array and then insert one or more of those inlineshapes into another document.

fumei
04-21-2013, 06:46 PM
Well...curses. I have never tried this. What the heck are we missing?

fumei
04-21-2013, 06:57 PM
Darn darn darn...or as Big Papi put it...%$@#*&^ InlineShapes.

Still trying. Do let me know if you get it to fly.

fumei
04-21-2013, 07:45 PM
Well Greg..good one. I appear to be stumped for now. I wonder if it is even possible to bypass .New, to insert an InlineShape.

gmaxey
04-21-2013, 08:04 PM
Gerry,

I usually don't post until I am stumped and then sometimes after letting it goes, a solution pops up like a flash and I can answer my own quesiton. Not this time though :-(

Using .New was the only way I could see to add and inlineshape at all. If there is another way, it is lost to me.

Thanks.

Dave
04-24-2013, 12:15 AM
Bump. Seems like a great idea storing inline shapes in an array for later use. I tried inserting places for the array contents (ie. a table) before inserting the contents... messing around with ranges and the New and several other things that didn't work. Just to be clear, I inserted a picture in a document and then tried it... a picture is stored as an inline shape is it not? Anyways, could you add how to trial code for us less literate. Thanks for the thread. Dave

gmaxey
04-24-2013, 10:16 AM
Eureka!!

Well almost. With a little push from Leigh Webber in another formum, I've learned that instead of "inserting" the array stored ILS into a new document, I can copy their stored .Range property to the clipboard then paste into a new document:

Sub ScratchMacro()
Dim lngIndex As Long
Dim oILS As InlineShape
Dim arrILS() As Variant
Dim oDoc As Word.Document
lngIndex = 0
For Each oILS In ActiveDocument.InlineShapes
ReDim Preserve arrILS(lngIndex)
Set arrILS(lngIndex) = oILS
lngIndex = lngIndex + 1
Next oILS
For lngIndex = LBound(arrILS) To UBound(arrILS)
Set oDoc = Documents.Add
arrILS(lngIndex).Range.Copy
oDoc.Range.Paste
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

Note: In actual practice I will include code to avoid the "You have placed a large amount of ...." message when Word exits.

Dave, yes a picture inserted in a document is usually an inlineshape. What do you mean by "add how to trial code?"

Thanks for all the interest and I have considered this one a real puzzler.

Dave
04-24-2013, 01:29 PM
Wow that is some cool stuff! Thanks for the code. "Trial the code" in the sense that to test.. copy and paste a picture in one document, run the code, and a new document is produced with the picture in it (from the array). Just wondering, with all the potential copy and pasting going on, will the clipboard fill up and cause problems with the code execution? (You did mention addressing the warning message on exit.) Again, thanks. Dave

gmaxey
04-24-2013, 03:29 PM
Dave, Gerry;

Thanks to another shove received in another formum by old friend Peter Jamieson this is one better. There is no need to copy and paste anything:

Sub ScratchMacro()
Dim lngIndex As Long
Dim oILS As InlineShape
Dim arrILS() As Variant
Dim oDoc As Word.Document
Dim strXML As String
lngIndex = 0
For Each oILS In ActiveDocument.InlineShapes
ReDim Preserve arrILS(lngIndex)
Set arrILS(lngIndex) = oILS
lngIndex = lngIndex + 1
Next oILS
For lngIndex = LBound(arrILS) To UBound(arrILS)
Set oDoc = Documents.Add
strXML = arrILS(lngIndex).Range.XML
oDoc.Range.InsertXML strXML
Next lngIndex
lbl_Exit:
Exit Sub
End Sub

fumei
04-24-2013, 04:38 PM
Except if course for those of us who can not use InsertXLM.

I have to say I am embarassed to have missed that, because in my testing I could get properties OUT of the InlineShapes in the array.

gmaxey
05-15-2013, 09:31 PM
What does your post have to do with this thread and who is "your" and what is his or her querry? Even a casual perusal of this thread should tell you that you that there is no longer a question here that really needs answering.

fumei
05-16-2013, 05:17 PM
What post are you refering to?

gmaxey
05-16-2013, 06:26 PM
Gerry, It's gone. Could have been the spam that Paul warned me about.

fumei
05-16-2013, 07:57 PM
Spam? What spam Paul warned about? Gee I miss everything. But thanks for answering, your post seemed was such a non sequitor that I had to wonder.