building a document from template

B

bryan

I want to populate a document based upon a selection from a userform. I
Thought of using 1 formfield however I have some checkboxs to insert as well.

A constant on the template is:
Dear Agent;
For simplicity let's say my uerform has 2 selections, A and B.
If A is chosen I want to insert:
We received a request to .......
If B is chosen I want to insert:
In order to process this please provide;
<tab>The last update for:
<tab>Plumbing _________
<tab>Roofing _________

<bullet> Are there other modifications? <checkbox> - Yes, how many?
<checkbox> - No

<tab><bold>I declare the above........


I look forward to insight...

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

I would use a insert the text into the range of a bookmark. For Case B, you
could have the block of text that you want to insert predefined as autotext
and inset that..

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

bryan

I have used something like:
ActiveDocument.FormFields("LOB").Result = "Commercial Fire"

So for A it would be similiar to this other than using a range so that I can
use more than 255 characters, right?
Like:
str1 = "We received a request to........"
ActiveDocument.Bookmarks("Text1").Range.Fields(1).Result.Text = str1

I'm still confused on B though.
1) I have not created autotext before and
2) How would you insert a checkbox form filed
Would the checkbox be part of the autotext?

This aspect is new to me so as much help as possible would be appreciated.
As stated I'm trying to bulid one document depending on criteria of selection
and insert this next line after the Dear Agent,

Thanks again,
Bryan
 
B

bryan

I have created the autotext.
Now how can I insert that programically after Dear Agent,

Thanks for all the insight,
Bryan
 
D

Doug Robbins - Word MVP

' Macro created 04/29/98 by Doug Robbins to replace formfield entry with its
autotext equivalent.

'

try = ActiveDocument.FormFields("Text1").Result

try = ActiveDocument.AttachedTemplate.AutoTextEntries(try).Value

ActiveDocument.FormFields("Text1").Result = try


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

bryan

Hi Doug,
So I need to have this as a formfield in order to insert?
I can't have 'B' within a formfield because then I wouldn't have check boxes
to check.
I thought you could search for "Dear Agent," and insert the autotext after
that right on the template and not within a formfield?

Thanks again,
Bryan
 
B

bryan

Let's step back and make this simple from the start and have both A and B as
autotext in this template because at some point I may add C and D.
How do I insert the autotext after Dear Agent, ?
I suppose I would need to unprotect, then insert, and then protect?

As much help as possible please...

Thanks,
Bryan
 
D

Doug Robbins - Word MVP

I think we should backup a bit.

Instead of using a formfield, use either a {Docvariable} field or just
insert the required text into the .Range of a bookmark.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP
 
B

bryan

I've done a lot with macros and more and more with the word object but, this
is a new aspect, which is good for learning more.
How would I do a .range if I want to insert the autotext after "Dear Agent,"
?
Assume I would need to unprotect and then protect as well ?

Thanks,
Bryan
 
G

Greg Maxey

Bryan,

You would put a bookmark after Dear Agent,

Lets call it "Bookmark1"

Then run code in your UserForm something like this:

Private Sub CommandButton1_Click()
Dim oRng As Word.Range
Set oRng = ActiveDocument.Bookmarks("Bookmark1").Range
Select Case Me.ListBox1.Value
Case Is = "A"
oRng.Text = "Blah, blah, blah 1, 2, 3"
Case Is = "B"
oRng.Text = "Blah, blah, blah A, B, C"
End Select
ActiveDocument.Bookmarks.Add "Bookmark1", oRng
Unload Me
End Sub

Private Sub UserForm_Initialize()
Me.ListBox1.AddItem "A"
Me.ListBox1.AddItem "B"
End Sub




I've done a lot with macros and more and more with the word object
but, this is a new aspect, which is good for learning more.
How would I do a .range if I want to insert the autotext after "Dear
Agent," ?
Assume I would need to unprotect and then protect as well ?

Thanks,
Bryan

--
Greg Maxey - Word MVP

My web site http://gregmaxey.mvps.org


McCain/Palin '08 !!!
 
B

bryan

I would prefer not to use a bookmark as that is a form field, correct?
I want to insert autotext 1 line down from Dear Agent,
I could have multiple autotext within this template and 1 has a fromfiled,
and 4 checkboxes.
For simplicity I used A and B.
If B is selected then the verbage is:
In order to process this please provide:
<tab>Plumbing ________
<tab>Roofing _________

<bullet> Are there other modifications? <checkbox> - Yes, how many?
<checkbox> - No

<tab><bold>I declare the above <formfield>........

So I will create all autotext in this template.
How can I insert the autotext 1 line down from "Dear Agent." ?

Thanks,
Bryan
 
G

Graham Mayor

In this context the bookmark would simply be a bookmarked location and not a
field. You would have to unlock the form with the macro to write the
autotext into the bookmark range. The work has already been done for you by
Doug and Greg, but the following may help clarify

Dim rAtext As Range
Dim bProtected As Boolean

'Unprotect the file
If ActiveDocument.ProtectionType <> wdNoProtection Then
bProtected = True
ActiveDocument.Unprotect Password:=""
End If
With ActiveDocument
Set rAtext = .Bookmarks("Bookmark1").Range
rAtext.Text = .FormFields("Text1").Result
rAtext = .AttachedTemplate.AutoTextEntries(rAtext).Value
.Bookmarks.Add "Bookmark1", rAtext
End With
If bProtected = True Then
ActiveDocument.Protect _
Type:=wdAllowOnlyFormFields, NoReset:=True, Password:=""
End If

This inserts the autotext named in form field Text1 in the document at the
location indicated by the bookmark Bookmark1. The form is unlocked to allow
the insertion then locked again. This code can be adapted to the userform
suggested by Greg if you prefer, or you can simply forget the autotext and
use his code to insert the texts directly.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

Got it. Thanks to all for your help. I appreciate the time all of you take to
help others. This forum is fantastic!

Thanks,
Bryan
 
B

bryan

Graham,
I tried your code but, t does not work. I inserted a bookmark1 and a text1

I used the following code and it works:
Dim bmRange As Range
Set bmRange = ActiveDocument.Bookmarks("myBookmark").Range

'''Insert AutoText
ActiveDocument.AttachedTemplate.AutoTextEntries(1).Insert Where:=bmRange,
RichText:=True

ActiveDocument.Bookmarks.Add _
Name:="myBookmark", _
Range:=bmRange

I want to try and understand the difference
 
G

Graham Mayor

In what way does it 'not work'? You have not said how you are using it. My
example works with a protected form and inserts the autotext entry with the
name that matches exactly the text entered in the Text1 form field and will
fail if that entry does not exist. Yours inserts the first autotext entry at
the bookmark.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
Graham Mayor - Word MVP


<>>< ><<> ><<> <>>< ><<> <>>< <>><<>
 
B

bryan

Thanks for explaining.
Having just a bookmark for this does not work as I have a formfield and 2
checkboxes which are part of the "HO H36" autotext I am adding.
If I insert this autotext to a bookmark I do not have the formfileds
available at all, I get * FORMTEXT ******* in place of the formfield which is
part of "HO H36" autotext.
If I insert this autotext into a formfield then I have I have it available
to populate and the checkboxes available as well.
Here is what I am using which works, mybookmark is a formfield on the
template:

Set UF = New UserForm1
UF.Show
strrec = UF.ComboBox1.Text
Set UF = Nothing
If InStr(1, strrec, "Pet") > 0 Then
strtext = Left(strrec, 3)
ElseIf InStr(1, strrec, "HOL") > 0 Then
strtext = Left(strrec, 7)
Else
strtext = Left(strrec, 6)
End If

Dim bmRange As Range
Set bmRange = ActiveDocument.Bookmarks("myBookmark").Range

'''Insert AutoText
ActiveDocument.AttachedTemplate.AutoTextEntries(strtext).Insert
Where:=bmRange, RichText:=True
ActiveDocument.Bookmarks.Add _
Name:="myBookmark", _
Range:=bmRange

If there is something that you suggest I do different, please let me know. I
am open to whatever works best.
As mentioned, I actually have 4 differnet autotext, 3 are just text. The "HO
H36" has a formfield and 2 checkboxes.

Thanks,
Bryan
 
B

bryan

Well a new twist thrown into this. Rahter than a combo box to select one they
want to be able to select multiple from a list. I have done the list box
before with adding additional documents but, this one is to keep adding to
the document.
I have the autotext in a string and am extrapolating that but, if I select 2
it overwrites.
How can I use this code to append rather than overwrite?

Dim bmRange As Range
Set bmRange = ActiveDocument.Bookmarks("myBookmark").Range

For Each Item In array2
If InStr(1, Item, "Pet") > 0 Then
strtext = Left(Item, 3)
ElseIf InStr(1, Item, "HOL") > 0 Then
strtext = Left(Item, 7)
Else
strtext = Left(Item, 6)
End If
'''Insert AutoText
ActiveDocument.AttachedTemplate.AutoTextEntries(strtext).Insert
Where:=bmRange, RichText:=True
ActiveDocument.Bookmarks.Add _
Name:="myBookmark", _
Range:=bmRange

Next

Thanks,
Bryan
 
B

bryan

Addendum:
I cannot just write to the formfield as each is more than 255 in length and
I have 1 autotext with a formfiled and 2 checkboxes.

Appreciate the help,
Bryan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top