Automatically update document styles failsafe

S

Susan J-P

Greetings:

To begin with, I'm *not* a programmer, so please keep this in mind when you
reply. This is a relatively long post on the theory that too much information
and detail up front is better than too little.

I'm using Word 2003 on Windows XP.

Problem I'm trying to resolve: automatically unselect the "Automatically
update document styles" on the Tools/Templates and Add-Ins... dialog whenever
someone uses the dialog to refresh a document's styles from its attached
template. I've been following the steps detailed in the MVP article at
http://word.mvps.org/faqs/macrosvba/UpdateStyles.htm with partial success but
am encountering some frustrating issues.

The code below (snipped from the article) breaks the document's attachment
to a custom global template (after I refresh the document styles), and with
the Tools/Templates and Add-Ins... dialog mostly greyed out, I can't reattach
it without quitting Word and restarting:

Sub FileTemplates()

With Dialogs(wdDialogToolsTemplates)
.Show

If .LinkStyles = 1 Then
ActiveDocument.UpdateStyles
Dim oLT As ListTemplate

On Error Resume Next
For Each oLT In ActiveDocument.AttachedTemplate.ListTemplates
If Not oLT.Name = "" Then
If Not ActiveDocument.ListTemplates(oLT.Name).ListLevels(1) _
.LinkedStyle = oLT.ListLevels(1).LinkedStyle Then
ActiveDocument.UpdateStyles
Exit For
End If
End If
Next oLT
ActiveDocument.UpdateStylesOnOpen = False
End If
End With

End Sub

When I try the code below (also snipped from the MVP article -- a workaround
to the menu display bug from the first solution), it errors out at the first
line:

CommandBars.FindControl(ID:=751).Execute

and I haven't been able to figure out why.

Sub ReplacementToolsTemplatesAndAddins()

'Execute the built-in button
CommandBars.FindControl(ID:=751).Execute
If Dialogs(wdDialogToolsTemplates).LinkStyles = 1 Then
Dim oDoc As Document, oLT As ListTemplate
Set oDoc = ActiveDocument
oDoc.UpdateStyles

On Error Resume Next
For Each oLT In oDoc.AttachedTemplate.ListTemplates
If Not oLT.Name = "" Then
If Not oDoc.ListTemplates(oLT.Name).ListLevels(1) _
.LinkedStyle = oLT.ListLevels(1).LinkedStyle Then
oDoc.UpdateStyles
Exit For
End If
End If
Next oLT
oDoc.UpdateStylesOnOpen = False
End If

End Sub

All I really need here is to have a failsafe for the "Automatically update
document styles" checkbox, and to keep (or reattach) my custom global
template.

Thanks in advance for your help,
 
S

Susan J-P

BTW, the error message I get at CommandBars.FindControl(ID:=751).Execute:

Run-time error '91':
Object variable or With block variable not set
 
J

Jezebel

If you're concerned only with clearing the 'Automatically update' checkbox,
you can leave out all that stuff about ListTemplates. All you need is ---

Sub FileTemplates()

'Show the Templates and Add-ins dialog -- user can do what they like
Dialogs(wdDialogToolsTemplates).Show

'Clear the 'Automatically Update' setting
ActiveDocument.UpdateStylesOnOpen = False

End sub

Whether this gets around the bug in the dialog that the MVPS site refers to,
I don't know. If not, you'll have to try their second method. The error
you're getting means that the code can't find the Templates and Add-ins menu
option: perhaps you misunderstood their instruction about dragging the menu
from the Tools menu ONTO the Hidden toobar, and instead simply dragged the
option into oblivion. To fix that, bring up the toolbars > Customize dialog,
right-click the Tools menu, and select reset.

In any case, all that mucking around with the 'Hidden' toolbar is
unnecessary. All you need do is hide the old Templates and Add-in option,
and add your new one. To hide the old menu option, switch to VBA and in the
immediate window, enter --

CommandBars.FindControl(ID:=751).Visible = false
 
S

Susan J-P

Hi Jezebel:

Sorry it's taken me so long to reply and thank you. I wasn't able to
dedicate the necessary time to work through and test your suggestions until
this week.

Everything worked wonderfully except for using the Immediate Window--I'm
assuming I'm probably doing something wrong since I'm really not very adept
in VBA.

However, I was able to develop a workaround using an AutoExec() macro
instead, so things are just fine here.

I appreciate your help and time.

BTW, if there's a good resource for me to learn more about the Immediate
Window, please let me know. I didn't find the VBA online help particularly
useful for that.

Thanks again,

--
Susan
Technical Writer


Jezebel said:
If you're concerned only with clearing the 'Automatically update' checkbox,
you can leave out all that stuff about ListTemplates. All you need is ---

Sub FileTemplates()

'Show the Templates and Add-ins dialog -- user can do what they like
Dialogs(wdDialogToolsTemplates).Show

'Clear the 'Automatically Update' setting
ActiveDocument.UpdateStylesOnOpen = False

End sub

Whether this gets around the bug in the dialog that the MVPS site refers to,
I don't know. If not, you'll have to try their second method. The error
you're getting means that the code can't find the Templates and Add-ins menu
option: perhaps you misunderstood their instruction about dragging the menu
from the Tools menu ONTO the Hidden toobar, and instead simply dragged the
option into oblivion. To fix that, bring up the toolbars > Customize dialog,
right-click the Tools menu, and select reset.

In any case, all that mucking around with the 'Hidden' toolbar is
unnecessary. All you need do is hide the old Templates and Add-in option,
and add your new one. To hide the old menu option, switch to VBA and in the
immediate window, enter --

CommandBars.FindControl(ID:=751).Visible = false
 

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