Great thanks Paul. I agree.
Sebastian, what I use to do was either bring in the content text from a document resource file to a bookmark location. Or Autotexts are great for content you don't change that you bring in conditionally. So the resource files are for text that may be edited from time to time, autotexts or quickparts are for building blocks.
This stub gives you an idea what you can do. Capture your checkbox events then based on your logic bring in the elements or remove them. (I've just shown bringing them in.)
Unfortunately, Bookmarks can easily be deleted or typed in accidentally in paragraphs.
If you can use a table cell and put the bookmark around the table cell, that is better, i.e. not so easy to accidentally delete.
Sub ImportDocElementsStub()
Dim bCheckBoxA As Boolean, bCheckBoxB As Boolean, bCheckBoxC As Boolean
Dim lRangeStart As Long, lRangeEnd As Long
Dim sResourceFile As String
Dim oDoc As Word.Document
Dim oTemplate As Word.Template
Dim oRange As Word.Range
Const cBookmarkForAutoText As String = "AutotextElement"
Const cBookmarkForResource As String = "ResourceElement"
' Reference if you need it may be to specific document
Set oDoc = ActiveDocument
' You will need to reference the template with the autotexts
' whether that be the template attached to the document
' or a global template
Set oTemplate = oDoc.AttachedTemplate
' The path to your resource file with the text formatted and ready
sResourceFile = "C:\ResourceFile.docx"
' Determine if your checkboxes have been checked or not
bCheckBoxA = True
bCheckBoxB = True
bCheckBoxC = True
If bCheckBoxA Then
Set oRange = oDoc.Bookmarks(cBookmarkForAutoText).Range
' Sample of how to insert a building block
Set oRange = oTemplate.BuildingBlockEntries(cBookmarkForAutoTex t).Insert _
(oRange, RichText:=True)
' Add the bookmark back to the range
oRange.Bookmarks.Add "AutoTextElement"
End If
' Bookmark around a paragraph - NOT IDEAL
If bCheckBoxB Then
lRangeStart = oDoc.Bookmarks(cBookmarkForResource).Start
' So you don't loose the bookmark if around a paragraph
lRangeEnd = oDoc.Bookmarks(cBookmarkForResource).End - 1
' reference to range for text
Set oRange = oDoc.Range(lRangeStart, lRangeEnd)
' Either you can open and copy the data
' or insertfile to a range
oRange.InsertFile FileName:=sResourceFile
End If
'if you can use a table cell, do so!
If bCheckBoxC Then
' Bookmark around a single cell table
Set oRange = oDoc.Bookmarks("TableCellBookmark").Range
oRange.InsertFile sResourceFile
End If
Set oRange = Nothing
Set oTemplate = Nothing
Set oDoc = Nothing
End Sub
|