Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Style not linking to ListTemplate

Status
Not open for further replies.

duGly

Technical User
Nov 13, 2006
52
US
I am trying to apply a ListTemplate to a style. The code runs without error, but there is no change made to the document. Here are the details:

I have a UserForm that creates multiple documents and places them in a common project folder. The code creates a new document for each chapter and appendix of the document. The number of chapters and appendices is variable.

In my code (see excerpt below), I try to link the style "Chapter Heading 1" to a custom ListTemplate named "ltChapterOutline". I adjust the StartAt property, as well as the NumberFormat and NumberStyle (for appendices only). But when I open the newly created documents, each one is numbered as Chapter 1.

I have troubleshot this by stepping through the code on a dummy document, and it works fine. But when I run it from the UserForm, it fails to make the changes. Here is my code:

Code:
    ' Set m_Doc to newly created document and open it.
    Set m_Doc = Documents.Open(FileName:=m_strFileLocation & strDocumentName, _
        AddToRecentFiles:=False, Visible:=False)
    With m_Doc

        ' Set values for CustomDocumentProperties.
        .CustomDocumentProperties("ProjectName").Value = m_strProject
        .CustomDocumentProperties(strDocType & "Name").Value = g_strAryPlanner(2, intEntry)
        .CustomDocumentProperties(strDocType & "Number").Value = m_intChapter

        ' Set template.
        .UpdateStylesOnOpen = True
        .AttachedTemplate = Left(ThisDocument.Path, Len(ThisDocument.Path) - 5) _
            & "\Styles\StyleTemplates\" & Me.lblStyleTemplate
        .XMLSchemaReferences.AutomaticValidation = True
        .XMLSchemaReferences.AllowSaveAsXMLWithoutValidation = False

        ' Set chapter number for Chapter Outline 1 style.
        With .ListTemplates("ltChapterOutline").ListLevels(1)
            If strDocType = "Appendix" Then
                .NumberFormat = "Appendix %1:"
                .NumberStyle = wdListNumberStyleUppercaseLetter
            End If
            .StartAt = m_intChapter
            .LinkedStyle = "Chapter Outline 1"
        End With
        .Styles("Chapter Outline 1").LinkToListTemplate .ListTemplates("ltChapterOutline"), 1
    End With    ' m_Doc

I realize the following two of the lines of code are redundant:
Code:
.LinkedStyle = "Chapter Outline 1"
Code:
.Styles("Chapter Outline 1").LinkToListTemplate .ListTemplates("ltChapterOutline"), 1
I have tried it both ways, but neither way seems to work.

Any ideas? I've been butting heads with this problem for three days now.

[purple][ponder]— Artificial intelligence is no match for natural stupidity.[/purple]
 
OK, I've done some more work on this. It still isn't working, but I think I have identified the problem. Now I just need a solution.

Once again, what I'm trying to do is set the NumberFormat, NumberStyle, and StartAt properties of my ListTemplate. I'm using VBA in Word 2003.

I have tested and debugged my code and found the following: If I run this code from a regular module, it works fine. If, however, I run the code from an event handler of a UserForm (e.g. cmdOK_Click), nothing happens until I close the UserForm. The problem is, I need the UserForm to remain open until several documents have been 1) created, 2) opened, 3) modified, 4) saved, and 5) closed. Since my documents are being saved and closed while the UserForm is still open, I assume this is the reason the changes are not taking effect in the document.

Here is my code, shortened a bit to focus on the problem:
Code:
    ' Set chapter number for Chapter Outline 1 style.
    With ActiveDocument
        With .ListTemplates("ltChapterOutline").ListLevels(1)
            If strDocType = "Appendix" Then
                .NumberFormat = "Appendix %1:"
                .NumberStyle = wdListNumberStyleUppercaseLetter
            Else
                .NumberFormat = "Chapter %1:"
                .NumberStyle = wdListNumberStyleArabic
            End If
            .StartAt = m_intChapter
            .LinkedStyle = "Chapter Outline 1"
        End With
    End With    ' ActiveDocument

Anyone know of a solution I can try?

[red][banghead]— Artificial intelligence is no match for natural stupidity.[/red]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top