PDA

View Full Version : Bookmarks Show/Hide code makes document SO SLOW



mjohnston
07-21-2008, 11:37 PM
Hi All,

I'm hoping someone will be able to give me a hand.. I have inherited someones project which is a document which has a number of checkboxes at the top, and depending on which checkbox is selected a number of tables- which are inside bookmarks- and shown or hidden. This has unfortunately made the word doucment extremely slow to load (~2-3Minutes to open) and the show/hide functions can take ~40seconds-1min to load when a box is checked!

My question is, is this an inherent problem with using bookmarks and show/hide data within them, or is it more likely the code that has been written? I have included a small snippet below so you can get the jist of what is going on. Any feedback would be appreciated.

Personally I think that it could just be the way that it is written, but I haven't really worked with bookmarks before so its something I will have to look at. Please let me know if you want any additional info, or if it makes it easier I will send the document to someone if they would like to see the full thing!

Thanks in adavnce :)
Matt

Private Sub Selection_BI_CB_Click()
StripHiddenBookmarks
If Selection_BI_CB.Value = True Then
Module1.disable_all_apps
Selection_BI_CB.Enabled = True
Module1.non_super1_hide
Module1.areas_affected_disable
Set rng = Bookmarks("Extra_Space_BI1").Range
hide
Set rng = Bookmarks("Extra_Space_BI2").Range
hide
Set rng = Bookmarks("Extra_Space_BI3").Range
hide
Set rng = Bookmarks("Extra_Space_BI4").Range
hide
End If

If Selection_BI_CB.Value = False Then
Set rng = Bookmarks("Extra_Space_BI1").Range
show
Set rng = Bookmarks("Extra_Space_BI2").Range
show
Set rng = Bookmarks("Extra_Space_BI3").Range
show
Set rng = Bookmarks("Extra_Space_BI4").Range
show

Module1.enable_all_apps
Set rng = ThisDocument.Bookmarks("S1_Areas_Affected").Range
show
Set rng = ThisDocument.Bookmarks("KOL_Areas_Affected").Range
show
Set rng = ThisDocument.Bookmarks("STP_Areas_Affected").Range
show
Set rng = ThisDocument.Bookmarks("Nova_Areas_Affected").Range
show
Set rng = ThisDocument.Bookmarks("Eplat_Areas_Affected").Range
show
Set rng = ThisDocument.Bookmarks("EAI_Areas_Affected").Range
show
Set rng = ThisDocument.Bookmarks("Wizard_Areas_Affected").Range
show
Module1.non_super1_show
End If
End Sub

OTWarrior
07-22-2008, 01:12 AM
Well, the first thing would be to put each duplicate selection into a for next loop, and to use with statements. The main issue is the shear amount of data you are checking here, which is why it is slow. By optimising the code, you should get some kind of increase.

try this:

Private Sub Selection_BI_CB_Click()
Dim i As Integer
Dim j As Integer
Dim listA As Variant
Dim listB As Variant

StripHiddenBookmarks
i = 0
j = 0

listA = Array("Extra_Space_BI1", "Extra_Space_BI2", "Extra_Space_BI3", "Extra_Space_BI4")

listB = Array("S1_Areas_Affected", "KOL_Areas_Affected", "STP_Areas_Affected", "Nova_Areas_Affected", _
"Eplat_Areas_Affected", "EAI_Areas_Affected", "Wizard_Areas_Affected")

with Module1
If Selection_BI_CB.Value = True Then
.disable_all_apps
Selection_BI_CB.Enabled = True
.non_super1_hide
.areas_affected_disable

For i = 0 To 3
Set rng = Bookmarks(listA(i)).Range
hide
Next i

Else

For i = 0 To 3
Set rng = Bookmarks(listA(i)).Range
hide
Next i

.enable_all_apps
For j = 0 To 6
Set rng = ThisDocument.Bookmarks(listB(j)).Range
Show
Next j
.non_super1_show
End If
end with
End Sub

macropod
07-22-2008, 02:10 AM
Hi Matt,

If automatic column widths are enabled for the tables, the recalculation the column widths (if that's what Word does when they're hidden/unhidden) will drmatically slow down the macro’s execution. To avoid this, you could run a macro like:
Sub FixTableWidths()
Dim oTbl As Table
For Each oTbl In ActiveDocument.Tables
oTbl.AllowAutoFit = False
Next
End SubThis is equivalent to using Table|Properties|Table|Options and unchecking the "Automatically resize to fit contents" option for each table in the document. You should only have to run this code once.

mjohnston
07-23-2008, 08:55 PM
Thanks guys :) I have implemented the changes and it seems to be working a bit better. I also put

Application.ScreenUpdating = False / True

At the Top / Bottom of each of the check box functions. This seems to have helped a bit with the loading as well. I spoke to another friend of mine who said that even though some of the sections are hidden, when the doc is opened the whole lot is loaded into memory so that could be playing a part with the initial loading time a bit. Thanks anyway :) It was good to see another aussie on here too! :)