mercredi 16 septembre 2015

VBA Word: Insert custom textbox building blocks via Userform into template at specific locations

I am trying to create a template for a report that will prompt the user via Userfrom to select which boilerplate text items to add to the document

  1. I have created about 40 building blocks (text boxes containing boilerplate text, all formatted as they should display) which have been saved to my custom made template.
  2. I have created the Userform in VBA which prompts the user the select which items they wish to add to the template (items are displayed with a checkbox which user can check to select item)
  3. There is content in the template that should remain at the top and bottom of the document, so the inserted building blocks need to go in between these

I can't figure out the best way to insert the text into the desired locations, because which items the user chooses will be variable. How would I go about inserting the text the user chooses each into its own paragraph, one after the other, beginning after the static text at the top of the document and before the static text at the bottom of the document?

Here is the code I have so far (it is not complete):

Private Sub cmdDone_Click()

 Dim oTmp As Template
  Set oTmp = ActiveDocument.AttachedTemplate
  Dim oRng As Word.Range
     If CheckBox1.Value = True Then
         Set oRng = oTmp.BuildingBlockTypes(wdTypeTextBox).Categories("category1"). _
                  BuildingBlocks("category1bb1").Insert(ActiveDocument.Bookmarks("Entry1").Range)
         ActiveDocument.Bookmarks.Add "Entry1", oRng

     If CheckBox2.Value = True Then
         Set oRng = oTmp.BuildingBlockTypes(wdTypeTextBox).Categories("category2"). _
                  BuildingBlocks("category2bb1").Insert(ActiveDocument.Bookmarks("Entry1").Range)
         ActiveDocument.Bookmarks.Add "Entry1", oRng    

       ' (ETC)

 Exit Sub
End Sub




Aucun commentaire:

Enregistrer un commentaire