none
Difficulty inserting Title page building block into document RRS feed

  • Question

  • Hi, I'm fairly new to VSTO programming.

     

    I have created a custom template which contains a large amount of building blocks. Through a ribbon user interface I want users to be able of inserting building blocks into the document.

    I have managed to add the template containing the BB's to the application, however loading BB's from the template isn't as straightforward as I had expected.

     

            Dim wordApp As Word.Application
            Dim wordDoc As Word.Document
            Dim wordTemplate As Word.Template
            Dim tempTemplate As Word.Template
            Dim wordBuildingBlock As Word.BuildingBlock
            Dim templatePath As String
    
            wordApp = DirectCast(GetObject(,"Word.Application"), Word.Application)
            wordDoc = wordApp.ActiveDocument()
    
            templatePath = "C:\Users\Username\Documents\Visual Studio 2010\Company name\Company name Building Blocks.dotx"
            wordApp.AddIns.Add(templatePath)
            wordApp.Templates.LoadBuildingBlocks()
    
            'select the template containing the BB's
            For Each tempTemplate In wordApp.Templates
                If (tempTemplate.FullName = templatePath) Then
                    wordTemplate = tempTemplate
                End If
            Next
    
    
            Dim paramBBType As WdBuildingBlockTypes = WdBuildingBlockTypes.wdTypeCoverPage
            Dim paramBBCategory As String = "Pages"
            Dim paramBBname As String = "Title Page"
    
            wordBuildingBlock = wordTemplate.BuildingBlockTypes.Item(paramBBType) _
            .Categories.Item(paramBBCategory).BuildingBlocks.Item(paramBBname).Insert(wordApp.ActiveDocument.Paragraphs(1).Range)
    

    This code crashes at the buildingblock insert statement, without generating an error, and without inserting the buildingblock.

     

    If anyone has any suggestions on how to do this in VSTO, it would be greatly appreciated.

     


    • Edited by trVrie Tuesday, November 1, 2011 3:30 PM
    Tuesday, November 1, 2011 3:24 PM

Answers

  • Put the insert code into a Try/Catch and use a MsgBox to display the error.

            Try
                wordBuildingBlock = wordTemplate.BuildingBlockTypes.Item(paramBBType) _
                        .Categories.Item(paramBBCategory).BuildingBlocks.Item(paramBBname).Insert(wordApp.ActiveDocument.Paragraphs(1).Range)
            Catch
                MsgBox("Insert Error: " & ex.Message, vbCritical, "My Message")
            End Try
    
    

     


    Kind Regards, Rich ... http://greatcirclelearning.com
    Wednesday, November 2, 2011 11:05 AM