Using AutoText

A

Anne P.

I have a really big problem that I hope someone can help with. I have a
userform that has two textboxes, txtReLine and txtATName. The user will
enter text into the first and a name into the second. When the user clicks
OK, I need to create an AutoText entry in a template named Personal.dot in
the startup directory. It needs to be under a category named ReLine, with
the name of the entry coming from the second textbox and the value of the
entry coming from the first box.

All of the information that I can find on creating AutoText shows it being
created from a Selection object or a Range object. Is there anyway that I
can assign the value of the first textbox to a selection or range object
without inserting it into a document first? This is a routine that will be
used in several templates (letter and memo). I am in a big time crunch here
and cannot figure out what to do.

I initially thought of using an INI file or the Registry to store this
information, but in all templates that will call the routine, there is a
combo box which has to list the existing Re Line entries. The only way that
I can figure out how to fill the combo box with those entries is to use
AutoText.

Can this be done, or should I be looking at a different way of doing it?

Thanks for any help.

Anne P.
 
J

Jay Freedman

I have a really big problem that I hope someone can help with. I have a
userform that has two textboxes, txtReLine and txtATName. The user will
enter text into the first and a name into the second. When the user clicks
OK, I need to create an AutoText entry in a template named Personal.dot in
the startup directory. It needs to be under a category named ReLine, with
the name of the entry coming from the second textbox and the value of the
entry coming from the first box.

All of the information that I can find on creating AutoText shows it being
created from a Selection object or a Range object. Is there anyway that I
can assign the value of the first textbox to a selection or range object
without inserting it into a document first? This is a routine that will be
used in several templates (letter and memo). I am in a big time crunch here
and cannot figure out what to do.

I initially thought of using an INI file or the Registry to store this
information, but in all templates that will call the routine, there is a
combo box which has to list the existing Re Line entries. The only way that
I can figure out how to fill the combo box with those entries is to use
AutoText.

Can this be done, or should I be looking at a different way of doing it?

Thanks for any help.

Anne P.

You're correct, in order to create an AutoText entry in any template,
the text or other stuff that becomes the entry must exist in a
document first. The Range parameter of the AutoTextEntries.Add method
is a required parameter, and you can't substitute any sort of
in-memory object for it. Further, the category is specified by the
paragraph style applied to that range; there's no other way to supply
the category name or to change it later.

For code that shows how to load text into a template's AutoText
collection, see the macro in
http://jay-freedman.info/autotextloader2.zip.

Your code could insert the user's text into a scratch document (which
could be hidden behind the active document, or possibly created with
Visible = False although I'm not sure that will work), apply the
ReLine style to it, add it to the AutoText collection, and then delete
the range. There might be some flicker while that's going on, but it
shouldn't be too bad.

Your last comment confuses me, though. Is the stuff listed in the
combo boxes supposed to be a list of the available AutoText entries,
or is it not necessarily AutoText? If not, what else is it?
 
A

Anne P.

Jay,

Thanks I will look at the info.

For the last statement, right now I have a few entries in AutoText, which is
really easy to load into a combo box.

Originally, I was saving the Re Lines to an INI file like so (example of the
INI file section):

[ReText]
TimeWarner=TimeWarner Co. v. Cablevision|Case #10456-1045|Other Text Here
IBM Corp.=IBM Corp. v. Fujitsu, Inc.|Case #10545-2045|Other Text Here

I was able to get them saved into the INI file without a problem using
System.PrivateProfileString. I was also able to get them out using the same
technique, passing the "literal" name of an entry to
System.PrivateProfileString and replacing the pipe characters with a Soft
(or Hard) returns.

However, I need to load a list of the available Re Lines into a combobox or
listbox for the user to choose which one to insert into the document and
that is the part that I can't figure out. I assume that I need to do an
array of some kind to read all the Keys under a particular section and add
them to the array, but I am totally lost.

I would appreciate any help on this that you could offer. I searched the
Web through Google and Ask.com for working with INI files and could only
find very complicated VB6 examples.

I also tried using the Registry to write these values, but had the same
problem. I had no problem getting the entries saved to the Registry or
pulling them back out if I knew the name of the Subkey. My problem there
was the same thing: filling a listbox or combobox with all of the existing
Subkeys.

Thanks,
Anne P.
 
J

Jay Freedman

Hi Anne,

My question was really, "Is the purpose of the combobox to enable the user
to choose AutoText entries, or are you using AutoText entries only as
someplace to store strings that might be better stored some other way?" The
point being that AutoText is a great way to store stuff that can later be
inserted into documents, but if you don't need the ability to keep the
formatting with the strings, then there might be other ways to handle them.

I still can't tell from your latest reply.

However, if you do need AutoText, I'll point out that you do *not* need to
store the data in an INI file or the registry or anywhere else -- the
information is all available from the AutoText entries themselves. In the
procedure that loads the combobox, you can use something like this:

Dim oAT As AutoTextEntry

For Each oAT In myTemplate.AutoTextEntries
If oAT.StyleName = "RefLine" Then
ComboBox1.AddItem oAT.Name & "|" & oAT.Value
End If
Next oAT

(I'm not sure whether you intend to show the | character in the combobox,
but it can be replaced with a space or other character if you want.)

If you want to use an external file instead of AutoText, read the VBA help
about the Print # statement and the Line Input statement. The file can be
structured like an INI file, but it doesn't have to be. The
PrivateProfileString method isn't appropriate when you don't know the names
of the keys in advance.

--
Regards,
Jay Freedman
Microsoft Word MVP
Jay,

Thanks I will look at the info.

For the last statement, right now I have a few entries in AutoText,
which is really easy to load into a combo box.

Originally, I was saving the Re Lines to an INI file like so (example
of the INI file section):

[ReText]
TimeWarner=TimeWarner Co. v. Cablevision|Case #10456-1045|Other Text
Here IBM Corp.=IBM Corp. v. Fujitsu, Inc.|Case #10545-2045|Other Text
Here

I was able to get them saved into the INI file without a problem using
System.PrivateProfileString. I was also able to get them out using
the same technique, passing the "literal" name of an entry to
System.PrivateProfileString and replacing the pipe characters with a
Soft (or Hard) returns.

However, I need to load a list of the available Re Lines into a
combobox or listbox for the user to choose which one to insert into
the document and that is the part that I can't figure out. I assume
that I need to do an array of some kind to read all the Keys under a
particular section and add them to the array, but I am totally lost.

I would appreciate any help on this that you could offer. I searched
the Web through Google and Ask.com for working with INI files and
could only find very complicated VB6 examples.

I also tried using the Registry to write these values, but had the
same problem. I had no problem getting the entries saved to the
Registry or pulling them back out if I knew the name of the Subkey.
My problem there was the same thing: filling a listbox or combobox
with all of the existing Subkeys.

Thanks,
Anne P.


Jay Freedman said:
You're correct, in order to create an AutoText entry in any template,
the text or other stuff that becomes the entry must exist in a
document first. The Range parameter of the AutoTextEntries.Add method
is a required parameter, and you can't substitute any sort of
in-memory object for it. Further, the category is specified by the
paragraph style applied to that range; there's no other way to supply
the category name or to change it later.

For code that shows how to load text into a template's AutoText
collection, see the macro in
http://jay-freedman.info/autotextloader2.zip.

Your code could insert the user's text into a scratch document (which
could be hidden behind the active document, or possibly created with
Visible = False although I'm not sure that will work), apply the
ReLine style to it, add it to the AutoText collection, and then
delete the range. There might be some flicker while that's going on,
but it shouldn't be too bad.

Your last comment confuses me, though. Is the stuff listed in the
combo boxes supposed to be a list of the available AutoText entries,
or is it not necessarily AutoText? If not, what else is it?
 
A

Anne P.

Yes, the sole purpose of the combobox is to enable the user to choose an
existing AutoText entry. After choosing an entry from the combobox, the
value of the entry would be inserted into a textbox on the userform and then
inserted at a bookmark in the document when the user clicks OK.

I do understand that if I use AutoText I do *not* need an INI file or the
registry. The reason I mentioned all of that is that I was originally
trying to use INI files for this purpose. When I couldn't get that to work,
I tried the registry. However, the problem I encountered with both of those
methods was filling a combobox with the name keys of the existing entries so
that the user could choose one to be inserted. That's when I thought of
using AutoText because it is easy to load a combobox with names of AT
entries. I am already using similar code to what you show below to fill
comboboxes with the names of AutoText entries for the letter closing,
salutation etc. By the way, I only need the name of the autotext entry to
appear in the combobox, but I need to insert the value of the AT entry into
the textbox and document.

I took a look at the AutoTextLoader template. That is pretty cool, but I
think a little more advanced than what I need to do in this case. I
wouldn't need for the user to choose a template to store the AutoText
entries in because each user will have a template named Personal.dot that is
in their startup directory and that is where any AT entries will go. We are
not using Normal.dot for anything more than we absolutely have to. Also, I
think it would be too complicated for them to create the two column table
document to do the importing.

You had mentioned before about using a scratch document to save the text
from the textbox into, creating the AutoText entry from the scratch document
and then scratching the document. Would the Print statement and the Line
Input statement be what I would use to do this? If not, could you point me
to what command I would search for in VBA help? I am going to check out the
Print and Line INput statements right now.

Thanks for your help,

Anne P.


Jay Freedman said:
Hi Anne,

My question was really, "Is the purpose of the combobox to enable the user
to choose AutoText entries, or are you using AutoText entries only as
someplace to store strings that might be better stored some other way?"
The
point being that AutoText is a great way to store stuff that can later be
inserted into documents, but if you don't need the ability to keep the
formatting with the strings, then there might be other ways to handle
them.

I still can't tell from your latest reply.

However, if you do need AutoText, I'll point out that you do *not* need to
store the data in an INI file or the registry or anywhere else -- the
information is all available from the AutoText entries themselves. In the
procedure that loads the combobox, you can use something like this:

Dim oAT As AutoTextEntry

For Each oAT In myTemplate.AutoTextEntries
If oAT.StyleName = "RefLine" Then
ComboBox1.AddItem oAT.Name & "|" & oAT.Value
End If
Next oAT

(I'm not sure whether you intend to show the | character in the combobox,
but it can be replaced with a space or other character if you want.)

If you want to use an external file instead of AutoText, read the VBA help
about the Print # statement and the Line Input statement. The file can be
structured like an INI file, but it doesn't have to be. The
PrivateProfileString method isn't appropriate when you don't know the
names
of the keys in advance.

--
Regards,
Jay Freedman
Microsoft Word MVP
Jay,

Thanks I will look at the info.

For the last statement, right now I have a few entries in AutoText,
which is really easy to load into a combo box.

Originally, I was saving the Re Lines to an INI file like so (example
of the INI file section):

[ReText]
TimeWarner=TimeWarner Co. v. Cablevision|Case #10456-1045|Other Text
Here IBM Corp.=IBM Corp. v. Fujitsu, Inc.|Case #10545-2045|Other Text
Here

I was able to get them saved into the INI file without a problem using
System.PrivateProfileString. I was also able to get them out using
the same technique, passing the "literal" name of an entry to
System.PrivateProfileString and replacing the pipe characters with a
Soft (or Hard) returns.

However, I need to load a list of the available Re Lines into a
combobox or listbox for the user to choose which one to insert into
the document and that is the part that I can't figure out. I assume
that I need to do an array of some kind to read all the Keys under a
particular section and add them to the array, but I am totally lost.

I would appreciate any help on this that you could offer. I searched
the Web through Google and Ask.com for working with INI files and
could only find very complicated VB6 examples.

I also tried using the Registry to write these values, but had the
same problem. I had no problem getting the entries saved to the
Registry or pulling them back out if I knew the name of the Subkey.
My problem there was the same thing: filling a listbox or combobox
with all of the existing Subkeys.

Thanks,
Anne P.


Jay Freedman said:
On Thu, 2 Jun 2005 15:50:29 -0400, "Anne P."

I have a really big problem that I hope someone can help with. I
have a userform that has two textboxes, txtReLine and txtATName.
The user will enter text into the first and a name into the second.
When the user clicks
OK, I need to create an AutoText entry in a template named
Personal.dot in the startup directory. It needs to be under a
category named ReLine, with the name of the entry coming from the
second textbox and the value of the entry coming from the first box.

All of the information that I can find on creating AutoText shows
it being created from a Selection object or a Range object. Is
there anyway that I can assign the value of the first textbox to a
selection or range object without inserting it into a document
first? This is a routine that will be
used in several templates (letter and memo). I am in a big time
crunch here
and cannot figure out what to do.

I initially thought of using an INI file or the Registry to store
this information, but in all templates that will call the routine,
there is a combo box which has to list the existing Re Line
entries. The only way that
I can figure out how to fill the combo box with those entries is to
use AutoText.

Can this be done, or should I be looking at a different way of
doing it?

Thanks for any help.

Anne P.


You're correct, in order to create an AutoText entry in any template,
the text or other stuff that becomes the entry must exist in a
document first. The Range parameter of the AutoTextEntries.Add method
is a required parameter, and you can't substitute any sort of
in-memory object for it. Further, the category is specified by the
paragraph style applied to that range; there's no other way to supply
the category name or to change it later.

For code that shows how to load text into a template's AutoText
collection, see the macro in
http://jay-freedman.info/autotextloader2.zip.

Your code could insert the user's text into a scratch document (which
could be hidden behind the active document, or possibly created with
Visible = False although I'm not sure that will work), apply the
ReLine style to it, add it to the AutoText collection, and then
delete the range. There might be some flicker while that's going on,
but it shouldn't be too bad.

Your last comment confuses me, though. Is the stuff listed in the
combo boxes supposed to be a list of the available AutoText entries,
or is it not necessarily AutoText? If not, what else is it?
 
A

Anne P.

Yes, the sole purpose of the combobox is to enable the user to choose an
existing AutoText entry. After choosing an entry from the combobox, the
value of the entry would be inserted into a textbox on the userform and then
inserted at a bookmark in the document when the user clicks OK.

I do understand that if I use AutoText I do *not* need an INI file or the
registry. The reason I mentioned all of that is that I was originally
trying to use INI files for this purpose. When I couldn't get that to work,
I tried the registry. However, the problem I encountered with both of those
methods was filling a combobox with the name keys of the existing entries so
that the user could choose one to be inserted. That's when I thought of
using AutoText because it is easy to load a combobox with names of AT
entries. I am already using similar code to what you show below to fill
comboboxes with the names of AutoText entries for the letter closing,
salutation etc. By the way, I only need the name of the autotext entry to
appear in the combobox, but I need to insert the value of the AT entry into
the textbox and document.

I took a look at the AutoTextLoader template. That is pretty cool, but I
think a little more advanced than what I need to do in this case. I
wouldn't need for the user to choose a template to store the AutoText
entries in because each user will have a template named Personal.dot that is
in their startup directory and that is where any AT entries will go. We are
not using Normal.dot for anything more than we absolutely have to. Also, I
think it would be too complicated for them to create the two column table
document to do the importing.

You had mentioned before about using a scratch document to save the text
from the textbox into, creating the AutoText entry from the scratch document
and then scratching the document. Would the Print statement and the Line
Input statement be what I would use to do this? If not, could you point me
to what command I would search for in VBA help? I am going to check out the
Print and Line INput statements right now.

Thanks for your help,

Anne P.


Jay Freedman said:
Hi Anne,

My question was really, "Is the purpose of the combobox to enable the user
to choose AutoText entries, or are you using AutoText entries only as
someplace to store strings that might be better stored some other way?"
The
point being that AutoText is a great way to store stuff that can later be
inserted into documents, but if you don't need the ability to keep the
formatting with the strings, then there might be other ways to handle
them.

I still can't tell from your latest reply.

However, if you do need AutoText, I'll point out that you do *not* need to
store the data in an INI file or the registry or anywhere else -- the
information is all available from the AutoText entries themselves. In the
procedure that loads the combobox, you can use something like this:

Dim oAT As AutoTextEntry

For Each oAT In myTemplate.AutoTextEntries
If oAT.StyleName = "RefLine" Then
ComboBox1.AddItem oAT.Name & "|" & oAT.Value
End If
Next oAT

(I'm not sure whether you intend to show the | character in the combobox,
but it can be replaced with a space or other character if you want.)

If you want to use an external file instead of AutoText, read the VBA help
about the Print # statement and the Line Input statement. The file can be
structured like an INI file, but it doesn't have to be. The
PrivateProfileString method isn't appropriate when you don't know the
names
of the keys in advance.

--
Regards,
Jay Freedman
Microsoft Word MVP
Jay,

Thanks I will look at the info.

For the last statement, right now I have a few entries in AutoText,
which is really easy to load into a combo box.

Originally, I was saving the Re Lines to an INI file like so (example
of the INI file section):

[ReText]
TimeWarner=TimeWarner Co. v. Cablevision|Case #10456-1045|Other Text
Here IBM Corp.=IBM Corp. v. Fujitsu, Inc.|Case #10545-2045|Other Text
Here

I was able to get them saved into the INI file without a problem using
System.PrivateProfileString. I was also able to get them out using
the same technique, passing the "literal" name of an entry to
System.PrivateProfileString and replacing the pipe characters with a
Soft (or Hard) returns.

However, I need to load a list of the available Re Lines into a
combobox or listbox for the user to choose which one to insert into
the document and that is the part that I can't figure out. I assume
that I need to do an array of some kind to read all the Keys under a
particular section and add them to the array, but I am totally lost.

I would appreciate any help on this that you could offer. I searched
the Web through Google and Ask.com for working with INI files and
could only find very complicated VB6 examples.

I also tried using the Registry to write these values, but had the
same problem. I had no problem getting the entries saved to the
Registry or pulling them back out if I knew the name of the Subkey.
My problem there was the same thing: filling a listbox or combobox
with all of the existing Subkeys.

Thanks,
Anne P.


Jay Freedman said:
On Thu, 2 Jun 2005 15:50:29 -0400, "Anne P."

I have a really big problem that I hope someone can help with. I
have a userform that has two textboxes, txtReLine and txtATName.
The user will enter text into the first and a name into the second.
When the user clicks
OK, I need to create an AutoText entry in a template named
Personal.dot in the startup directory. It needs to be under a
category named ReLine, with the name of the entry coming from the
second textbox and the value of the entry coming from the first box.

All of the information that I can find on creating AutoText shows
it being created from a Selection object or a Range object. Is
there anyway that I can assign the value of the first textbox to a
selection or range object without inserting it into a document
first? This is a routine that will be
used in several templates (letter and memo). I am in a big time
crunch here
and cannot figure out what to do.

I initially thought of using an INI file or the Registry to store
this information, but in all templates that will call the routine,
there is a combo box which has to list the existing Re Line
entries. The only way that
I can figure out how to fill the combo box with those entries is to
use AutoText.

Can this be done, or should I be looking at a different way of
doing it?

Thanks for any help.

Anne P.


You're correct, in order to create an AutoText entry in any template,
the text or other stuff that becomes the entry must exist in a
document first. The Range parameter of the AutoTextEntries.Add method
is a required parameter, and you can't substitute any sort of
in-memory object for it. Further, the category is specified by the
paragraph style applied to that range; there's no other way to supply
the category name or to change it later.

For code that shows how to load text into a template's AutoText
collection, see the macro in
http://jay-freedman.info/autotextloader2.zip.

Your code could insert the user's text into a scratch document (which
could be hidden behind the active document, or possibly created with
Visible = False although I'm not sure that will work), apply the
ReLine style to it, add it to the AutoText collection, and then
delete the range. There might be some flicker while that's going on,
but it shouldn't be too bad.

Your last comment confuses me, though. Is the stuff listed in the
combo boxes supposed to be a list of the available AutoText entries,
or is it not necessarily AutoText? If not, what else is it?
 
J

Jay Freedman

It seems I threw too many things in together and confused both of us. :)

- The idea of using a scratch document deals with *creating* new AutoText
entries. I had the impression that you were allowing the user to create new
AutoTexts, which you wanted to store in the RefLine category in a specific
template. I offered the AutoTextLoader as an example of the kind of code
you'd need to handle that job, not as a full-fledged solution.

- The Print # and Line Input statements are what you would use if you
discard the idea of working with AutoText, and instead implemented storage
in a text file -- an alternative to the INI file or registry solution.

- Something else you'll want to look at: If the document with the bookmark
is not a protected form, then an AutoTextList field could be useful in place
of the userform and bookmark. See
http://word.mvps.org/FAQs/TblsFldsFms/AutoTextList.htm for a description.
You'd still need to provide a way for the user to create new entries, while
making sure they go into the correct category in the correct template, but
this could simplify your job.

--
Regards,
Jay Freedman
Microsoft Word MVP
Yes, the sole purpose of the combobox is to enable the user to choose
an existing AutoText entry. After choosing an entry from the
combobox, the value of the entry would be inserted into a textbox on
the userform and then inserted at a bookmark in the document when the
user clicks OK.

I do understand that if I use AutoText I do *not* need an INI file or
the registry. The reason I mentioned all of that is that I was
originally trying to use INI files for this purpose. When I couldn't
get that to work, I tried the registry. However, the problem I
encountered with both of those methods was filling a combobox with
the name keys of the existing entries so that the user could choose
one to be inserted. That's when I thought of using AutoText because
it is easy to load a combobox with names of AT entries. I am already
using similar code to what you show below to fill comboboxes with the
names of AutoText entries for the letter closing, salutation etc. By
the way, I only need the name of the autotext entry to appear in the
combobox, but I need to insert the value of the AT entry into the
textbox and document.

I took a look at the AutoTextLoader template. That is pretty cool,
but I think a little more advanced than what I need to do in this
case. I wouldn't need for the user to choose a template to store the
AutoText entries in because each user will have a template named
Personal.dot that is in their startup directory and that is where any
AT entries will go. We are not using Normal.dot for anything more
than we absolutely have to. Also, I think it would be too
complicated for them to create the two column table document to do
the importing.

You had mentioned before about using a scratch document to save the
text from the textbox into, creating the AutoText entry from the
scratch document and then scratching the document. Would the Print
statement and the Line Input statement be what I would use to do
this? If not, could you point me to what command I would search for
in VBA help? I am going to check out the Print and Line INput
statements right now.

Thanks for your help,

Anne P.


Jay Freedman said:
Hi Anne,

My question was really, "Is the purpose of the combobox to enable
the user to choose AutoText entries, or are you using AutoText
entries only as someplace to store strings that might be better
stored some other way?" The
point being that AutoText is a great way to store stuff that can
later be inserted into documents, but if you don't need the ability
to keep the formatting with the strings, then there might be other
ways to handle them.

I still can't tell from your latest reply.

However, if you do need AutoText, I'll point out that you do *not*
need to store the data in an INI file or the registry or anywhere
else -- the information is all available from the AutoText entries
themselves. In the procedure that loads the combobox, you can use
something like this:

Dim oAT As AutoTextEntry

For Each oAT In myTemplate.AutoTextEntries
If oAT.StyleName = "RefLine" Then
ComboBox1.AddItem oAT.Name & "|" & oAT.Value
End If
Next oAT

(I'm not sure whether you intend to show the | character in the
combobox, but it can be replaced with a space or other character if
you want.)

If you want to use an external file instead of AutoText, read the
VBA help about the Print # statement and the Line Input statement.
The file can be structured like an INI file, but it doesn't have to
be. The PrivateProfileString method isn't appropriate when you don't
know the names
of the keys in advance.

--
Regards,
Jay Freedman
Microsoft Word MVP
Jay,

Thanks I will look at the info.

For the last statement, right now I have a few entries in AutoText,
which is really easy to load into a combo box.

Originally, I was saving the Re Lines to an INI file like so
(example of the INI file section):

[ReText]
TimeWarner=TimeWarner Co. v. Cablevision|Case #10456-1045|Other Text
Here IBM Corp.=IBM Corp. v. Fujitsu, Inc.|Case #10545-2045|Other
Text Here

I was able to get them saved into the INI file without a problem
using System.PrivateProfileString. I was also able to get them out
using the same technique, passing the "literal" name of an entry to
System.PrivateProfileString and replacing the pipe characters with a
Soft (or Hard) returns.

However, I need to load a list of the available Re Lines into a
combobox or listbox for the user to choose which one to insert into
the document and that is the part that I can't figure out. I assume
that I need to do an array of some kind to read all the Keys under a
particular section and add them to the array, but I am totally lost.

I would appreciate any help on this that you could offer. I
searched the Web through Google and Ask.com for working with INI
files and could only find very complicated VB6 examples.

I also tried using the Registry to write these values, but had the
same problem. I had no problem getting the entries saved to the
Registry or pulling them back out if I knew the name of the Subkey.
My problem there was the same thing: filling a listbox or combobox
with all of the existing Subkeys.

Thanks,
Anne P.


On Thu, 2 Jun 2005 15:50:29 -0400, "Anne P."

I have a really big problem that I hope someone can help with. I
have a userform that has two textboxes, txtReLine and txtATName.
The user will enter text into the first and a name into the
second. When the user clicks
OK, I need to create an AutoText entry in a template named
Personal.dot in the startup directory. It needs to be under a
category named ReLine, with the name of the entry coming from the
second textbox and the value of the entry coming from the first
box.

All of the information that I can find on creating AutoText shows
it being created from a Selection object or a Range object. Is
there anyway that I can assign the value of the first textbox to a
selection or range object without inserting it into a document
first? This is a routine that will be
used in several templates (letter and memo). I am in a big time
crunch here
and cannot figure out what to do.

I initially thought of using an INI file or the Registry to store
this information, but in all templates that will call the routine,
there is a combo box which has to list the existing Re Line
entries. The only way that
I can figure out how to fill the combo box with those entries is
to use AutoText.

Can this be done, or should I be looking at a different way of
doing it?

Thanks for any help.

Anne P.


You're correct, in order to create an AutoText entry in any
template, the text or other stuff that becomes the entry must
exist in a document first. The Range parameter of the
AutoTextEntries.Add method is a required parameter, and you can't
substitute any sort of in-memory object for it. Further, the
category is specified by the paragraph style applied to that
range; there's no other way to supply the category name or to
change it later.

For code that shows how to load text into a template's AutoText
collection, see the macro in
http://jay-freedman.info/autotextloader2.zip.

Your code could insert the user's text into a scratch document
(which could be hidden behind the active document, or possibly
created with Visible = False although I'm not sure that will
work), apply the ReLine style to it, add it to the AutoText
collection, and then delete the range. There might be some flicker
while that's going on, but it shouldn't be too bad.

Your last comment confuses me, though. Is the stuff listed in the
combo boxes supposed to be a list of the available AutoText
entries, or is it not necessarily AutoText? If not, what else is
it?
 
A

Anne P.

Okay,

I'm confused too. Let's start over and only talk about using AutoText for
this because I think it is probably the best way to go.

1a. I have a userform (frmReLine) where the user will type some re text in
a text box named txtReLine and a name for the resulting AutoText entry that
will be created in a text box named txtATName. From this userform, the
information needs to be stored in Personal.dot as an AutoText entry
(txtATName is the name and txtReLine is the value and it should be stored in
a category named ReLines).

Now for the other templates that will use this information: I have a letter
and memo template whose userforms (frmMemo and frmLetter) have a combobox
named cmbReLine.

1b. First, This combobox needs to be filled with the names of stored
AutoText entries which match the category ReLine (I have this part down, I
have done it with other AutoText entries). When the user first runs either
the letter or memo template, there will be nothing to put into the combobox.
At this point, the user will click a button which will open the above
frmReLine to create a re line.

2b. Next, the user will choose an entry from the combobox list. The
AutoText entry matching the name chosen must be inserted into the document
at a bookmark named ReLine.

So, I belive that this is what I need to do: from the frmReLine userform
referenced in 1a. above, when the user fills out the two textboxes and
clicks OK, I need to open a Word document (preferably invisibly), insert the
text from txtReLine into the document, select the text, create a style named
ReLine and apply it to the text, save the AutoText entry to a template named
Personal.dot (in the Startup Directory) using the text in txtATName as the
name of the entry, and finally close the Word document without saving
changes.

After this step is complete, when I choose Insert, AutoText I should see a
category called ReLine. When I navigate to the ReLine category, I should
see entries which match any text previously entered into txtATName in step
1a. above.

I have the following code that I believe you, Jay, might have helped me with
before. This code works on a selection object. I would first select text
in a document, then run a userform named frmCreateAutoText. I had two
textboxes, one for the name of the category and one for the name of the
entry. I think I can reuse some of this code in my new frmReLine, however,
I would need to open a doc, use either a range object or a selection object
to create the entry, then close the doc without saving changes.

Dim oTemplate As Template
Dim strPath As String
Dim oNewStyle As Style
Dim bRemoveStyle

On Error GoTo ErrHdl
' assign the global template to an object
strPath = Options.DefaultFilePath(wdStartupPath) & _
"\SKGlobal.dot"
Set oTemplate = Templates(strPath)

On Error Resume Next
bRemoveStyle = False
' if the style already exists, this succeeds
Set oNewStyle = ActiveDocument.Styles(strATCategory)
If Err.Number <> 0 Then
' the style didn't exist, so create it
Err.Clear
Set oNewStyle = _
ActiveDocument.Styles.Add(Name:=strATCategory)
bRemoveStyle = True
End If

' change style of selection's paragraph to new category
Selection.Paragraphs(1).Style = _
ActiveDocument.Styles(strATCategory)

' add AT entry to global template
oTemplate.AutoTextEntries.Add Name:=strATName, _
Range:=Selection.Range

' reverse style assignment
ActiveDocument.Undo

' remove new style if it was created by this code
If bRemoveStyle Then
ActiveDocument.Styles(strATCategory).Delete
End If

Unload Me

ErrHdl:
MsgBox "Could not locate " & strPath
Unload Me
Dim oTemplate As Template
Dim strPath As String
Dim oNewStyle As Style
Dim bRemoveStyle

On Error GoTo ErrHdl
' assign the global template to an object
strPath = Options.DefaultFilePath(wdStartupPath) & _
"\SKGlobal.dot"
Set oTemplate = Templates(strPath)

On Error Resume Next
bRemoveStyle = False
' if the style already exists, this succeeds
Set oNewStyle = ActiveDocument.Styles(strATCategory)
If Err.Number <> 0 Then
' the style didn't exist, so create it
Err.Clear
Set oNewStyle = _
ActiveDocument.Styles.Add(Name:=strATCategory)
bRemoveStyle = True
End If

' change style of selection's paragraph to new category
Selection.Paragraphs(1).Style = _
ActiveDocument.Styles(strATCategory)

' add AT entry to global template
oTemplate.AutoTextEntries.Add Name:=strATName, _
Range:=Selection.Range

' reverse style assignment
ActiveDocument.Undo

' remove new style if it was created by this code
If bRemoveStyle Then
ActiveDocument.Styles(strATCategory).Delete
End If

Unload Me

ErrHdl:
MsgBox "Could not locate " & strPath
Unload Me

I hope this is a bit clearer on what I want to do.

In the meantime, I am going to take a look at the code I pasted above and
see if I can figure out how to make it work.

Thanks a bunch for your help.

Anne P.
Jay Freedman said:
It seems I threw too many things in together and confused both of us. :)

- The idea of using a scratch document deals with *creating* new AutoText
entries. I had the impression that you were allowing the user to create
new
AutoTexts, which you wanted to store in the RefLine category in a specific
template. I offered the AutoTextLoader as an example of the kind of code
you'd need to handle that job, not as a full-fledged solution.

- The Print # and Line Input statements are what you would use if you
discard the idea of working with AutoText, and instead implemented storage
in a text file -- an alternative to the INI file or registry solution.

- Something else you'll want to look at: If the document with the bookmark
is not a protected form, then an AutoTextList field could be useful in
place
of the userform and bookmark. See
http://word.mvps.org/FAQs/TblsFldsFms/AutoTextList.htm for a description.
You'd still need to provide a way for the user to create new entries,
while
making sure they go into the correct category in the correct template, but
this could simplify your job.

--
Regards,
Jay Freedman
Microsoft Word MVP
Yes, the sole purpose of the combobox is to enable the user to choose
an existing AutoText entry. After choosing an entry from the
combobox, the value of the entry would be inserted into a textbox on
the userform and then inserted at a bookmark in the document when the
user clicks OK.

I do understand that if I use AutoText I do *not* need an INI file or
the registry. The reason I mentioned all of that is that I was
originally trying to use INI files for this purpose. When I couldn't
get that to work, I tried the registry. However, the problem I
encountered with both of those methods was filling a combobox with
the name keys of the existing entries so that the user could choose
one to be inserted. That's when I thought of using AutoText because
it is easy to load a combobox with names of AT entries. I am already
using similar code to what you show below to fill comboboxes with the
names of AutoText entries for the letter closing, salutation etc. By
the way, I only need the name of the autotext entry to appear in the
combobox, but I need to insert the value of the AT entry into the
textbox and document.

I took a look at the AutoTextLoader template. That is pretty cool,
but I think a little more advanced than what I need to do in this
case. I wouldn't need for the user to choose a template to store the
AutoText entries in because each user will have a template named
Personal.dot that is in their startup directory and that is where any
AT entries will go. We are not using Normal.dot for anything more
than we absolutely have to. Also, I think it would be too
complicated for them to create the two column table document to do
the importing.

You had mentioned before about using a scratch document to save the
text from the textbox into, creating the AutoText entry from the
scratch document and then scratching the document. Would the Print
statement and the Line Input statement be what I would use to do
this? If not, could you point me to what command I would search for
in VBA help? I am going to check out the Print and Line INput
statements right now.

Thanks for your help,

Anne P.


Jay Freedman said:
Hi Anne,

My question was really, "Is the purpose of the combobox to enable
the user to choose AutoText entries, or are you using AutoText
entries only as someplace to store strings that might be better
stored some other way?" The
point being that AutoText is a great way to store stuff that can
later be inserted into documents, but if you don't need the ability
to keep the formatting with the strings, then there might be other
ways to handle them.

I still can't tell from your latest reply.

However, if you do need AutoText, I'll point out that you do *not*
need to store the data in an INI file or the registry or anywhere
else -- the information is all available from the AutoText entries
themselves. In the procedure that loads the combobox, you can use
something like this:

Dim oAT As AutoTextEntry

For Each oAT In myTemplate.AutoTextEntries
If oAT.StyleName = "RefLine" Then
ComboBox1.AddItem oAT.Name & "|" & oAT.Value
End If
Next oAT

(I'm not sure whether you intend to show the | character in the
combobox, but it can be replaced with a space or other character if
you want.)

If you want to use an external file instead of AutoText, read the
VBA help about the Print # statement and the Line Input statement.
The file can be structured like an INI file, but it doesn't have to
be. The PrivateProfileString method isn't appropriate when you don't
know the names
of the keys in advance.

--
Regards,
Jay Freedman
Microsoft Word MVP

Anne P. wrote:
Jay,

Thanks I will look at the info.

For the last statement, right now I have a few entries in AutoText,
which is really easy to load into a combo box.

Originally, I was saving the Re Lines to an INI file like so
(example of the INI file section):

[ReText]
TimeWarner=TimeWarner Co. v. Cablevision|Case #10456-1045|Other Text
Here IBM Corp.=IBM Corp. v. Fujitsu, Inc.|Case #10545-2045|Other
Text Here

I was able to get them saved into the INI file without a problem
using System.PrivateProfileString. I was also able to get them out
using the same technique, passing the "literal" name of an entry to
System.PrivateProfileString and replacing the pipe characters with a
Soft (or Hard) returns.

However, I need to load a list of the available Re Lines into a
combobox or listbox for the user to choose which one to insert into
the document and that is the part that I can't figure out. I assume
that I need to do an array of some kind to read all the Keys under a
particular section and add them to the array, but I am totally lost.

I would appreciate any help on this that you could offer. I
searched the Web through Google and Ask.com for working with INI
files and could only find very complicated VB6 examples.

I also tried using the Registry to write these values, but had the
same problem. I had no problem getting the entries saved to the
Registry or pulling them back out if I knew the name of the Subkey.
My problem there was the same thing: filling a listbox or combobox
with all of the existing Subkeys.

Thanks,
Anne P.


On Thu, 2 Jun 2005 15:50:29 -0400, "Anne P."

I have a really big problem that I hope someone can help with. I
have a userform that has two textboxes, txtReLine and txtATName.
The user will enter text into the first and a name into the
second. When the user clicks
OK, I need to create an AutoText entry in a template named
Personal.dot in the startup directory. It needs to be under a
category named ReLine, with the name of the entry coming from the
second textbox and the value of the entry coming from the first
box.

All of the information that I can find on creating AutoText shows
it being created from a Selection object or a Range object. Is
there anyway that I can assign the value of the first textbox to a
selection or range object without inserting it into a document
first? This is a routine that will be
used in several templates (letter and memo). I am in a big time
crunch here
and cannot figure out what to do.

I initially thought of using an INI file or the Registry to store
this information, but in all templates that will call the routine,
there is a combo box which has to list the existing Re Line
entries. The only way that
I can figure out how to fill the combo box with those entries is
to use AutoText.

Can this be done, or should I be looking at a different way of
doing it?

Thanks for any help.

Anne P.


You're correct, in order to create an AutoText entry in any
template, the text or other stuff that becomes the entry must
exist in a document first. The Range parameter of the
AutoTextEntries.Add method is a required parameter, and you can't
substitute any sort of in-memory object for it. Further, the
category is specified by the paragraph style applied to that
range; there's no other way to supply the category name or to
change it later.

For code that shows how to load text into a template's AutoText
collection, see the macro in
http://jay-freedman.info/autotextloader2.zip.

Your code could insert the user's text into a scratch document
(which could be hidden behind the active document, or possibly
created with Visible = False although I'm not sure that will
work), apply the ReLine style to it, add it to the AutoText
collection, and then delete the range. There might be some flicker
while that's going on, but it shouldn't be too bad.

Your last comment confuses me, though. Is the stuff listed in the
combo boxes supposed to be a list of the available AutoText
entries, or is it not necessarily AutoText? If not, what else is
it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top