PDA

View Full Version : Using AutoTextList with VBA



helplessUser
04-06-2009, 01:14 PM
Help,

I need to insert an AutoTextList a table I create with VBA...
This list should be a defined set in the template I want to distribute.
Can pleeeease anybody help me with information and/or sourcecode?

Demosthine
04-09-2009, 09:19 AM
Good morning and welcome to the Forum.

I'm not quite sure what you mean by an "AutoTextList." Would you please explain this in a little more detail. Also, if you can provide any sort of example workbook, that would be very beneficial as well.

Scott

fumei
04-09-2009, 10:33 AM
This may help.

http://word.tips.net/Pages/T000549_Creating_an_AutoText_List.html

I got the above by typing AutoTextList into Google...

fumei
04-09-2009, 10:36 AM
BTW: make sure you read it all, especially:

"will see a listing of all the AutoText entries that have been defined, provided they use the same style as the paragraph in which the field is located. Thus, if the paragraph where you entered the AUTOTEXTLIST field is in a paragraph formatted as Body Text, only those AutoText entries that utilize the Body Text style will be listed. "

fumei
04-09-2009, 10:54 AM
Also - VERY important -

"If the user then picks an AutoText entry from the listing, that entry replaces the field."

Thus, once used, the field is gone gone gone.

Lastly, as you asked about doing this with VBA - I think, as I have to agree with Demosthine that you are not being very clear - to put in that field you MUST cover the use of string literals in your code.

Typed manually (as described on the web page):

{ AUTOTEXTLIST "[Pick an Entry]" }

Created via code; note the extra "!:
Sub MakeAT_List()
With Selection
.Fields.Add Range:=Selection.Range, _
Type:=wdFieldEmpty, _
PreserveFormatting:=False
.TypeText Text:="AUTOTEXTLIST ""[Pick an entry]"""
.Fields.Update
End With
End Sub

helplessUser
04-10-2009, 02:53 AM
@fumei: thank you, that helped me quite.

sorry, about not being clear, but that was all I knew. The guys at the office I work at part times showed me a Word-template with macroes, and told me "We wanna have that in our document, too!"
The macro they showed me puts a list of address information into the document. A special information was a field you could right-click with your mouse and pop-up a List of possible items. So I used ALT+F9 to see what kind of flied it is and read "AutoTextList". Unfortunately the macro is property of the company, and I signed not to distribute such things, so I could not post it here. Sorry :-x

BTT: In the macro my office-colleagues showed me, the AutoTextList does not vanish when you choose an item?! You can save the document and later change the item, so now I am a little bit confused why it does not work in this case...?!

helplessUser
04-15-2009, 11:59 AM
*ahem* sorry, but I have a slight Problem to get the AutoTextField-code into the .cell(10, 2) of the table I create via macro....
I started VBA with Excel, so I have a problem with the selection.range.
Could someone please help me out?

fumei
04-15-2009, 01:18 PM
It would be better to not use Selection at all. I assume you are having problems with:
.Fields.Add Range:=Selection.Range


The range used to place the Field can be ANY range. The default is - like so many defaults in Word - the Selection. This is what is in the code above.

It is always helpful if you mention things fully. I take - now - that you are, in fact doing this from Excel? This is rather significant. If this is correct then you MUST fully qualify operations you are doing in your instance of Word.

I do not know what you are doing to create the table via code. The best way - that is if you wish to perform further actions on the new table - is to use a table object.

Here is a demo Word document that makes a 10 x 2 table, then puts an AutoTextList field into Cell(10,2). Click "Make New Table" on the top toolbar.

Here is the code that works running from Word. If you are executing similar instructions from Excel, you will have to adjust it to fully qualify objects for your instance of Word.

NOTE (hint) #1: Selection is an object of the Application, not the Document.

NOTE (hint) #2: variables need to be declared as appropriate for the application they are being used in.

For example, if you are executing VBA from Excel:

Dim r As Range

is VERY VERY different from

Dim r As Word.Range