Problem with VBA runtime efficiency

Q

Qiang

I have a VB standard exe program as following:
Dim app as new Word.Application
app.Visible = False
Dim doc as Word.Document
Set doc = app.Documents.Open("some word document")
Dim str As Word.Range
StartTime = Time
For i = 1 To 10000
Set str = doc.Range
Next i
EndTime = Time
MsgBox DatedDiff("s",StartTime,EndTime)
doc.Close
Set doc = nothing
app.Quit
Set app = Nothing


It takes 25 senconds to execute this program,o my god,I
don't know why,because if i use macro to run this program
in word document,it takes less than 1 sencond.why the
execution efficiency is so slow?Could anybody tell me how
can i improve the efficiency or what's wrong with my
program.thank you very much
 
J

Jezebel

The time goes on the marshalling between the two apps (ie Word and your VB
app). There's no immediate fix other than to design your app to minimise the
communications between the two. You can gain some efficiency by 'minimizing
the dots' -- use With ... End With constructions --

With app
set doc = .Documents.Open(FileName)
with doc
set str = .range
end with
end with

Doubt it will make much difference in this case (which is a fairly odd test
in any case); but it does help if there are multiple references to the same
thing.
 

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