Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copying and Moving Shapes in MS Word VBA

Status
Not open for further replies.

Orgdoctor

Technical User
Jan 8, 2003
13
US
I'm trying to copy two sets of 3 lines that form arrows from one part of an MS Word document to another using a VBA macro. As the code below shows, I have declared an array (Arrows) as "shape". In the code I define a range that subsumes the six shapes. Then I try to assign each of the shapes to the array elements. Three problems arise. First, the count reveals that it is only finding 5 of the six line shapes in the range. Second, as soon as I try to assign a shape item to an array element (e.g., Arrows(1)), I get a "Run-time error 91, Object variable or With block variable not set." Finally, how do I insert the copied shapes (once you help me figure out how to copy them) into the selection area? Can I use "Selection.text = Arrows(1) + Arrows(2) + ..."? Here's my code so far:

Dim Arrows(6) As Shape
Dim ArrowRange As Range

RHIndex = 8
Set ArrowRange = ActiveDocument.Range(ActiveDocument.Tables(RHIndex).Range.Start, ActiveDocument.Tables
(RHIndex + 1).Range.Start)
ShapeCount = ArrowRange.ShapeRange.Count
Arrows(1) = ArrowRange.ShapeRange.Item(1)
Arrows(2) = ArrowRange.ShapeRange.Item(2)
Arrows(3) = ArrowRange.ShapeRange.Item(3)
Arrows(4) = ArrowRange.ShapeRange.Item(4)
Arrows(5) = ArrowRange.ShapeRange.Item(5)
Arrows(6) = ArrowRange.ShapeRange.Item(6)

ActiveDocument.Tables(TablesIndex).Select
Selection.EndOf wdTable
Selection.MoveDown wdLine, 1, wdMove
Selection.Text = Arrows(1) + Arrows(2)
Selection.MoveDown wdLine, 1, wdMove
Selection.Text = Arrows(2) + Arrows(4)
Selection.MoveDown wdLine, 1, wdMove
Selection.Text = Arrows(3) + Arrows(6)
Selection.MoveDown wdLine, 1, wdMove
.
.
.
I would greatly appreciate anyone's help with this.
 
How are the arrows put in originally?

Also, there seems to be funny things with the tables.
Code:
Set ArrowRange = ActiveDocument.Range(ActiveDocument.Tables(RHIndex).Range.Start, ActiveDocument.Tables
     (RHIndex + 1).Range.Start)
You are making a range from the start of a table (#8 it seems) to the start of the next table.

Is there text between these? Why are you doing it this way?

Then you select a different table, go to the end of it, then move the selection down lines. Seems odd. Could you describe precisely what you are trying to do regarding this?

OK.
"Run-time error 91, Object variable or With block variable not set."
When you make an array of Shapes, you are NOT copying them. And you need to use Set - just like the error message states. I take it you are not using the default 0 indexing...but no mind.
Code:
[COLOR=red]SET[/color red] Arrows(1) = ArrowRange.ShapeRange.Item(1)
will in fact fill your array of the Shapes. But again, this is NOT copying them. You are making an array of the objects. Therefore you can use that array to do stuff TO THOSE OBJECT properties.

Try using Set to make the array, and then Arrows(1). and see what Intellisense comes up with. NO COPY. Just the methods and properties OF THE OBJECT itself.

lastly, as these lines (arrows) all the same? If so...why make an array?

Gerry
My paintings and sculpture
 
Gerry,

Thanks so much for your reply. To answer your questions, the document I'm working with contains a survey form that I'm seeking to populate with the task descriptions to be rated that are listed in another part of the document. As the survey form gets progressively longer, I need to keep re-establishing its heading containing the rating categories at the start of a new page. These are in a 1 row table. There are arrows (shapes) connecting the rating categories in this table to the response categories in the table below containing the task descriptions. This design is not my doing, but something I have to work with. Thus, I need to get these arrows that are between the two tables which exist in the starting page of the form into an array so that I can use them in setting up each addition page of the survey that is needed.

Your suggestion about using the Set command WORKED! Thanks so much for that. However, when I got to the point where I tried to insert the arrows on the new page, using commands of the following type:
Selection.Text = Arrows(1) + Arrows(2)
it didn't work -- "object doesn't support the method".
Could you give me some advice on how to accomplish this?

Again, you have been very helpful and I sincerely appreciate it.

Jeff
 
Uh Jeff...yeah it won't work this way. That was my point. You are building an array of objects. You are not making a copy of those objects.

Selection.Text is expecting.....text. And Arrows(1) is not text, it is an object.

It seems to be it would waaaaaaaay easier to have the chunk that need to keep adding (blank I assume, the table with the - yuck yuck yuck - arrows to another table) as either a separate file that you can bring in.

That is:

Need a new chunk? InsertFile filename:=blankchunk.doc

OR,

have a blank chunk within the document that you can just copy the whole thing and put in the new location;

OR,

make a range of an existing chunk, copy that, strip out the existing text (if required) and put THAT in a new location.

Building it this way is crappy. And frankly...I have been trying to make this work, and can't yet. I have tried making arrays of selections and a bunch of other ways. The problem is that the shapes are in those slkdhskldfhv xbvxkhkiyetoeteltkljslfjs#$^&#$%&^**((**@#! drawing boxes. Lordy I hate them I hate them I hate them.

There must be a better way. I am still trying, because it is p#$$ing me off. But I can't see it working doing it this way.

Gerry
My paintings and sculpture
 
Gerry,

I tried every way I could think of to make it work with the array today and failed. It pisses me off, too, because what else would be the purpose in a Word document of putting shapes into an array if you didn't want to use them again in the document? Anyway, I tried the copy and paste route and got that to work. So, I just have to remember not to add anything else to the clipboard as long as I have need for the stuff I copied. Is there any way in VBA to add several items to the clipboard and then select the one you want to paste?

Again, thanks so much for your help, and I apologize for immersing you in such a frustrating problem.

Regards,

Jeff
 
Hey Jeff, please do not EVER apologize for tossing interesting (even if frustrating) problems here.
Is there any way in VBA to add several items to the clipboard and then select the one you want to paste?
Not that I know of.

I am going to try and make a Collection of the shapes and use that, but I have some other stuff to do.

Again, this is NOT the way I would do this. Individual copy and pastes???? Bleeech.
what else would be the purpose in a Word document of putting shapes into an array if you didn't want to use them again in the document?
That is easy to answer.

1. delete them
2. rotate them
3. etc etc every method and property of the objects

But you are limited to the methods of the objects themselves...because that is what you have, an array of objects. And a Shape itself does NOT have a copy method.

Gerry
My paintings and sculpture
 
Gerry,

But if you wanted to do things like rotate the shapes once they were in an array, at some point you would want to put them back in the document. And that is just the point we're stuck at. I don't like the copy and paste approach either, but it seems to be the only thing that works.

I'll await further insights from you. You really seem to know your stuff on VBA.

Regards,

Jeff
 
But if you wanted to do things like rotate the shapes once they were in an array, at some point you would want to put them back in the document.
Huh????? What do you mean "back" in the document????? The array references the objects. It does not extract them, or anything like that. It does not take them out.

Say you build the array of six shapes - called ShapeArray.
Code:
Dim var
Dim i As Single
i = 1.25
For var = 0 To Ubound(ShapeArray())
   ShapeArray(var).ScaleWidth i, msoTrue
   i = i + 0.25
Next
would scale the width of the first Shape 125%; the second Shape 150%; the third Shape 175%; the fourth Shape 200% etc. It does not take them out, and put them back. It is an array of the objects, and you can do whatever methods you can with them. You are actioning the objects themselves - by referencing them. Having them in the array does not make some independent entity.

But Copy is not one of the methods available. Objects do not copy themselves.

Gerry
My paintings and sculpture
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top