PDA

View Full Version : vba code to select predefined text blocks into word document



vsempoux
06-09-2009, 12:39 AM
Hello all,

I'm looking for a way to insert predefined text blocks into a word document. I'm working on word 2003.
Let me explain what i'm trying to achieve.

I would like to create a word template with a macro quicklink in the upper taskbar. When a user clicks on macro icon, a selection list should appear with different text blocks in it with checkboxes next to them. When a user selects multiple checkboxes and he clicks on ok, those selected textblocs should be inserted into the document.

I would prefer that all the textblocs that can be selected, are stored into the template. Due to security reasons, all our users don't have much priveliges on their workstation.

All help is very welcome.

lucas
06-09-2009, 06:24 AM
Gerry shared this with us a long time ago. The best way I know of is to have all the textblocks in the document and each block is bookmarked. Use the listbox to select the ones you want and all others are deleted. See attachment.

You will of course have to save this file as a template after you get the blocks in the bookmarks to use it.....

fumei
06-09-2009, 11:50 AM
In this case, this sounds EXACTLY like AutoText.

It sounds like an additive process, rather than a reductive process like my bookmark example.

AuotText can be stored in the template, although getting them there can be a bit of a pain, as the default location is Normal.dot.

However, I have a question.

"When a user selects multiple checkboxes and he clicks on ok, those selected textblocs should be inserted into the document."

OK, but.....inserted where? At the current Selection point? That is not a problem at all, that is the default. BUT:

1. if you want to insert them at a point that is NOT the Selection point, then you have to deal with that.

2. what if you have multiple choices...are ALL of them going to be inserted at the same point?

3. and more importantly, if you have multiple "chunks" checked, in what order will they be inserted?

These are logic questions. No matter what answer you come up with, I have no doubt it can be done.

vsempoux
06-16-2009, 05:05 AM
Hello Fumei,

Sorry for the late response and thanks already for the reply.

Let me explain you the situation.

The whole concept is based on a custom CRM application that is completly web based and where all info is stored on a SQL server.
In this application we have a section where i can store word documents which i will then use as a template in this application.

So when i was talking about a template, it was not ment as a word template, but a word document that is stored in our application (db) that will then be used as a template from the application.

So what i need to create is a word document with an embedded macro to select diffent text parts to insert in the document.

At a certain moment in the application i will do merge between data from the application and the stored word document (with the embedded macro).

This word document will then be opened by the customer, and by clicking on a button the macro is started and the customer can select the different text parts he wants to show in the word doc (this will be letters from the customer service that will be sent to customers).

I hope this gives a better overview on the setup i want to achieve.

So to answer your questions :
* The text has to be inserted in the actual document.
* The point were the must be inserted should be fix (the user doesn't has a choise where to insert them, only what will be inserted is part of his selection)
* The text parts in the macro must be organised in such a way that they are inserted in the same way they are selected.

Does this give you the needed input ?

Vince.

fumei
06-16-2009, 08:59 AM
"* The text parts in the macro must be organised in such a way that they are inserted in the same way they are selected."

The "same way"? What does that mean?

User sees:

A
B
C
D
E

User selects A, C, E. OK...now what? If you are saying that selecting choice A means: insert A_Text at location X; selecting choice C means: insert C_Text at location R; then that is simply the logic you define. You define location R as after location X if "they are inserted in the same way they are selected" has some significance.

Use bookmarks.

Anyway...

"In this application we have a section where i can store word documents which i will then use as a template in this application."

Then please do not refer to them as templates. You are not using them as templates.

If I understand correctly, then I would go back and look at the example Steve (lucas) posted. However, because that IS a real template, you will have to make absolutely sure that you automate a SaveAs instruction. Otherwise you will be changing your original file.

I am not sure why you are bothering though. If "all info is stored on a SQL server", and "i will do merge between data from the application and the stored word document ", then why not just get the information from the user and build the document from scratch?