Find & Build Running Extremely Slow

B

Brian DePratto

I have a macro that was originally written for Word 2000.
It searches for a specific style, then builds a textbox
beside that area, and places an image inside of it (to get
the image in the margins, without ruining the document
margins). Attempting to run this macro in Word 2003 is a
chore in boredom, often taking up to 20 times longer to
complete execution. I attempted to disable repagination
(which seems to be the source of the problem - Word
repaginates every time a text box is created). However,
this was impossible, as Word renables it for some reason
(seemingly undocumented). As well, attempting to run the
macro in a non-paginated (normal) view didn't work, as Word
switches to Layout mode without asking.

Does anyone have any suggestions for ways to improve the
exectute time of this macro?

-Brian DePratto
Junior Technical Developer
Campana Systems Inc.
 
J

Jay Freedman

Hi, Brian,

In the absence of any code to look at, I'll guess that the macro is using
the Selection.Find object to go to the styled paragraph, and
ActiveDocument.Shapes.Add to create the textbox anchored in the resulting
Selection. If that's the case, try using a declared Range object's .Find
instead.

Also, you do *not* need a textbox in order to put an image in the margin.
The image will float perfectly well without the box, as long as its wrapping
is properly set.

Finally, instead of pasting in external images, create AutoText entries to
hold them in the template (I assume they're small icons), and use the
AutoTextEntries(index).Insert method to place them in the document.

Following these suggestions leads to code like this, where
..AutoTextEntries("icon") is an entry made from an image floating in the
margin:

Sub InsertIcons()
Dim oRg As Range, oRg2 As Range
Set oRg = ActiveDocument.Range

ActiveDocument.ActiveWindow.View = wdNormalView
Options.Pagination = False
With oRg.Find
.ClearFormatting
.Text = ""
.Format = True
.Forward = True
.Wrap = wdFindStop
.Style = ActiveDocument.Styles("Body Text 3")

Do While .Execute
Set oRg2 = oRg.Duplicate
oRg2.Collapse wdCollapseStart
ActiveDocument.AttachedTemplate _
.AutoTextEntries("icon").Insert _
where:=oRg2, RichText:=True
oRg.Collapse wdCollapseEnd
Loop
End With
ActiveDocument.ActiveWindow.View = wdPrintView
End Sub
 

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