Adding TextBox Shapes becomes slow

D

Diman

I need to add about 200 TextBox Shapes to a document through Visual
Basic using Word 9.0 Library

The Code i use is:
Set textBox22 = wordDoc.Shapes.AddTextbox(msoTextOrientationHorizontal,
10, y, 100, 15)

Then i add formatting:

With textBox22
.Name = "TextBox" & i
With .TextFrame
.MarginTop = 0
.MarginLeft = 0
.TextRange = "sdfgsf"
With .TextRange
.Bold = True
.Font.Name = "Verdana"
.Font.Size = 12
End With
End With
End With

This all works like a charm... except it works SLOOOOOOOW

The first 30 or so controls are added at a reasonable speed (i have a
counter and a progressbar to monitor the process)
After that it starts to slow down more and more....
On textbox 150 it adds about one textbox per second... Very Slow!!!!!

I tried following:
Turned off spelling and grammar checking, Word app is not visible while
textboxes are added, I even used LockWindowUpdate TOGETHER with
ScreenUpdating = False. Didn't help. Still slow....

Here's the code for you to try. Visual Basic 6.0. Add a reference to
the Word library before running the code. Add a progressbar to Form1
and name it PB1.

The code adds 300 textboxes to a word document and sets margins and
font format options. if you comment the formatting (all With..End With)
the code runs faster.


P.S. I HAVE tried it on different computers, and no they were not 486,
but decent p4 machines. And it must be done from outside, thus i can't
use the code from within a word macro.


Code:
Dim wA As Word.Application
Dim wD As Word.Document




Private Sub Command1_Click()

PB1.Min = 1 'PROGRESSBAR
PB1.Max = 300

Dim ttt As Word.Shape, y As Long
y = 10

wA.ScreenUpdating = False

For i = 1 To 300
PB1.Value = i
y = y + 18

Set tB = wD.Shapes.AddTextbox(msoTextOrientationHorizontal, 10,
y, 100, 15)

With tB
.Name = "TextBox" & i
With .TextFrame
.MarginTop = 0
.MarginLeft = 0
.TextRange = "Sample Text"
With .TextRange
.Bold = True
.Font.Name = "Verdana"
.Font.Size = 12
End With
End With
End With
Next

wA.ScreenUpdating = True

wA.Visible = True
End Sub





Private Sub Form_Load()
Set wA = New Word.Application

wA.Options.CheckGrammarAsYouType = False
wA.Options.CheckGrammarWithSpelling = False
wA.Options.CheckSpellingAsYouType = False
wA.Options.CheckHangulEndings = False
'wA.Options.Pagination = False

Set wD = wA.Documents.Add
wD.Activate
wD.ShowSpellingErrors = False
wD.ShowGrammaticalErrors = False

End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
On Error Resume Next
wA.Quit wdDoNotSaveChanges

End Sub
 
J

Jezebel

Might be more useful to explain what you're actually trying to do. Why not
just do it once and save as a template?
 
D

Diman

I'm building this word document with a different number of textboxes
each time.. depending on data i receive from network. Even formatting
may differ, so a template is not an option.
But WHY does it slow down like hell after a while... That's what i
want to know... not suggestions on other workarounds.
Thanks anyway
 
J

Jezebel

Given that your current method is, on your own account, unworkable, an
alternative method is precisely what you DO want. Why bother diagnosing what
doesn't work?
 
D

Diman

As soon as i remove formatting, everything goes very fast again, but i
must have formatting... (font, font.size, fill.forecolor etc).

I'm tearing my hear out......
 
D

Diman

Jezebel said:
Might be more useful to explain what you're actually trying to do. Why not
just do it once and save as a template?


news:[email protected]...




i'm creating a document based on data from network, so i can't use
templates, because the number of textboxes and their formatting may
vary from time to time
 
J

Jezebel

i'm creating a document based on data from network, so i can't use
templates, because the number of textboxes and their formatting may
vary from time to time

That's not a reason not to use a template. However, you seem committed to
your existing, malfunctioning method ...
 
T

Tony Strazzeri

Diman said:
As soon as i remove formatting, everything goes very fast again, but i
must have formatting... (font, font.size, fill.forecolor etc).

I'm tearing my hear out......

You could consider storing an iteration of the paragraph or line which
contains the textboxes as an autotext entry and then insert this as
many times as needed.

I'm not saying it will be faster, but it may be worth a try.

Cheers
TonyS.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top