PDA

View Full Version : Using autotexts to store info used repeatedly



RoyLiam
11-25-2006, 05:49 PM
Hi there

I have a userform that asks for info for eg file notes on client matters and asks for client code, name, file save location etc before releasing the user to the word doc filenote itself for completion. I am trying to automate it such that when the client code field is exited (say 'A12' was typed in) it looks for an autotext entry in the template called 'A12|name' and if it finds one then inputs that autotext into the client name field of the form automatically. This seems to work OK.

If it doesn't find an entry and the user types in the client name, file save location etc manually then I am trying to program a button to create the required autotexts so that next time a file note for that client matter is created this information will autopopulate. This is the line that I have been trying to use where 'code' is the field in the userform that the client code is input and the 'client' is the field where the client name has been input

ActiveDocument.AttachedTemplate.AutoTextEntries.Add code.Text & "|" & "entity", client.text

When this runs I get run time error 12 type mismatch and I am unclear how to resolve this, I wonder if anyone can advise? It seems as though it should be a relatively minor bug.

If I hover over code.text then it says code.text="B12" and over client.text="hello" when trying to debug which seems fine.

Generally is it OK to load up the autotexts like this, I would imagine that I would get around 500 or so entries over time. If these are stored in the template then presumably there is not a risk that if anyone say emailed a word document of a file note out to some other party then the contents of the autotexts library saved in the template would be released/visible within that document?

Would it be more efficient to store these autotexts in a seperate template, to minimise space and to allow other template docs to use the same data?

Finally i was using the following code to look for an autotext entry, I don't know if there is a more efficient way which doesn't cycle through each entry? I don't particularly need to know if there has been a match so long as the client.text field is updated if there is a matching entry.

Private Sub code_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim oAuto As AutoTextEntry
Dim ispresent As Boolean
ispresent = False
For Each oAuto In ActiveDocument.AttachedTemplate.AutoTextEntries
If oAuto.name = code & "|" & "client" Then
ispresent = True
Client.Text = ActiveDocument.AttachedTemplate.AutoTextEntries(oAuto.name)
End If
Next oAuto
End Sub

Regards and thanks in advance

TonyJollans
11-26-2006, 01:46 AM
Hi Roy,

Autotexts are very useful but my own opinion (possibly not generally shared) is that they are a missed opportunity and could have been made much easier to work with. I haven't yet looked at Building Blocks in 2007 to see whether it has improved. Anyway, on to your questions ...

AutoTextEntry Values are presented to VBA code as Strings but are effectively Ranges. A Range is a dynamic part of a Document and there is no way to work with one outside a Document. When you Add an AutotextEntry you must provide the Value as a Range, which you can, therefore, only do if you have it in a Document somewhere. How you arrange that may depend on circumstance - if you are adding the text to the Document anyway you could use the Range of the inserted text, or you may find it more convenient to temporarily add it to a Document and delete it afterwards.

There is nothing wrong with having a few hundred AutoTexts in a template but it seems to me you are trying to use simple AutoTexts as a form of database and I have some slight misgivings about that, but Word is not a database application so, to some extent, whatever you do is a bit of a kludge. Autotexts are stored in Templates and only available when the Template is available. Assuming you are not using AutoTextList Fields, the fact that a particular piece of text was inserted as an Autotext is not retained in the Document and using, or mailing, the Document without the Template is (a) no problem and (b) secure.

Generally speaking, AutoTexts are designed for reuse and are most useful in a global Template, but they could be used in a Template on which multiple Documents are based. You do need to be careful, though, if you have a shared Template.

As for your last question, there is no need to search, you can index AutoTexts directly and just do this:


Private Sub code_Exit(ByVal Cancel As MSForms.ReturnBoolean)
On Error Resume Next
client.Text = ActiveDocument.AttachedTemplate.AutoTextEntries(code.Text & "|" & "client")
End Sub

RoyLiam
11-26-2006, 09:50 AM
Thanks Tony this sounds fine - Sounds like i can insert them into the bookmarks and then refer to the bookmarks in the main doc - i agree that autotexts doesn't feel like the right long term answer for database type info - the info is actually stored in a firebird sql but it's beyond me to work out how to interrogate that from VBA

thanks for your help

regards

mdmackillop
11-26-2006, 11:27 AM
Here's some sample code that can recover text from a text file, based on an initial code. Assign a keyboard shortcut and use it in the same fashion as Autotext, ie type the abbreviation followed by the shortcut. See attached text file. I've used :: as a separator as an unlikely string.


Option Explicit
Option Compare Text
Sub AText()
Dim aTxt As String
Dim fs, a, strLine As String
Dim Loc1 As Long, Loc2 As Long
Selection.MoveLeft Unit:=wdWord, Count:=1, Extend:=wdExtend
aTxt = Selection.Text
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.OpenTextFile("c:\AAA\atext.txt", 1, 0)
strLine = a.readall
Loc1 = InStr(1, strLine, aTxt & "::")
Loc2 = InStr(Loc1 + Len(aTxt) + 2, strLine, "::")

aTxt = Mid(strLine, Loc1 + 4, Loc2 - Loc1 - 8)
Selection.TypeText aTxt

a.Close
End Sub

fumei
11-26-2006, 01:49 PM
When you Add an AutotextEntry you must provide the Value as a Range, which you can, therefore, only do if you have it in a Document somewhere.Not quite accurate. The text (or Value) of an added AutoText entry does NOT have to be anywhere in the document. Yes, you must supply a Range, but it can be Selection.Range - best with that Range being a collapsed selection.

New document; nothing in it (just the single paragraph mark); the following code will add an AutoText entry "Test" with a Value of "Yadda Yadda whatever":With NormalTemplate
.AutoTextEntries.Add Name:="Test", _
Range:=Selection.Range
.AutoTextEntries("Test").Value = _
"Yadda Yadda whatever"The text value does not have to actually be in the document. Again though, collapse the Selection so it does not actually have a character.

It is a GOOD idea to keep autotext entries in their own template. It can be loaded global automatically, or dynamically depending on your requirements.

The error with your code, your Type Mis-match:ActiveDocument.AttachedTemplate.AutoTextEntries.Add _
code.Text & "|" & "entity", client.text is that VBA is looking for a Range parameter...and client.txt is not a Range.ActiveDocument.AttachedTemplate.AutoTextEntries.Add _
code.Text & "|" & "entity", Range:=Selection.Range
ActiveDocument.AttachedTemplate. _
AutoTextEntries(code.Text & "|" & "entity").Value = client.textshould do it.

TonyJollans
11-27-2006, 03:49 AM
You are correct, of course, Gerry. That technique also has a side effect which I don't really like but which may well be beneficial in this case: the Autotext, when created, will have the Stylename of whatever the Style is at the Selection but it will be overridden to Normal when the Value is changed.

fumei
11-27-2006, 05:20 AM
Hi Tony.
the Autotext, when created, will have the Stylename of whatever the Style is at the Selection

This is interesting. If you use an actual Range object to make an Autotext, even if the Range has a style different from Normal, when you use the AutoText the style will be Normal.Sub RangeMe()
Dim r As Range
' set Range within a paragraph with Testing style
Set r = ActiveDocument.Range(Start:=87, End:=92)
NormalTemplate.AutoTextEntries.Add _
Name:="TestNEW", Range:=r
MsgBox r.Style
' returns "Testing"
Set r = Nothing
End SubUsing the new AutoText will insert the actual text that was in the Range, BUT the style is Normal.

Normally, if I use a range (collapsed) to make an AutoText, and then make the value whatever text I want, I make the Range object as Start:=0, End:= 0.

Even if you make a range (within a styled paragraph) and then actually Select it, then use Range:=Selection.Range to make the AutoText, it will still come out NormaL.

It is only if the paragraph mark is included will the style be attached to the AutoText. This applies to whether you use a Range, or a Selection.

Personal note: it is 4:00 AM. Vancouver rarely gets snow. It is common that we can go through a winter without any snow at all. We have had 30 cm over the last 24 hours. Normally, even at 4:00 AM, as it is a port city of 2,000,000 +, there is ALWAYS a low rumble of noise. Trucks in the distance, sirens, LRT (Light Rapid Transit) running.

It is absolutely dead quiet out there. Not a sound. Nothing is moving. It is very weird hearing...nothing. I can almost imagine hearing the snow flakes landing...it is still snowing.

TonyJollans
11-27-2006, 07:36 AM
Hi Gerry,

I haven't seen a foot of snow for many a year - it's lovely to look at if you don't have to go out in it. Silence, on the other hand, I have most of the time - and it is equally lovely.

This confusion is part of why I say Autotext could have been so much more. Autotexts have categories (StyleName property) by which they are grouped - see the AutoText toolbar - these categories are not changeable by the user and get lost whenever the AT entries are copied. Autotext contents, on the other hand, may have character styling applied to parts of them and may or may not have paragraph styling (depending, as you say, on whether or not they include paragraph marks) applied to parts of them. There is not necessarily any relationship between the StyleName and the actual Style.

When you use an Autotext which does not include a paragraph mark, the text takes on the style of the paragraph into which you insert it and any character styling is superimposed - and possibly reversed: if your autotext is "normal bold normal" and you have bold formatting applied at the insertion point it will come out as "normal bold normal".

It really drives me crazy on occasion - I am trying to do a write-up on it (and AutoCorrect and AutoFormat) at the moment because I have failed to find full information anywhere.

fumei
11-27-2006, 07:40 AM
When you do find the information...write it up.

I agree. AutoText could be much better.

What is the situation with 2007 and AutoText?

TonyJollans
11-27-2006, 08:12 AM
2007 has a whole new concept of Building Blocks - of which ATs are just one type. I think the AutoTextList Field has gone but don't have it in front of me at the moment to check.