We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# VBA / MS Word problem

on
Medium Priority
417 Views
Hi

Hope someone can help me...

I have a collection (named PointsCollection) of X and Y values that I use to draw rectangle shapes on a word document (named Doc).

X and Y are the left and top values for each rectangle, relatives to the page.

For each Point in PointsCollection

Set Shp = Doc.Shapes.AddShape( msoShapeRectangle, Point.X, Point.Y, 50, 50 )

next

This works fine but I must only print a maximum of 3 rectangles on each page. If there are any more rectangles then I must add a new pages and start adding the remaining rectangles there.

How can I do this?
Comment
Watch Question

## View Solution Only

Commented:

dim i as integer

For each Point in PointsCollection

i = i + 1
Set Shp = Doc.Shapes.AddShape( msoShapeRectangle, Point.X, Point.Y, 50, 50 )
if (i = 3) then
Application.Selection.InsertBreak Type:=wdPageBreak
i= 0
end if
next

Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
You need to use the Anchor parameter. Put page breaks in and then a paragraph to tie the shapes to.

Option Explicit

Sub InsertShapes(Doc as Word.Document)
Dim shp As Shape
Dim p As Word.Paragraph
Dim t As Integer

Set shp = Doc.Shapes.AddShape(msoShapeRectangle, 10, 10, 50, 50, p.Range)
For t = 2 To 10
Doc.Bookmarks("\EndOfDoc").Range.InsertBreak wdPageBreak
Doc.Bookmarks("\EndOfDoc").Range.InsertParagraph
Set p = Doc.Paragraphs.Last
Set shp = Doc.Shapes.AddShape(msoShapeRectangle, 10 * t, 10, 50, 50, p.Range)
Next t
End Sub

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
Hello EDDYKT , I have already tested your suggestion, if you run the code you'll find that it doesn't works as spected

Hi GrahamSkan, thanks!, I think I can work using your suggestion!
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile