Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / November 2004

Tip: Looking for answers? Try searching our database.

"Merging" Templates and limiting views

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Charles Linquist - 01 Nov 2004 07:54 GMT
Hello everyone,

I have a document template that receives its input from a VBA Userform I've
created. I have a second template that is very similar to this first
template. In an effort to streamline things, I would like to combine these
two templates.

Then based on the user's responses in the dialog box I would like the
following to happen:

    1. Show them a print preview of only the sections that need to be
       printed.
    2. Allow them to close the print preview and make changes if necessary.
       Again showing only the sections that pertain to their responses.
    3. Print only the appropriate sections.

Secondly, is it possible to "name" a section? That way instead of section 1
I could refer to it as coverLetter or whatever I have chosen to name it.

Any assistance that can be provided is greatly appreciated. Please post to
the group, and to nntpgroups@mac.com

I am using Office 2000/Word 2000(v9.0.2720) and the OS is WIN2K SP4.

Again, thanks in advance.

Charles

"I do not feel obliged to believe that the same God who has endowed us with
sense, reason, and intellect has intended us to forgo their use." -- Galileo
Galilei
Geoff - 03 Nov 2004 23:39 GMT
> 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

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.