#1   Report Post  
Posted to microsoft.public.word.docmanagement
Naga
 
Posts: n/a
Default Word Automation

I ran the below program in a machine with XP SP1 and Word 2000 it took only 2
or 3 seconds to insert the bookmarks into the word document. But in a machine
with XP SP2 and Word 2000 it took 18 seconds in an average. I am using the
similar kind of program to insert the bookmarks into a letter at the runtime.

Could anyone please help me out.

Private Sub Command1_Click()
On Error GoTo err
Dim obj As New Word.Application
Dim bk As Bookmark
obj.Documents.Open "C:\Test\ObjA.dot"
obj.Visible = True
StartLog
SendToLog ("BookMark Insertion Starts")
For Each bk In obj.ActiveDocument.Bookmarks
If obj.ActiveDocument.Bookmarks.Exists(bk.Name) Then
obj.ActiveDocument.GoTo What:=wdGoToBookmark, Name:=bk.Name
obj.ActiveDocument.Bookmarks(bk.Name).Select
obj.Selection.InsertAfter bk.Name
obj.ActiveDocument.Bookmarks(bk.Name).Delete
End If
Next
SendToLog ("BookMark Insertion Ends")
EndLog
MsgBox "completed"

err:
Set obj = Nothing

End sub
  #2   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman
 
Posts: n/a
Default Word Automation

Try eliminating the selection and using just the bookmark's range:

For Each bk In obj.ActiveDocument.Bookmarks
obj.ActiveDocument.Bookmarks(bk.Name).Range.Text = bk.Name
Next

The selection isn't necessary, and it often forces Word to redraw the
screen, which is by far the slowest operation in the macro. Also, it isn't
necessary to test whether the bookmark exists -- if it didn't exist, the For
Each loop wouldn't iterate it.

As for your original question -- which I assume was "why is there a
difference between SP1 and SP2?" -- I don't know. This change should make
the program enough faster that it doesn't matter.

If you have any further questions, please post in one of the word.vba
newsgroups, not in docmanagement.

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

Naga wrote:
I ran the below program in a machine with XP SP1 and Word 2000 it
took only 2 or 3 seconds to insert the bookmarks into the word
document. But in a machine with XP SP2 and Word 2000 it took 18
seconds in an average. I am using the similar kind of program to
insert the bookmarks into a letter at the runtime.

Could anyone please help me out.

Private Sub Command1_Click()
On Error GoTo err
Dim obj As New Word.Application
Dim bk As Bookmark
obj.Documents.Open "C:\Test\ObjA.dot"
obj.Visible = True
StartLog
SendToLog ("BookMark Insertion Starts")
For Each bk In obj.ActiveDocument.Bookmarks
If obj.ActiveDocument.Bookmarks.Exists(bk.Name) Then
obj.ActiveDocument.GoTo What:=wdGoToBookmark, Name:=bk.Name
obj.ActiveDocument.Bookmarks(bk.Name).Select
obj.Selection.InsertAfter bk.Name
obj.ActiveDocument.Bookmarks(bk.Name).Delete
End If
Next
SendToLog ("BookMark Insertion Ends")
EndLog
MsgBox "completed"

err:
Set obj = Nothing

End sub



  #3   Report Post  
Posted to microsoft.public.word.docmanagement
Jay Freedman
 
Posts: n/a
Default Word Automation

Taking a second look at this, the code can be even further condensed
-- there's no need to work with the bookmark's name as an index into
the Bookmarks collection once you have a pointer to the bookmark
object itself:

For Each bk In obj.ActiveDocument.Bookmarks
bk.Range.Text = bk.Name
Next

--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.

On Mon, 17 Apr 2006 15:11:23 -0400, "Jay Freedman"
wrote:

Try eliminating the selection and using just the bookmark's range:

For Each bk In obj.ActiveDocument.Bookmarks
obj.ActiveDocument.Bookmarks(bk.Name).Range.Text = bk.Name
Next

The selection isn't necessary, and it often forces Word to redraw the
screen, which is by far the slowest operation in the macro. Also, it isn't
necessary to test whether the bookmark exists -- if it didn't exist, the For
Each loop wouldn't iterate it.

As for your original question -- which I assume was "why is there a
difference between SP1 and SP2?" -- I don't know. This change should make
the program enough faster that it doesn't matter.

If you have any further questions, please post in one of the word.vba
newsgroups, not in docmanagement.

Reply
Thread Tools
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
WP merge file to Word sstires Tables 4 February 14th 06 06:26 PM
How can Word display full path of a file in the title bar? SAsif Microsoft Word Help 1 January 26th 06 04:32 PM
Does Word have a QuickCorrect/Quick Word option like WordPerfect? CW New Users 2 December 20th 05 05:54 PM
In Word, how do I surpress headers and footers on page 2 Bill Microsoft Word Help 1 December 15th 05 06:13 PM
Envelope Address GR New Users 5 April 24th 05 09:48 PM


All times are GMT +1. The time now is 02:59 PM.

Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 Microsoft Office Word Forum - WordBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Word"