Word automation, best course of action

W

Wayne

Let me first say that I am new to word automation, been playing with a few
examples today.

I have an application requirement to print letters, I want to use word to
print the letters. In doing so I need to place some sort of tag/marker in
the letter template so I know where to put things like name, address, etc. I
have tried bookmarks and found that they work great, until I need the name
in two places. So here are my questions:

1) Anyone have any suggestions on what I can use in word as a tag/marker?
2) Based on the tag marker suggestion, what properties/methods of the
document will I need to use to replace the tag/marker?


Thanks
Wayne
 
P

Peter Huang

Hi Wayne,

I think you may try to use the Autotext field to do the job.
Here goes the VBA code for your reference.
Sub AutoTxt()
Dim tm As Template
Set tm = ThisDocument.AttachedTemplate
'establish autotextentries
tm.AutoTextEntries.Add "tName1", ThisDocument.Range(0, 0)
tm.AutoTextEntries.Add "tName2", ThisDocument.Range(0, 0)
tm.AutoTextEntries.Add "tAddress", ThisDocument.Range(0, 0)

'Add autotext entries
ThisDocument.Fields.Add ThisDocument.Range(0, 0), wdFieldEmpty, "AUTOTEXT
""tName1"" "
ThisDocument.Fields.Add ThisDocument.Range(0, 0), wdFieldEmpty, "AUTOTEXT
""tName2"" "
ThisDocument.Fields.Add ThisDocument.Range(0, 0), wdFieldEmpty, "AUTOTEXT
""tAddress"" "

'Give new value to the autotext entries
tm.AutoTextEntries("tName1").Value = "John"
tm.AutoTextEntries("tName2").Value = "Peter"
tm.AutoTextEntries("tAddress").Value = "US"

'Update the value in the fields
Dim fd As Field
For Each fd In ThisDocument.Fields
fd.Update
Next
'Give new value to the autotext entries
tm.AutoTextEntries("tName1").Value = "Marry"
tm.AutoTextEntries("tName2").Value = "Linda"
tm.AutoTextEntries("tAddress").Value = "CA"
'Update the value in the fields
For Each fd In ThisDocument.Fields
fd.Update
Next
End Sub


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wayne

I finally got it all working, this will do exactly what I need. However, I
do have one more question.

I want the users to write up their own templates knowing the list of
available Items that my application will reconize, and as such they will
need to be able to enter the auto text names and fields in their documents.
So how would I do this in word vs. doing it in Code?

Thanks
Wayne
 
P

Peter Huang

Hi Wayne,

If you wants to do it in word, you may follow the steps below.
1. On the Tools menu, click AutoCorrect Options.
2. Select AutoText Tab
3. Choose the correct template in the Lookin combobox.
4. Now you can do the insert,add ...... operation

You may have a try and let me know the result.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wayne

it allows me to add the auto text values, but what I am seeing is that no
matter what I do to the doc in Word, I can't get to the autotext fields. I
have tried what you suggested, then did insert field and chose autotext and
the new value I added. When I run my app and spin through the
tm.AutoTextEntries the one manually added wasn't in the list.

I have also tried the following:

Typed some text in the doc
Highlighted it
insert --> AutoText --> New
clicked Ok
then added the field as above

Still it did not show in the tm.AutotTextEntries.

However, I have gotten it to work, what I have found is that I have to do
one of the options above, this gives the users the ability to add the fields
to the document. In the code I have to do what you had given me with the
exception of adding the fields to the document. So basiclly what I need to
do is the following:

User creates Template
Code loads new word doc with template
Spin through list of supported autext values adding them to the
tm.AutoTextEntries
Spin through each AutoTextEntry setting its value
Save/print document

This works, and seems to work well.

Thanks for all the help, without it I wouldn't have gotten this far this
quickly.
Wayne
 
P

Peter Huang

Hi Wayne,

I am glad that I can help you.
If you still have any concern on this issue, please feel free to post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
W

Wayne

I have finally reached a point where I am testing the below on other
machines. I have created a word template to use within my application, but
when I create a word document on another machine from the Template all of
the auto text fields get the following:

Error! AutoText entry not defined.

I have found that if I create the autotext fields on the other machine they
go away, but I don't want to have to do this on every users machine. Anyone
know how to auto create the autotext fields for the user?

Thanks
Wayne
 
W

Wayne

Nevermind, our Microsoft Office trainer answered this for me.

To start with when adding the auto text I added it to the normal.dot and not
my template. Once I figured that out I was able to go to the organizer and
copy the auto text from one template to the other.

Everything works just fine now.
Thanks
Wayne
 

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