Building a WOrd Document

B

Bigfoot17

I have done all of my VBA programming from Excel. Now I haev a task in Word
and feel like a fish out of water!

Point me in the right direction(s), please.

I have a document which goes on for several pages. Let's call them "Rules
1-30" with each rule a paragraph or two. What I'd like to do is present a
dialog which prompts the user to select which rules/paragraphs to include,
and then present them a Word Document to save and print which has just the
rules they selected.

Of course there is much more involved but this would get me going. Any
links to examples is appreciated. Sites to visit,, etc.
 
J

Jay Freedman

I have done all of my VBA programming from Excel. Now I haev a task in Word
and feel like a fish out of water!

Point me in the right direction(s), please.

I have a document which goes on for several pages. Let's call them "Rules
1-30" with each rule a paragraph or two. What I'd like to do is present a
dialog which prompts the user to select which rules/paragraphs to include,
and then present them a Word Document to save and print which has just the
rules they selected.

Of course there is much more involved but this would get me going. Any
links to examples is appreciated. Sites to visit,, etc.

The "dialog" you describe is what Word calls a UserForm. There's a short
tutorial at http://www.word.mvps.org/FAQs/Userforms/CreateAUserForm.htm that
shows how to use text entry fields in a UserForm.

For what you describe, there are a couple of ways to construct the UserForm. One
way is to display 30 check boxes for the user to play with. Unless the
descriptions are very short, though, that would be pretty overwhelming. I think
I'd go with a multi-select list box instead. Set the following properties of the
list box:

MultiSelect = fmMultiSelectMulti
ListStyle = fmListStyleOption

Then each item in the list will have a check box next to it, and the user can
scroll through the list, checking and unchecking items.

In the template for the form, store the text of the rules as AutoText entries.
I'd recommend naming them all with the same beginning part followed by a number,
where the number corresponds to the position of the item in the list box
(starting with zero). That way, if the UserForm code determines that, say, items
5 and 12 are selected in the list box, then the AutoText entries Rule5 and
Rule12 should be inserted into the document.

For determining which items in the list box are selected, you run a loop over
all the entries and check the .Selected property of the list. This fragment
gives the idea:

Private Sub OKButton_Click()
Dim rng As Range
Dim idx As Long

Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
With ListBox1
For idx = 0 To .ListCount - 1
If .Selected(idx) Then
ActiveDocument.AttachedTemplate _
.AutoTextEntries("Rule" & idx).Insert Where:=rng
Set rng = ActiveDocument.Range
rng.Collapse wdCollapseEnd
End If
Next
End With

Me.Hide
End Sub

The UserForm could also include boxes for entering the other information needed
in the document, and all of the document can then be constructed from the one
input source.
 

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