PDA

View Full Version : Solved: Moving a Shape/InlineShape between sections in Word



ukemike
09-15-2005, 01:30 PM
Hey guys,

This is turning out to be a big pain in the ass, but i have a simple question.
Lets say i got a shape object in section 1 of my document. I need to select it and move it to Section 2 of my word document.

what would be the VBA code for this?


Thank you
Mike

TonyJollans
09-15-2005, 02:47 PM
Shapes belong to either the Shapes or the InlineShapes collections (of the Document) depending on how they are formatted. You can cut using, for example,

ActiveDocument.InlineShapes.Range.Cut

and paste using, say,

ActiveDocument.Range(100,101).Paste

Not sure exactly what you want but hopefully you'll be able to adapt to fit. If not, come back.

fumei
09-16-2005, 12:05 AM
The Shapes/InlineShapes collection can be very odd and frustrating to use sometimes. It really depends on what kind of "shape object" you are trying to get at.

ukemike
09-16-2005, 05:16 AM
Here is my code:




Shapes = wordFile.Selection.Range.ShapeRange.Item(ref rangeObject);
PicProps myPicture = new PicProps();

myPicture.Height = Shapes.Height;

myPicture.Width = Shapes.Width;

myPicture.Top = Shapes.Top;

myPicture.Left = Shapes.Left;

myPicture.RLeft = Word.WdRelativeHorizontalPosition.wdRelativeHorizontalPositionPage;

myPicture.RTop = Word.WdRelativeVerticalPosition.wdRelativeVerticalPositionPage;



wordFile.Selection.GoTo(ref section, ref first, ref secNumber, ref missing);

Word.Shape OShape = wordFile.ActiveDocument.Shapes.AddPicture(imageFilename, ref linktofile, ref savewithdocument, ref missing, ref missing, ref missing, ref missing, ref missing);



OShape.Name = "myShape";

object shapeName = "myShape";

wordFile.ActiveDocument.Shapes.Item(ref shapeName).Select(ref missing);



object moveSection = 8;

object wdMove = 0;

object Test1 = 2;

wordFile.Selection.Move(ref moveSection, ref secNumber);

OShape.Top = Convert.ToSingle(400);

OShape.LockAspectRatio = Office.MsoTriState.msoFalse;

OShape.Width = Convert.ToSingle(myPicture.Width);

OShape.Height = Convert.ToSingle(myPicture.Height);

OShape.Left = Convert.ToSingle(myPicture.Left);

OShape.RelativeHorizontalPosition = myPicture.RLeft;

OShape.RelativeVerticalPosition = myPicture.RTop;

OShape.WrapFormat.Type = Word.WdWrapType.wdWrapNone;



It seems whenever you do:
Word.Shape OShape = wordFile.ActiveDocument.Shapes.AddPicture(imageFilename, ref linktofile, ref savewithdocument, ref missing, ref missing, ref missing, ref missing, ref missing);

this will insert this image always on the first page of the document (in my document, section 1). I need to move this OShape onto Section 2 of my document. I have tried selecting the shape:

OShape.Name = "myShape";

object shapeName = "myShape";

wordFile.ActiveDocument.Shapes.Item(ref shapeName).Select(ref missing);



and using MoveStart(), MoveDown(), Move(). None of this works. I would prefer not to use Cut/Paste. Anyone have any other ideas??


Thank you
Mike

TonyJollans
09-16-2005, 07:29 AM
I can't fully understand your code but you position a shape by giving it an anchor (a Word Range) and a position relative to that anchor - the fourth to eighth parameters to the AddPicture are Left, Top, Width, Height, Anchor. You have all these coded as "ref missing". If you use the start of section 2, say, as your anchor (or the Selection following your GoTo) and whatever it is that you want as the offsets you might have more luck getting the picture where you want it.

ukemike
09-16-2005, 07:44 AM
Tony,

fumei
09-16-2005, 07:58 AM
Your code is difficult to read.

You mention both moving the Shape, but also it seems creating a Shape.

1. you still have not specified what the shape is

2. shapes have a Range. This is the Anchor Tony mentioned. If you do not specify the Range, it goes to the default which is ALWAYS the top of the first page.

3. when you move the selection by code...it moves the selection. It does not bring along what was previously selected. It moves the selection.

4. why are you reluctant to use cut and paste? It is actually the best solution. It is precisely what you seem to want to do....CUT the object out of where it was, and PASTE it at the new location.

Selecting it and moving the Selectiion will not work. Again, that just move the Selection. And, again, if you make a Shape (NOT an InlineShape using the default Range parameter it will make at the top of the page.

sandam
09-16-2005, 08:33 AM
Renamed thread to give a better description of the problem.

ukemike
09-16-2005, 10:06 AM
fumei:

1.The shape is just a rectangle shape in word. I clicked on the rectangle from the Drawing toolbox and drew a shape.

2. OK
3. OK
4. Im relucatant to use Cut/Paste is because if the user is doing something else with the clipboard while this report is being generated it will get screwed up.

So in the

Word.Shape OShape = wordFile.ActiveDocument.Shapes.AddPicture(imageFilename, ref linktofile, ref savewithdocument, ref missing, ref missing, ref missing, ref missing, ref missing);

since the last parameter is an anchor, if i specify an anchor in the second section this picture will be imported into the first page of the second sections?


Thanks
Mike

ukemike
09-16-2005, 10:14 AM
As for the code being messy: this code will has to be compatible with word 2000 and word 2003. That code is the only one i found (actually i think someone from here wrote it for me (the VBA version)) that will work on all word versions. What I am trying to do can be done much easier word 2003 but i have to make it word 2000 compatible.


Mike

ukemike
09-16-2005, 10:25 AM
Hmmm for some reason i cant figure out how to this. Can someone post the VBA code for going to section x of a document and getting the range of the first Shape object and using

ActiveDocument.Shapes.AddPicture()

go have an anchor parameter point to this range.

This is what TonyJollans was talking about earlier correct?

Thank you very much.
Mike.

TonyJollans
09-16-2005, 10:48 AM
Hi Mike,

This should put a picture at the start of the second section
ActiveDocument.Shapes.AddPicture "Picture Path and Name", False, True, , , , , ActiveDocument.Sections(2).Range

ukemike
09-16-2005, 11:08 AM
Perfect. That is exactly what I needed. My 3 week problem is solved. Thank you Tony.

Mike

TonyJollans
09-16-2005, 11:37 AM
My pleasure!

MOS MASTER
09-16-2005, 04:51 PM
Hi Mike,

This should put a picture at the start of the second section
ActiveDocument.Shapes.AddPicture "Picture Path and Name", False, True, , , , , ActiveDocument.Sections(2).Range

Hahaha this is a nice Story. :yes

It started to look for a method of moving a shape in a document to a new location and it turned out it only needed to be inserted in to a perticular section of the document.

Very good of you to get there Tony! :hi: