Associates said:
Thank you, Jean-Guy and Fumei for your replies.
Fumei, you're right. I actually use that code to insert the AutoText at the
selection on the userforms. All you said was exactly what's on my mind. I
know that what I'm asking is not gonna be easy one (at least to me) to carry
out. One thing i know is that they are placed or inserted in the document in
the following order. For example,
1. executive summary
2. sections
3. bibliography
4. methodology
5. glossary
So, on the userform, I have them listed in that order. Users can tick the
ones they want to have in their report. However, there might be a case where
they may change their mind later after selecting executive summary, sections,
and methodology at the start. They can come back to the userform that would
disable those that are already ticked previously (to prevent them from
deleting the autotext - which could create a similar worse headache). They
would want to have bibliography to be included in the report. I agree with
Fumei that how the word would know that here is the chunk for executive
summary, the next chunk for sections. It would be nice to have some sort of
mark attached to this chunks here to indicate here is the beginning and the
end of the chunk of executive summary.
I'm curious about how to use bookmarks as indicator.
Here is some sample code to get you going. Use the code on a blank document.
Use the Subs in order (A, B C). I guess you could do it step by step while
checking what happens in the document to see the effect of some of the lines.
Option Explicit
Const strTextFiller As String = "Some text to insert so as to create " _
& "paragraphs. Even if they are short ones. "
Const strBookName As String = "Book_"
Sub A_InsertText()
Dim rngText As Range
Set rngText = ActiveDocument.Range
With rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "1", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "2", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "3", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "5", rngText
.InsertParagraphAfter
.Collapse wdCollapseEnd
End With
End Sub
Sub B_DeleteText()
Dim rngDelete As Range
With ActiveDocument.Bookmarks
If .Exists("Book_3") Then
Set rngDelete = .Item("Book_3").Range
With rngDelete
.MoveEnd wdCharacter, 1
.Delete
End With
End If
End With
End Sub
Sub C_AddText()
Dim rngInsert As Range
With ActiveDocument.Bookmarks
If .Exists("Book_3") Then
Set rngInsert = .Item("Book_3").Range
ElseIf .Exists("Book_2") Then
Set rngInsert = .Item("Book_2").Range
ElseIf .Exists("Book_1") Then
Set rngInsert = .Item("Book_1").Range
Else
Set rngInsert = ActiveDocument.Range
rngInsert.Collapse wdCollapseStart
End If
End With
With rngInsert
.Collapse wdCollapseEnd
.InsertParagraphAfter
.Collapse wdCollapseEnd
.Text = strTextFiller
.Bookmarks.Add strBookName & "4", rngInsert
End With
End Sub
Of course, you would have to modify the code to work with AutoText and a
userform. The "A_..." sub would be run when clicking on OK the first time the
userform is run.
The "B_..." and "C_..." subs would be run every time after that.
I did not include code you would need to run before displaying the userform
the second time onward. This would check the existence of bookmarks (Like in
the "B_,.." sub) and for each bookmark found, the corresponding checkbox on
the userform would be set to True.
You need to be carful with bookmarks because as users work in the document,
they migth mess up bookmark boundaries... Make sure you have some visual
clues indicating the beginning and end of each section, and then train users
not to mess with those areas...
Just to be on the safe side, I would save the document before executing the
userform the second time around (and every time after that as well). So, if
the user has messed up the bookmarks, the garbge the code might produce can
be easily fixed by quitting without saving and reverting to the last saved
version...