Removing a ListTemplate from a style in Word
Ran into an interesting question on Stack Overflow the other day. The problem description is short, so I’ll copy it here:
There is a word VBA method
style("style").LinkToListTemplate ListTemplate:=Nothing
which is used to set the style numbering to None. My problem is that I cannot find the same in C# word interop. The method exists but does not work withstyle.LinkToListTemplate(null)
.
For starters, I had never even thought about how to remove a ListTemplate from a Word style – so I learned something new and very useful.
Figuring out the C# took a little thought. As the OP said, how to pass the correct value to the LinkToListTemplate
method? After some consideration and a bit of experimenting I realised that the LinkToListTemplate
method requires a ListTemplate
object.
VBA isn’t so choosy; as usual, it figures out the correct data type for the developer, so the following works:
Sub StyleWithListTemplate()
Dim styl As word.style, lt As word.ListTemplate
Set styl = ActiveDocument.styles("test")
''' Use the following to attach a ListTemplate to the style
''' (assumes the document has two ListTemplates!)
'Set lt = ActiveDocument.ListTemplates(2)
'styl.LinkToListTemplate ListTemplate:=lt, ListLevelNumber:=1
''' Remove the ListTemplate from the style
styl.LinkToListTemplate ListTemplate:=Nothing
End Sub
In C# the method works like this:
Word.ListTemplate lt = null;
styl.LinkToListTemplate(lt);
Note: While testing I noticed that assigning and removing a ListTemplate to/from a style (that’s used in the document) creates additional ListTemplates in the document. This can be a problem if you do it a lot in the one document. In the version where ListTemplates were introduced (97, as I recall) it could corrupt files, or even crash them. MS fixed that by having Word automatically delete unused LTs when a critical number accumulated. But it can affect file size.