PDA

View Full Version : INSERTING AUTOTEXT VIA RADIO BUTTONS



Greg
11-18-2006, 06:06 PM
Hi All,

You have previously helped me to refine my userforms but I now wish to improve them further.

My plan is to add 4 or 5 radio buttons, each one bringing up some specific text. I have some documents prepared by others that achieve this by calling up autotext entries. If there is another way please let me know.

Anyway, the code in relation those other documents seems overly complex. I wonder if it was badly written or whether it is indeed a complex procedure.

My attempts to adapt the code in those other documents for my own use has met with only limited success.

So, would one of you care to walk me through the process of adding some radio buttons to my userform, then some simple code to bring up the autotext entry of my choice by selecting one of those buttons.

I understand that autotext is stored in the normal.dot template but I wonder if it can be stored in any other template?

I have been struggling with these problems for quite some time and I would therefore be grateful if one of you could put me out of my misery.

Regards,

Greg.

lucas
11-19-2006, 09:41 AM
There are many easy methods for adding predefined text and formatting to the insertion point of your document...you could use a simple menu that is attached to a specific document only...see attached and look for the "insert text" menu at the top of the page.

lucas
11-19-2006, 10:05 AM
and since you seem to want to call a list of available text I will send a version that calls text from an excel file that Malcolm posted a while back. I added a form with one radio button to call the form that opens the excel file....you could use multiple excel files and forms to attach to different radio buttons....one excel file and form in the word doc for a specific set of text, etc..
file path for this is currently set for
FName = "C:\AAA\Data1.xls"
you will have to change that in the userform1 initialize statement if you put it somewhere else.....only the excel file has to be located there.

Note that it takes 2 or 3 seconds after you click the radio button for the selection userform to open so wait for it.

Greg
11-19-2006, 05:28 PM
Thanks Lucas,

The first one looks particulary useful. Can that functionality be easily added to my userform so that the different text entries can be selected via radio buttons?

I'm not sure what to do with the second although a quick check of the code suggests it is password protected.

Greg.

lucas
11-19-2006, 09:15 PM
for the second one you have to put the excel file in a directory under C called AAA

C:\AAA\

if you don't have a C drive you will have to change the path in the code. No password protection on these files.

then run the vet doc and hit control F
that will open the userform with the radio button...click on the one radio button.

none of the code is password protected.

Greg
11-19-2006, 10:29 PM
Thanks Lucas,

The protection was on some of the other modules in that document.

Anyway, using the first alternative, can you explain how I might insert text via a radio button on my userform so that it goes into a single bookmark in the subject document?

The intention is to populate the document (using ref fields) with the chosen text in two other places. The precise location of the text in the document is important, hence the need for one bookmark capable of accepting whichever text is chosen.

As previously mentioned, I thought it may be best to call-up some autotext for this purpose but having seen your code I may be able to avoid doing it that way. Having said that, I think the autoxtext option is still worth considering.

Regards,

Greg.

fumei
11-20-2006, 06:19 AM
Steve. It would be better to use one instruction (a With Statement) rather than all those individual instructions. So instead of:Selection.TypeText Text:="Sincerely,"
Selection.TypeParagraph
Selection.TypeParagraph
Selection.Font.Bold = wdToggle
Selection.TypeText Text:="Your Company Name Here"
Selection.Font.Bold = wdToggle
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeParagraph
Selection.TypeText Text:="So and So"
Selection.TypeParagraph
Selection.TypeText Text:="Certified Know it all!"you could use:
With Selection
.TypeText Text:="Sincerely," & vbCrLf & vbCrLf
.Font.Bold = wdToggle
.TypeText Text:="Your Company Name Here"
.Font.Bold = wdToggle
.TypeText Text:=vbCrLf & vbCrLf & vbCrlf & vbCrLf & _
"So and So" & vbCrLf & "Certified Know it all!"
End WithEvery one of those Selection. instructions must be run through the compiler (which queries the properties and methods of the object). Using the one With statement, the code is parsed ONCE, and the object (the Selection) is queried ONCE.

Your code forces 13 queries to the object.

Greg. AutoText is stored in normal.dot by default. However, ANY template file (.DOT) can stored AutoText entries. In fact, it is a good idea to move autotext to anothe rtemplate, as there are commonly problems with normal.dot. Deletion of normal.dot is one of th efirst "fixes" support people do. If your autotext entries are only there...well they are toast.

I am not sure why you want to use radio buttons. I presume you mean option buttons. You could do that.

I have modified the menu.doc file that Steve posted. It is now menuGerry.DOT. So, a template. The entries that Steve had for signatures (Joe, and SoandSo) have been duplicated as autotext entries. They are available from a combobox on a userform. The userform is loaded from "Insert Text" on the top toolbar. This was the menu Steve had, it is now a macro to load the userform.

You COULD make the selection of an item from the combobox automatically be inserted at the Selection point. However, it works when you click the Insert button.

Notes:

1. Autotext can be inserted anywhere. The userform is non-modal, so you can move it around and put the cursor (Selection) anywhere you like.

2. I also put a demo of using a bookmark. You COULD have a combobox with the listing of all bookmarks in the document. As it is, I put in one bookmark. What is insert at that bookmark is the the AutoText Value, as represented by the AutoText Name that is in the combobox.

lucas
11-20-2006, 07:45 AM
Great ideas Gerry...thanks for stepping in and helping me out as Word is not my strong point. I was just trying to give Greg some ideas and see if we could figure out what he's trying to do exactly.

fumei
11-20-2006, 08:15 AM
Hey, we're all a team here. I too am trying to give some ideas.

Greg, could you tell us more about what exactly you want to do? I am unsure about the radio button thing.

Greg
11-20-2006, 05:48 PM
Thanks Gerry and Lucas.

Gerry, your solution works very well until I try to adapt it to my own userform and document.

Rather than try to explain my processes, I have attached my document which should speak for itself. However, I supect that your code is being over-ridden by my pre-existing code, much of which was provided by you so it should look familiar.

This document is not currently a document template but I was unsuccessful with it even when it was **.dot file.

Essentially, I need to retain the functionality of the present document and the exiting userform whilst still being able to insert various Indorsements into a bookmark (named "Indorsements") following which a ref field (yet to be inserted) will populate the document in two other places.

If you can give me a little more guidance, I'll have another go at it.

Regards,

Greg.

fumei
11-21-2006, 03:32 AM
The attached document has no code, or userform in it. It is completely blank.

The document has lots of stuff in it, but there is NO code, or userform in this file.

Greg
11-21-2006, 05:58 AM
Hmmm, I'll try again.

Greg.

Greg
11-21-2006, 07:14 AM
Hi again Gerry and Lucas,

The attached document started life as a much more complicated document using Word Basic.

It is has since been chopped up by me and is now only a shadow of its former self.

This was my attempt to distil its ingredients into something useful for my own work.

Alas, the task is bigger than I had expected and as you can see I am yet reduce it to a managable size.

Anyway, it may give you some idea of what I was hoping to achieve with my own userform and document.

I only wish that could understand it (and rewrite it).

Regards,

Greg.

fumei
11-21-2006, 09:30 AM
And you can certainly do it. This is not beyond your skills Greg. It may - and in fact I have no doubt it would - try your patience though....

Looking at that WordBasic code....shudder...I had forgotten. It is much cleaner, and I think, much easier, with VBA.

This is not technically difficult really. And as I said, you have the skills, mostly. The hardest thing here is working out the logic flow.

Other than that, it simply takes text and puts into bookmarks, which you know how to do.

Will it take a fair bit of work to get a userform with all the elements working correctly? Yes. Is it hard to do? No, not really. It will be a bit tedious though, because you will have to make sure it works perfectly.

There is a major error in the Wordbasic dialogs. When it asks do you want another indorsement, if you click Yes...it returns you to another indorsement. If you click No...it returns you to another indorsement. Good choice that.

Oh...and the ReStart button...crashes Word for me. Nice. Sweet.

May I suggest something? Do not put all this stuff on your existing userform. Could you? Yes. You could redo your existing userform and put your current stuff on one page of a MultiPage, this indorsement stuff on another Page. But....

Methinks the code is going to get fairly long. Try to get it, and keep it, in edible chunks.

A procedure that ONLY determines which radio button is selected. Nothing else. It does not do any other action.

A procedure that ONLY determines what captions to have for the radio buttons.

A procedure that ONLY sets the text captions for the textboxes. They change depending on what radio button was selected. There are still only two textboxes, but the title (the label above them) changes depending on the radio button selected.

Do not try and put all the actions into one long procedure. I can pretty much guarantee that you will get lost, and screwed up trying to debug and figure what is wrong...when it goes wrong....which it will.

Chunk up your code as much as possibile, and Call the procedures as needed.Function SelectedButton() As String
Dim oCtl As Control
' NAME your frame that holds the radio buttons!
For Each oCtl in fraRadioButtons.Controls
If oCtl = True Then
SelectedButton = oCtl.Caption
End If
Next
End FunctionThis is just an example. A Function that does ONE thing - determines which radio button is selected and gets the name of that button.Sub SetTextboxLabels()
Select Case SelectedButton
Case "Damages Claim"
lbTextInput1.Caption = "Name of Strata"
lbTextInput1.Caption = "Strata Plan Number"
Case "Loan Agreement"
lbTextInput1.Caption = "Name of the Corporation"
lbTextInput1.Caption = "Australian Company Number"
' etc etc
End Select
End SubI hope you are following this.

Calling SetTextboxLabels does ONE thing - sets the textbox labels based on what button was selected. It does that by calling another procedure - the Function SelectedButton that does ONE thing - gets the string of the selected button Caption.

Is this do-able? Absolutely. Can you do it? I think you can.

I strongly suggest you make a logic table. List every control. Beside each one, write out - on paper - what should happen for each and every choice.

If radio button Sale of Goods is selected, THEN.......

If Cheques Act selected, THEN.....

If OK button clicked, THEN.....

If Previous button clicked, THEN....

Go through and write down every single step, no matter how small. EVERY step. Do not make ANY assumptions. You will find that many control actions will be the same, or similar.

For example:

Shire Rates, Loan Agreement and Cheques Act all have the same labels (titles) for the two textboxes: Name of Corporation, and Australian Company Number.Sub SetTextboxLabels()
Select Case SelectedButton ' call function to get selected button
Case "Damages Claim"
lbTextInput1.Caption = "Name of Strata"
lbTextInput1.Caption = "Strata Plan Number"
Case "Shire Rates", "Loan Agreement", "Cheque Act"
lbTextInput1.Caption = "Name of the Corporation"
lbTextInput1.Caption = "Australian Company Number"Are you following this?

Technically, coding wise, this is well within your grasp. It is not difficult coding. It is the logic flow that will take up the work. If you have a full grasp - WRITTEN DOWN - of that, you can do this.

Lots of people can code, but to code well nothing beats knowing, precisely, in detail, what it is you want it to do.

Are you having fun yet?

Greg
11-21-2006, 10:38 PM
Hi Gerry,

Thanks for your help and encouragement.

The Word Basic errors you referred to are not present in the original, much larger document. The original was a real monster until I broke it down to a more manageable size. Unfortunately I managed to spoil parts of it in the process.

You allude to the difficulty of putting everything on one page of a multiform. Rather than do that, is there any reason why I can't keep my existing userform and add a command button that calls up the Word Basic multiform (or similar) from which I can then choose an indorsement? The indorsements would, of course, be held in the .dot template as autotext.

Alternativley, if we return for a moment to my userform with the drop down list, were you able to see where I had gone wrong with the code you provided in the MenuGerry document shown above? If that can be made to work, it may avoid having to persevere with the multiform.

Or, having regard to the number of indorsements, do you advise against using the drop down list?

Regards,

Greg.

fumei
11-22-2006, 02:24 AM
Alternativley, if we return for a moment to my userform with the drop down list, were you able to see where I had gone wrong with the code you provided in the MenuGerry document shown above? If that can be made to work, it may avoid having to persevere with the multiform.

Or, having regard to the number of indorsements, do you advise against using the drop down list?Please explain. What went wrong? I don't know what went wrong.

Yes, there is a dropdown. There is nothing in the dropdown. What "did not work"?

You could expand the userform to handle indorsements. I don't see why not.

Greg
11-27-2006, 07:09 AM
Hi Gerry,

I have been messing around with your userform but with only limited success.

My problem is this: If I enter text into a textbox I want that same text to re-appear the next time I bring up the userform.

I don't have this problem in the other userforms I have created recently but in this instance I can't achieve that result. If I do, the autotext functionality stops working.

I would grateful if you were to peruse my changes and tell where I have erred.

Many thanks,

Greg.

Greg
11-27-2006, 07:12 AM
Gerry,

If that document wasn't any help another one is attached.

Greg.

fumei
11-27-2006, 07:51 AM
You can use document variables to store whatever you had in the textbox. Pull the value of the variable on userform initialize and put it in the textbox.

However, WHY do you want to do that? It seems a little odd.

Greg
11-27-2006, 05:34 PM
Hi Gerry,

I often amend our documents during the drafting process and it would be a real nuisance if I had to re-enter all the text and other variables just to make one minor change. This is not an issue if text already entered re-appears in the userform each time I bring it up. Without that feature, the userform is fairly pointless.

In other words, if I create a document by entering information into 8 textboxes and I then realise that one of them contains an error, I need to be able to bring up the userform to correct that error without re-entering all of the other information as well.

If I only re-enter information into one text box (to amend it) and leave all the other text boxes blank, all the text previously entered into those other text boxes will be erased (except for the amended part).

Greg.

fumei
11-28-2006, 05:06 AM
OK. Fair enough. Then fill up the textboxes from with the text that was put into the bookmarks.

The useform puts text into bookmarks. It can just as easily prefill the textboxes with what is in the existing bookmarks.

You have already done this I believe.

Greg
11-28-2006, 05:43 PM
Hi again Gerry.

This brings me back to my original problem. I thought I had done everything correctly to retain the text in the bookmarks (and show the retained text in the userform) but something is obviously amiss. I don't know what it is and I was hoping you might identify the problem.

Can you see where I may have gone wrong?

By the way, has it stopped snowing yet?

Greg.

fumei
11-29-2006, 08:04 AM
I thought I had done everything correctly to retain the text in the bookmarks (and show the retained text in the userform)What on earth did you think that was???

Hmmm. maybe it was someone else, but I thought it was you.

OK. You want previous values - that were placed into bookmarks - put BACK into the textboxes when the userform is loaded again. Right?

So, how do you think you would do that?

OK. Fair enough. Then fill up the textboxes from with the text that was put into the bookmarks.

The useform puts text into bookmarks. It can just as easily prefill the textboxes with what is in the existing bookmarks.

fumei
11-29-2006, 08:10 AM
What I am saying is, work out the logic...what you want it to do.

You want the userform, when it loads to see if there is anything in the bookmarks. If there is, grab that and put it in the appropriate textbox on the userform.

You have no logic for this in the userform_initialize event.

Greg
11-29-2006, 05:20 PM
Okay, I have another look at it.

fumei
11-29-2006, 09:20 PM
Trying to find that thread.

In the Initialize event:

If bookmark.range.text <> "" then
userform.textbox.text = bookmark.range.text
End If

will load the userform textboxes with the text from the bookmarks, if any.

Greg
11-29-2006, 11:32 PM
Many thanks. I'll sort it out tonight.

Greg.

Greg
11-30-2006, 08:53 PM
Eureka. I got it.

The next challenge will be to get the autoext to accept some variables like dates or account numbers and so on. But that'll have to wait.

Many thanks for your help Gerry.