PDA

View Full Version : Insert AutoText table loses combobox values



will1128
09-01-2010, 09:29 AM
I've attached the Word 2003 document I'm trying to update.

When I click on Add Schedule my table is inserted with combo boxes which need to contain 00:00 through 24:00 for their hours.

How do I keep my AutoText table and the values for the combobox?

Also, how do I make sure the first table is inserted BELOW the button?

Here's the code I'm using to insert the AutoText:

Public Sub AddScheduleButton_Click()
Set myRange = ActiveDocument.Range(Start:=Selection.End, End:=Selection.End)
ActiveDocument.Content.Select
ActiveDocument.AttachedTemplate.AutoTextEntries("SchTbl").Insert _
Where:=myRange, RichText:=True
End Sub

fumei
09-01-2010, 10:04 AM
1. This demonstrates the issue I mentioned regarding AutoText. AutoText does NOT work for the attached file as it does not exist locally. It is in your normal.dot.

2. you have the code:
Set myRange = ActiveDocument.Range(Start:=Selection.End, End:=Selection.End)
This means the AutoText is set to be inserted (basically) at the Selection. Which is...at the button.

3. your combobox have no items as it is. However, that does not mean anything as comboboxes in an AutoText entry do not, CAN NOT, have items in them. You have to populate each one.

will1128
09-01-2010, 10:10 AM
Since AutoText doesn't seem to be my solution for distribution, any other suggestions?

geekgirlau
09-01-2010, 04:46 PM
You CAN use AutoText, provided the AutoText entry exists on the template attached to your document. If you use Tools | Templates & Add-In | Attach, you can set this to a template containing your AutoText (plus styles, macros etc.). Obviously the template you attach needs to be accessible to all users.

To position the cursor below the button, the button is already activated as the user has clicked on it. Therefore you only need to move to the right and insert a new paragraph:


Selection.MoveRight wdCharacter, 1
Selection.TypeParagraph


(Gerry, I believe this may be one of the few occasions where "Selection" is valid, but I'm sure you'll correct me if I'm mistaken :tongue: )

After inserting the AutoText, you can then loop through the comboboxes in the table.


Dim tbl As Table
Dim ctrl As InlineShape


' make sure you're in the last inserted table
Do Until Selection.Information(wdWithInTable) = True
Selection.MoveLeft wdCharacter, 1
Loop

Set tbl = Selection.Tables(1)

tbl.Select

For Each ctrl In Selection.InlineShapes
With ctrl.OLEFormat.Object
.AddItem "A"
.AddItem "B"
End With
Next

will1128
09-02-2010, 06:45 AM
I have managed to figure out how to draw the table with combo boxes and have them populated. Althought, it's not an elegant code.

Geekgirlau, I appreciate the help, but I'm not sure how I insert the table after each table and the button from the example you provided.

This is the code I'm using to add the talbe at Range 0,0

Set myRange = ActiveDocument.Range(Start:=Selection.End, End:=Selection.End)

ActiveDocument.Tables.Add Range:=myRange, NumRows:=13, NumColumns:= _
8, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitContent
ActiveDocument.Tables(1).Borders.Enable = False

The remaining code is the format of the table.

fumei
09-02-2010, 01:10 PM
(Gerry, I believe this may be one of the few occasions where "Selection" is valid, but I'm sure you'll correct me if I'm mistaken )Yes, it is one of them. The reason is as you state, focus (and thus Selection) is at the commanbutton.

This is easily shown by:
Private Sub CommandButton1_Click()
Selection.TypeText "Gerry"
End Sub
which....deletes the commandbutton as the Selection (which encompasses the commandbutton) gets the text "Gerry", thus...no more commandbutton! To retain the commandbutton (as one would assume is desirable) Selection must be moved off it.

fumei
09-02-2010, 01:11 PM
"but I'm not sure how I insert the table after each table and the button from the example you provided."

By using a bookmark to place the table, delete the bookmark, and then re-create the bookmark after the new table.