PDA

View Full Version : Solved: Replace Object



blake.arnold
06-01-2006, 08:38 AM
How do I replace a shape with a copy of another shape. This is the code that I have so far.

With ActiveDocument.Shapes(i)
.Select
Selection.InlineShapes(1).Select
Selection.Copy
.Select
'Replace shape with inlineshape
End With

So I have a copy of the inline shape in the clipboard. And I have the shape that I want to replace selected. Now how do I go about replacing it?

Thanks For Your Help in Advance.:banghead:

Killian
06-01-2006, 11:17 AM
If you are working with inline shapes, the paste method will replace the selection.Selection.Paste
If the shape you are replacing is not inline, you will need to set up variables the hold it's position, delete it, then reposition the new shape

blake.arnold
06-01-2006, 11:27 AM
You cannot paste over a textbox apparently.

The Inlineshape is inside of a textbox and that is where the difficulties are comming in. I cannot seem to overwrite the textbox.

Killian
06-01-2006, 12:33 PM
is it a textbox? I don't believe you can put a shape inside a text box, tho it could be a frame...
It's more likely to be a "drawing canvas". If that's the case, they are a little bit problematic because once inserted, it is a "shape" (rather than a distinct "Canvas" object with it's own properties & methods) but it's type is msoCanvas so it's behaviour is a it different to normal shapes - just using "Selection.Paste" will add the new shape into the canvas.
However, the shaperange property of the selection (with the shape in the canvas selected) will give you the object. If it's also inline, you can delete it, the selection will go to it's previous position and then you can pasteSelection.ShapeRange.Delete
Selection.Paste

blake.arnold
06-01-2006, 01:01 PM
Based off of what I can see and the error messages that I am getting it is in fact a text box. The code that you sugested did not work either. What it does is deletes the text box and then pastes the image at the top of the page. My biggest goal here is to replace the text box with the inline image while retaining the position of the image.

If you would like an example of what I am working with I would be more than happy to provide it.

fumei
06-01-2006, 01:44 PM
Post a doc. This is a very tricky aspect of Word. You may, or may not be able to do this - remove the textbox and keep the inline shape in position.

Lordy I hate those textboxes. Yes, you can put shapes in those damn textboxes....

blake.arnold
06-01-2006, 02:03 PM
Here it all is. This is the exact code that I have been trying to use.

Sub trial()

Dim ammount
Dim vartype
ammount = ActiveDocument.Shapes.Count

For i = 1 To ammount
With ActiveDocument.Shapes(i)
.Select
vartype = ActiveDocument.Shapes(i).Type
If .Type = 1 Then
.Select
Selection.InlineShapes(1).Select
Selection.Copy
.Select
Selection.Delete
Selection.Paste
End If
End With

Next i
End Sub


My document is acctually several hundred pages long but this is a good piece.

fumei
06-02-2006, 03:47 AM
Aaaarrrrrggggggghhhhhhhhhhhhhhhhhhh!

OK.



1. Obviously you have your code in Normal.dot, as this file has no code in it...OK, so I come back and copy and paste from my web browser. Hmmmm.

2. I run the code. I wonder about the number of iterations of i (NOT declared by the way....). Hmmmm. What is with the opening "table"??????? Why on earth is it constructed????? There is no need for this at all, as far as I can see. Why is this not...a table???

3. You are Selecting every one of those lines making up your "table". Total waste pf code and time.With ActiveDocument.Shapes(i)
' selects EVERY shape
.Select
vartype = ActiveDocument.Shapes(i).Type
If .Type = 1 Then Better to use:
If ActiveDocument.Shapes(i).Type = 1 Then The other shapes are never selected. Also, no need for the vartype variable either...

In essense you are selecting twice. The With statement should be for the Selection, not the shape. For i = 1 To ammount
If ActiveDocument.Shapes(i).Type = 1 Then
ActiveDocumentShapes(i).Select
Selection.InlineShape(1).Select
With Selection
.Copy
.Delete
.Paste
End With
End If
NextHowever, I am still not sure exactly what you are trying to do.

blake.arnold
06-02-2006, 04:18 AM
Well on the second page there are two images. These are as you can see contained within textboxes. I want to put the inline images in place of the textbox.

Does that clarify?

The reason that I am doing this is becuase I have a program that I have to import this doc into. And if it sees textboxes it just tries to get the text not the image. But this in not my program so I cant exactly change its code.

The problem with the code that you gave me is that it copys the inline image and then deletes it and then pastes it right back in the same spot.

Killian
06-02-2006, 04:46 AM
Aaaarrrrrggggggghhhhhhhhhhhhhhhhhhh!Yeah...

Well I see the texboxes aren't inlineshapes, so the pictures inside (which are) will have to be positioned absolutely using the text box position.
On a positive note, it seems to be do-able (famous last words...)
Sorry, I'm a bit pressed for time, but here's some psuedo-code which should work in principle (though I'm not sure about getting the pasted shape from an inlineshape to a shape:

For Each shp in ActiveDocument.Shapes
(this loop will exclude the pictures, which are in the Inlineshapes collection)
if shp is a textbox then
if shp.range.inlineshapes.count=1 then
set oldshp = shp.range.inlineshapes(1)
oldshp.copy
paste oldshp outside textbox as newshp
convert newshp from inline to floating
position newshp using Top & Left of shp
shp.delete

etc...

Hope that's some help. I'll try to follow up later...

fumei
06-02-2006, 05:19 AM
paste oldshp outside textbox as newshp
convert newshp from inline to floating
position newshp using Top & Left of shpyeah....it is the darn positioning that is driving me nuts.

blake.arnold
06-02-2006, 05:35 AM
That is what has been making me do this. :banghead:

Anyways I thought I would put it to the experts and see what you all had to say.

I do apprecaite you guys helping me.:beerchug:

blake.arnold
06-05-2006, 04:43 AM
Has anyone been able to figure anything out?

Killian
06-05-2006, 07:40 AM
OK, well I think I've finally got something working with it (and believe me, noboby's more surprised than I am!). At least it seems to be working with the sample.
I'll wait for you to unleash it on a larger document before I start getting too excited...Dim shp As Shape
Dim newshp As Shape

For Each shp In ActiveDocument.Shapes
If shp.Type = 1 Then
If shp.TextFrame.TextRange.InlineShapes.Count = 1 Then
'move the cursor to the shape anchor
'(this is to make sure when we position the shape
'we're on the right page - hopefully)
Selection.Move wdCharacter, (shp.Anchor.Start - Selection.Start)
'set the original shape's position properties
With shp
.LockAnchor = False
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
End With
'copy/paste than expand the selection to include
'the pasted shape - it will be inline
shp.TextFrame.TextRange.InlineShapes(1).Range.Copy
Selection.Paste
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
Set newshp = Selection.InlineShapes(1).ConvertToShape
'set the new shape's position properties
With newshp
.LockAnchor = False
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.Left = shp.Left
.Top = shp.Top
End With
shp.Delete
End If
End If
Next shp

blake.arnold
06-05-2006, 09:46 AM
WOW!!!!!

That worked great. I am glad to see that someone could help. It would of took me forever to figure out the page anchors.


Thanks a-million.:ipray:

Killian
06-05-2006, 10:47 AM
Well that's good news... I'm never very confident when a solution involves shuffling the cursor around to work with the selection object but sometimes it seems the only way to get Word to cooperate. And if it does the job...