PDA

View Full Version : Adding a Large Number of Shapes is Slow



vdivito
08-30-2009, 07:05 PM
Adding a Large Number of Shapes is Very Slow in Word 2002

I have a fairly simple macro that adds 186 WordArt shapes to a Word document. When the macro starts it executes fairly quickly for the first few rows of shapes and then starts to get progressively slower as it goes. It seems if there are more than just a few shapes on the doc things get really slow. Execution takes minutes. Any suggestions are appreciated. My code is here:



Option Explicit

Sub Test()

Dim s As String
Dim a As Integer
Dim i As Integer
Dim vpos As Long
Dim hpos As Long
Dim oShape As Shape


Application.ScreenUpdating = False

vpos = 104

For a = 1 To 31

hpos = 53

s = a

'Place line number
Set oShape = ActiveDocument.Shapes.AddTextEffect _
(PresetTextEffect:=msoTextEffect6, _
Text:=s, _
FontName:="Comic Sans MS", _
FontSize:=10, _
FontBold:=False, _
FontItalic:=False, _
Left:=0#, _
Top:=0#)

With oShape
.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.ScaleHeight 0.7, msoFalse, msoScaleFromBottomRight
.TextEffect.ToggleVerticalText
.Fill.ForeColor = vbRed
.Line.BackColor.RGB = RGB(0, 0, 0)
.Shadow.ForeColor.RGB = RGB(0, 0, 0)
.Line.Visible = msoFalse
.IncrementRotation 0
.WrapFormat.Type = wdWrapNone
.ZOrder msoBringToFront
.Left = hpos - .Width
.Top = vpos
End With
Set oShape = Nothing


'Place 5 three digit numbers
hpos = 85

For i = 1 To 5

s = "333"

Set oShape = ActiveDocument.Shapes.AddTextEffect _
(PresetTextEffect:=msoTextEffect6, _
Text:=s, _
FontName:="Comic Sans MS", _
FontSize:=12, _
FontBold:=True, _
FontItalic:=False, _
Left:=0#, _
Top:=0#)


With oShape

.RelativeHorizontalPosition = wdRelativeHorizontalPositionPage
.RelativeVerticalPosition = wdRelativeVerticalPositionPage
.ScaleWidth 1.4, msoFalse, msoScaleFromTopLeft
.ScaleHeight 0.6, msoFalse, msoScaleFromBottomRight
.TextEffect.ToggleVerticalText
.Fill.ForeColor = vbRed
.Line.BackColor.RGB = RGB(0, 0, 0)
.Shadow.ForeColor.RGB = RGB(0, 0, 0)
.Line.Visible = msoFalse
.IncrementRotation 0
.WrapFormat.Type = wdWrapNone
.ZOrder msoBringToFront
.Left = hpos
.Top = vpos

End With

Set oShape = Nothing

hpos = hpos + 61
Next i

vpos = vpos + 13.5
Next a

Application.ScreenUpdating = True


End Sub


Edit: VBA tags added to code. Select your code when posting and hit the VBA button to format it for the forum.

lucas
08-31-2009, 04:19 AM
So you are basically building a document using code?

Obvious question for me is why not use a template?

macropod
08-31-2009, 04:47 AM
Hi vdivito,

Please see: http://www.excelguru.ca/node/7

Replied to at: http://www.tek-tips.com/viewthread.cfm?qid=1566959&page=1

My testing did not evidence the slowdown complained of.

vdivito
08-31-2009, 08:42 AM
Yes, I am building a document via code. The contents of the WordArt is established at run time so a template can not be used. The code I posted here does not include the actual strings in use in the original code but the behavior is identical.

lucas
08-31-2009, 08:56 AM
You should address the cross posting and verify that you followed the link that macropod supplied and that you understand.

Tinbendr
08-31-2009, 10:01 AM
Interesting... I've tried this on two different machines and have the same problem as vdivito.

Both machines run XP.

One machine, a 3Ghz machine, running Word 2002, roughly takes 4 seconds per iteration.

The second machine, a PIII 500mHz running Word 2003, takes about 5 seconds per interation.

Sorry, no help to offer, but curious how some machines are affected while others are not.

vdivito
08-31-2009, 11:11 AM
I understand the message about cross posting.

My real problem is that the code gets slower as more shapes are added to the document. I inserted a timer into my code and you can see here the execution time of each main loop which places 6 shapes. The second set of times is the macro executed with 186 shapes already present on the document. I have tested this on 3 machines. Two with W2002 and one with W2003.

Loop 1 time: 0.578125 seconds
Loop 2 time: 0.65625 seconds
Loop 3 time: 1.046875 seconds
Loop 4 time: 1.375 seconds
Loop 5 time: 1.90625 seconds
Loop 6 time: 2.125 seconds
Loop 7 time: 2.390625 seconds
Loop 8 time: 2.75 seconds
Loop 9 time: 3.09375 seconds
Loop 10 time: 3.546875 seconds
Loop 11 time: 3.953125 seconds
Loop 12 time: 4.484375 seconds
Loop 13 time: 5.03125 seconds
Loop 14 time: 5.546875 seconds
Loop 15 time: 6.109375 seconds
Loop 16 time: 6.65625 seconds
Loop 17 time: 7.203125 seconds
Loop 18 time: 7.703125 seconds
Loop 19 time: 8.21875 seconds
Loop 20 time: 8.8125 seconds
Loop 21 time: 9.34375 seconds
Loop 22 time: 9.921875 seconds
Loop 23 time: 10.5625 seconds
Loop 24 time: 11.21875 seconds
Loop 25 time: 11.78125 seconds
Loop 26 time: 12.42188 seconds
Loop 27 time: 12.95313 seconds
Loop 28 time: 13.57813 seconds
Loop 29 time: 14.28125 seconds
Loop 30 time: 15.73438 seconds
Loop 31 time: 15.48438 seconds

lucas
08-31-2009, 11:42 AM
Tell me again why a template won't work? Do you randomize the shapes or what?

vdivito
08-31-2009, 11:55 AM
The strings that are used in the shapes are generated from other data at runtime.

geekgirlau
09-01-2009, 12:35 AM
Does it make any difference changing your display settings for Word? For example, try setting the document to Normal view, and turn on picture placeholders.

fumei
09-01-2009, 01:08 PM
Honestly, I sincerely do not mean - in any way - to be offensive...but this is an abuse of Word.

Word is a word-processor. It is not, nor ever has been, designed to be a graphical/layout type application. It processes words very very well. Using it for 186 graphical elements pushes it beyond what it is designed to be...a word-processor.

If you have need for so many graphical elements, you really should use a proper application designed for graphical elements.

The problem is that Word moves EVERYTHING through the printer-driver.

So...insert ONE graphical element, Word sends the whole document out and processes through the printer driver. Fine, not usually a problem.

Insert TWO graphical elements, Word sends the whole document out and processes through the printer driver, checking the first graphical element, then the second.

Insert THREE graphical elements, Word sends the whole document out and processes through the printer driver, checking the first graphical element, then the second, then the third.

And so on. So 186 elements...Words send the whole document out and processes through the printer driver and checks/validates each and every graphical element individually, one by one sequentially through the printer driver.

This slows things down, as you have discovered. This is the way Word works...through the printer driver, each and every time.

There are a couple of things that may help though.

1. Turn refresh off
2. Turn screen update off.
3. Turn Pagination off.

Bottom line though, is all those graphical elements are processed through the printer driver, and that takes I/O back and forth, and there is nothing you can do about it.

Except of course use a proper graphical/layout application.

PS. If I had my way, I would have Word have a built-in limit to graphical elements. Say 20. That way it could handle brochures and the like, but refuse to work with massive numbers like 186.