Button adding Section of Text

  • Thread starter Thread starter Dax Arroway
  • Start date Start date
D

Dax Arroway

I'm looking for a way to add link or button to insert a group of text in my
Word document. I'm sure there's a way but I'm not sure of the approach.
Please help.

I have a form which describes customer problems by number. Some customers
only have one problem, others have up to 8. My form has a section for one
problem which says, "Problem #" followed by a dropdown menu to select the
problem number. This is followed by a form field where the client can
include a description of the problem (to control printed space this is inside
a table). And lastly is a list of options bulleted with checkboxes which
describe the follow up (need f/u, closed, needs resolution, etc.).

What I'm looking for is a button to follow this labled "Add New Problem"
which, when clicked would paste the above section again including text, form
fields, dropdown menues, and table. In other words, there's only one problem
section on the document but if a cusotmer had 5 problems, the client could
fill out the information for one problem, click the Add New Problem
button/link to add another problem section, fill that out, and click the
button again, and so on and so forth until all the problems were populated in
the document.
(I think that makes sense. Please let me know if more description is
needed.)

So, I'm looking for a way to do this. Any links to documents describing how
to do it would be helpful or the code itself. I'm guessing this could be
done with a Macro??? but I'm not the best Macro writter so any detailed help
is greatly appreciated. Maybe Visual Basic, but then again, I know very
(emphasis on the very) little about writting Visual Basic (last time I looked
at it we were on VB6 before the turn of the century).

I'm writting this in Word 03 in a Windows environment.

WhatChaGot? Thanks in advance for any help.
 
Set up the unfilled problem section and save it as an AutoText entry in the
template for the document. (Note: don't try to make multiple copies from a
document-- that's what a template is for.) See
http://www.word.mvps.org/FAQs/Customization/AutoText.htm for details.

Since you're using dropdowns, form fields, and checkboxes, I assume you're
protecting the document for forms. I'll also assume you named the AutoText entry
"NewProblem" although it could be any unique name up to 32 characters.

The macro is fairly simple. The task is to (a) unprotect the document, (b)
insert the AutoText entry at the end of the document, and (c) reprotect the
document.

Sub NewProblem()
Dim oRg As Range

With ActiveDocument
' unprotect
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

' insert the entry
Set oRg = .Range
oRg.Collapse wdCollapseEnd
.AttachedTemplate.AutoTextEntries("NewProblem").Insert _
Where:=oRg, RichText:=True

' reprotect
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

Finally, I'd suggest using a SEQ field instead of a dropdown to display the
problem number, eliminating one task for the user (and eliminating the chance
for them to pick the wrong number). To make sure the SEQ fields display
correctly, add this line just before the "reprotect" comment:

.Fields.Update


--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
Very nice. Yes, this is what I'm looking for, however, I actually want to
insert the text section into the middle of the document not at the bottom
(there are signature lines w/date lines and other items in the bottom that
need to stay in the bottom). I'm also looking for a way to call the Macro by
clicking on a line of text ("Add Problem") if that's possible. (I'm liking
the idea of text because I could format it as hidden text so when it's
printed, there won't be a Add Another Problem button at the bottoms of the
problems.) So the doc would look like this:

Header
Beginning text
Problem Section
Link to add another problem section
Bottom of doc text
Footer

You are correct in your assumptions below and I've named the Macro and
AutoText to match so that all works. I also password protected the document
and found how and where to include it in the code and also a way of
protecting the code from prying eyes. That's all good. If I can get the
positioning down, I'll be set. Something like: insert.autotext
where=link"Add Problem" (I know that's totally wrong but you know what I
mean).

Thanks so much for helping me out! Love ya!
PS: Thanks also for the SEQ suggestion. That works great.
 
Ok, that can be done. It just gets a little more complicated.

First, insert a MacroButton field in a paragraph by itself, between the
point where the newest problem section will be added and the "bottom of doc
text". Look at
http://www.word.mvps.org/FAQs/TblsFldsFms/UsingMacroButton.htm for an
explanation. The display text of the MacroButton field will be the "Add
Problem" text, and the macro being called is the NewProblem macro. So the
field code should look like

{ MacroButton NewProblem "Add Problem"}

Next, select the MacroButton field and insert a bookmark -- let's say you
call it "AddProblemButton", although the name could be anything unique up to
32 characters.

Now into the macro editor...

In the NewProblem macro, replace the lines

Set oRg = .Range
oRg.Collapse wdCollapseEnd

with the lines

Set oRg = .Bookmarks("AddProblemButton").Range
oRg.Move wdCharacter, -2

That will put the range oRg into the empty paragraph before the MacroButton,
and that's where the AutoText entry will be inserted.

Now, by default a MacroButton requires a double-click to launch its macro,
but that's a problem in a protected form: The first click of the
double-click tries to select the MacroButton, but the form protection forces
the selection to the next form field, so the macro never fires. To avoid
this, copy the AutoExec macro from
http://www.word.mvps.org/FAQs/TblsFldsFms/HLinksInForms.htm and paste it
into the macro module in your template.

Finally, to hide the MacroButton when printing, set up your print macros
like this:

Sub FilePrint()
With ActiveDocument
.Bookmarks("AddProblemButton").Range.Font.Hidden = True
Dialogs(wdDialogFilePrint).Show
.Bookmarks("AddProblemButton").Range.Font.Hidden = False
End With
End Sub

Sub FilePrintDefault()
With ActiveDocument
.Bookmarks("AddProblemButton").Range.Font.Hidden = True
.PrintOut Background:=False
.Bookmarks("AddProblemButton").Range.Font.Hidden = False
End With
End Sub

Oh, and one other thing: It is not necessary to name the macro the same as
the AutoText entry to make things work. I did that only to make it clear
that they're related, which might be a useful self-documentation if you come
back to the template after a few months.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
WOW!!! Look at you go! You're AWESOME! How long did it take you and how
much school did you have to go to to know all that? *hat's off to you* and
Thank You!

As I'm working with this I realize that I'm putting this template up on the
server and I'm wondering if the AutoText will travel with the document in the
template or is that a customization that only on my machine? If it's only on
my machine, is there a way of putting it in the Macro or getting it to travel
with the template?

And one final note, some of the users are using Word 03 and others are using
07. I'm designing in 03 because, well, you know how 07 is... so in order to
have more control over how things lay out I've decided to go that route, and
I'm wondering if it'll work the same when opened in 07. Do you know if it
will work or not and if not, is there a work around?
 
How long did it take? Well, I've been using Word since version 2.0, and I've
been answering questions in the newsgroups for around 10 years... and I have the
benefit of the knowledge all the other Word MVPs share. But it's more about
interest than longevity. Hang around here for a year or two and you can be an
expert, too.

Yes, the AutoText will stay in the template (but be certain you stored it in the
template for the form, not in your Normal.dot template; the "Look in" dropdown
in the Insert > AutoText dialog controls that) and will be available to anyone
who bases a document on the template as long as they're connected to the server.
The same is true of the macros in the template.

I've checked my copy of the template in Word 2007 and it works. There are some
things where Word 2003 macros and such don't work any more -- or don't work the
same way -- in Word 2007, but this one looks OK.
 
Hi Jay,
I'm back at this again. I have (hopefully) 2 easy questions.

First, the print macro stops because it says that the bookmark that the
macro trying to access is in a protected part of the document. So, how do I
add unlock and re-lock to the print macro or is there a way to unlock the
"button".

I'm also using a RunSpellCheck macro which I'm firing with the same method
(macrobutton) which I don't want to print. So, second question is do I have
to write a separate print macro for the RunSpellCheckButton bookmark or
should I combine them somehow?
 
Ack, you caught me. Use these macros instead (you'll recognize the added parts
from the macro that started all this), which also answers your second question:

Sub FilePrint()
With ActiveDocument
' unprotect
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

.Bookmarks("AddProblemButton").Range.Font.Hidden = True
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = True
Dialogs(wdDialogFilePrint).Show
.Bookmarks("AddProblemButton").Range.Font.Hidden = False
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = False

' reprotect
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

Sub FilePrintDefault()
With ActiveDocument
' unprotect
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

.Bookmarks("AddProblemButton").Range.Font.Hidden = True
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = True
.PrintOut Background:=False
.Bookmarks("AddProblemButton").Range.Font.Hidden = False
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = False

' reprotect
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so all
may benefit.
 
Sorry for continuing to throw things in but there's a password.

Let me give it a crack. So would:
----------------------
Sub FilePrint()
With ActiveDocument
' unprotect
If .ProtectionType <> wdNoProtection Then
.Unprotect
End If

.Bookmarks("AddProblemButton").Range.Font.Hidden = True
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = True
Dialogs(wdDialogFilePrint).Show
.Bookmarks("AddProblemButton").Range.Font.Hidden = False
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = False

' reprotect
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True
End With
End Sub

Be changed to:
--------------------------
Sub FilePrint()
With ActiveDocument
' unprotect
If .ProtectionType <> wdNoProtection Then
.Unprotect
else oDoc.Unprotect Password:="<password>"
End If

.Bookmarks("AddProblemButton").Range.Font.Hidden = True
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = True
Dialogs(wdDialogFilePrint).Show
.Bookmarks("AddProblemButton").Range.Font.Hidden = False
.Bookmarks("RunSpellCheckButton").Range.Font.Hidden = False

' reprotect
.Protect Type:=wdAllowOnlyFormFields, NoReset:=True,
Password="<password>"
End With
End Sub

Close? No cigar? What? *smile*

Also, since I'm posting this up on the Server, some clients are on 07 and
others on 03 with agency predefined Macro Security Level set at High by
default on install--I wish there were a Macro that would set the security
level at "accept for this doc only" and then reset to whatever it was. You
wouldn't have a magic macro for that would ya? No? You know you could make
millions of dollars if you could write one! *laughing*
 
No quite. You got the Protect part at the end correct, but the Unprotect
should just be this:

' unprotect
If .ProtectionType <> wdNoProtection Then
.Unprotect Password:="<password>"
End If

What this says is "if the document is protected (that is, the protection
type is NOT 'no protection') then unprotect it, using the password; but if
the user (probably just you as the administrator) has already turned off
forms protection, don't do anything, because trying to unprotect a
not-protected document will cause an error."

As for the macro security level, the only way to get the document
automatically enabled is to digitally sign the macro project with a security
certificate. Check with your IT staff to see if they can supply you with a
code-signing certificate -- it's pretty trivial if there's an in-house
certificate server, but expensive and a PITA if you have to go to one of the
commercial security companies like VeriSign.
 
You Da Man! That should do it for this issue (I think). Thank you so much,
REALLY, for all your hard work and advice. It's been invaluable! Please
don't stop.
Best Regards,
Dax
 

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

Back
Top