> Hello everyone,
>
[quoted text clipped - 27 lines]
> sense, reason, and intellect has intended us to forgo their use." -- Galileo
> Galilei
Charles,
This might be one way of achieving your objective:
1. Insert bookmarks into your template, one bookmark for each
section. The bookmark's range should cover all the text of that
section.
2. When the user clicks the Preview button on the user form, run
code that will open the template invisibly and open a blank document
invisibly.
3. Copy appropriate text from the template to the blank document
according to the user's request.
4. Close the template.
5. Make blank document visible.
Some air code follows.
As an alternative, you may want to use the Copy, Paste, or
PasteSpecial methods to copy the text from the template to the blank
document.
Geoff
Sub GetText()
' Get text from some bookmark ranges in a template
' and add that text to a blank document.
Dim objDOC1 As Word.Document
Dim objDOC2 As Word.Document
Dim objRNG2 As Word.Range
Dim strText As String
Dim RetVal As Boolean
' 1. Open template invisibly:
Set objDOC1 = Application.Documents.Add("TemplateName", , , False)
' 2. Open a blank document invisibly:
Set objDOC2 = Application.Documents.Add(, , , False)
' 3. Set a range object to beginning of blank document:
Set objRNG2 = objDOC2.Range
objRNG2.Collapse wdCollapseStart
' 4. Get text from a bookmark called "MyBookmark" in
' the template by calling the GetBookmarkRangeText
' function (below) and put text in the strText variable
' (RetVal is TRUE if bookmark exists):
RetVal = GetBookmarkRangeText(objDOC1, "MyBookmark", strText)
' 5. If the bookmark was found, add the text to the
' blank document:
If RetVal Then GoSub AddTextToBlankDoc
' 6. Repeat steps 4-5 here for each section the user has
' selected in the user form.
' 7. Close template:
objDOC1.Close wdDoNotSaveChanges
' 8. Show document:
objDOC2.Activate
' 9. Clean up:
Set objDOC1 = Nothing
Set objDOC2 = Nothing
Set objRNG2 = Nothing
Bye:
Exit Sub
AddTextToBlankDoc:
' Add a couple of blank lines to text obtained
' from template:
strText = strText & vbNewLine & vbNewLine
' Insert text obtained from template into blank doc:
objRNG2.Text = strText
' Go back:
Return
End Sub
Function GetBookmarkRangeText( _
objDoc As Word.Document, _
strBookmarkName As String, _
strText As String) As Boolean
' Returns:
' TRUE if bookmark exists; otherwise FALSE.
' Out:
' If bookmark exists, strText will contain
' the bookmark range's text.
Dim objRNG As Word.Range
' Ensure bookmark exists:
If Not ActiveDocument.Bookmarks.Exists(strBookmarkName) Then
GoTo InvalidBookmark
End If
' Point to the bookmark's range:
Set objRNG = ActiveDocument.Bookmarks("MyBookmark").Range
' Set the retrieval mode for the range's Text property:
With objRNG.TextRetrievalMode
.IncludeHiddenText = True
.IncludeFieldCodes = True
End With
' Put text into strText:
strText = objRNG.Text
' Set this function's return value to TRUE:
GetBookmarkRangeText = True
' Clean up:
Set objRNG = Nothing
Bye:
Exit Function
InvalidBookmark:
MsgBox "Invalid bookmark", vbOKOnly, strBookmarkName
GoTo Bye
End Function
Geoff - 04 Nov 2004 00:02 GMT
Correction:
Obviously the function should use objDOC, not Activedocument as in:
Set objRNG = objDOC.Bookmarks("MyBookmark").Range
Geoff