adding autotext to a template

A

Addy

Jonathan is now on holiday - can anyone else help in his abence please? We
have been corresponding over the last few months - please see below. Any
help would be greatly appreciated as I'm getting quite frustrated now!!

cheers

I have a letter template macro as a form chooser and I would like to add
various autotext in a list so that they can be added to the address bookmark
in the template. Is there a way or would I have to add each company name to
a module as an additem and then put the addresses in a macro in the form
chooser? If there is a better way which will pick up the autotex which are
contained in a global template in startup then that would be great as then I
wouldn't have to add a new macro every time someone requires a new address
adding to the list.

Many thanks

Addy

In VBA, you can create a loop to iterate through the AutotextEntries
collection of any Template object, get the Name property of each
AutoTextEntry in turn and use Additem to add that to the listbox in your
form. Something like this

Dim oTemplate As Template
Dim oAuto as AutotextEntry
Set oTemplate = Addins("My Global Template.dot")
For Each oAuto in oTemplate.AutoTextEntries
ListBox1.AddItem oAuto.Name
Next oAuto

Regards
Jonathan West - Word MVP

I tried the below but it doesn't seem to work. Please could you let me know
what I am doing wrong.

The below bit I put in my form chooser:-

Dim oTemplate As Template
Dim oAuto As AutoTextEntry

Set oTemplate = AddIns("legal.dot")

The below was put in a new module:-

With frmChooser 'frmchooser is my form (a letter template)

For Each oAuto In oTemplate.AutoTextEntries
CBOAddress.AddItem oAuto.Name
Next oAuto
.Show
End With

Anyone any ideas please?
Addy

Hi Addy

This line is wrong

CBOAddress.AddItem oAuto.Name

Its should be this

.CBOAddress.AddItem oAuto.Name

Note the extra period at the start of the line. That is what links it to the
"With frmChooser" statement

Regards
Jonathan West - Word MVP

Many thanks. I've changed that but now it doesn't like this line:

For Each oAuto In oTemplate.AutoTextEntries

When I hover over oAuto it says its empty

Cheers Addy

step the through the code. After you get to this line

Set oTemplate = AddIns("legal.dot")

check whether oTemplate is actually assigned to anything. If it isn't, then
you need to fix that object assignment.

If it is correctly assigned, then check the value of
oTemplate.AutoTextEntries.Count. If it is zero, then oTemplate doesn't have
any autotext entries in it.

Regards
Jonathan West - Word MVP
I've managed to get my otemplate to be assigned to legal.dot but it says
my
oauto is empty and I don't know why because there are autotext enteries in
legal.dot. This is what I've got so far:-

These are both in my form chooser:-

Dim oTemplate As Template
Dim oAuto As AutoTextEntry

The below are in a separate module:-

With frmChooser 'frmchooser is my form (a letter template)

Set oTemplate = AddIns("o:\msoffice97\winword\startup\legal.dot")
Set oAuto = AutoTextEntry (put this in entra as it wouldn't work)

For Each oAuto In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAuto.Name
Next oAuto
.Show
End With

Do you have an e-mail address that I could maybe sent my whole document to
you on to find out where I'm going wrong as it is a long and complex form

Addy

OK send it to (e-mail address removed). I might not have time to look before I go on
holiday this weekend for a fortnight, but I'll see what i can do.

Regards
Jonathan West - Word MVP
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
Jonathan is now on holiday - can anyone else help in his abence
please? We have been corresponding over the last few months - please
see below. Any help would be greatly appreciated as I'm getting
quite frustrated now!!

It is difficult for someone else to jumop in and replace Jonathan...

Why don't you tell use exactly what problem you are having right now
insteqad of asking us to read the whole thread and try to figure out what is
it that you

IN any case, I am not sure, but I think that this is were youa re at now:
Dim oTemplate As Template
Dim oAuto As AutoTextEntry

The below are in a separate module:-

With frmChooser 'frmchooser is my form (a letter template)

Set oTemplate = AddIns("o:\msoffice97\winword\startup\legal.dot")
Set oAuto = AutoTextEntry (put this in entra as it wouldn't work)

For Each oAuto In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAuto.Name
Next oAuto
.Show
End With

I do not understand what you mean by
"The below are in a separate module:-"

But, your code is still wrong:
With frmChooser 'frmchooser is my form (a letter template)

Set oTemplate = AddIns("o:\msoffice97\winword\startup\legal.dot")
Set oAuto = AutoTextEntry (put this in entra as it wouldn't work)

For Each oAuto In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAuto.Name
Next oAuto
.Show
End With


It should be:

Dim oTemplate As Template
Dim oAuto as AutotextEntry

Set oTemplate = Addins("o:\msoffice97\winword\startup\legal.dot")

With frmChooser
For Each oAuto in oTemplate.AutoTextEntries
.CBOAddress.AddItem oAuto.Name
Next oAuto
.Show
End With

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

Many thanks for your comments Jean.

Sorry if it was confusing. I just thought it might help to show what
Jonathan had suggested in the past. My code still doesn't work. It says
that my oauto is empty. It is quite difficult to explain through e-mail
without showing you the whole template. Is it possible I could send you the
whole template so that you can understand what I am trying to do. It is a
letter template and I would like the address part to have a drop down list
with addresses that come from autotext which is contained in a global add-in
called "legal.dot".

Many thanks for your help.

Addy
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
Many thanks for your comments Jean.

Sorry if it was confusing. I just thought it might help to show what
Jonathan had suggested in the past. My code still doesn't work. It
says that my oauto is empty. It is quite difficult to explain
through e-mail without showing you the whole template. Is it
possible I could send you the whole template so that you can
understand what I am trying to do. It is a letter template and I
would like the address part to have a drop down list with addresses
that come from autotext which is contained in a global add-in called
"legal.dot".

Sorry for my atrocious post... forgot to go back and clean it up...

Now, I do not think I need to see your template.
As Jonathan wrote, it the template assignments works and the object is
correctly assigned, then it means your template does not have Autotext
entries in it.

Try this simple code. If the combo box is empty, then for sure your template
does not have any autotext entries in it.

When you create the autotext entries, make sure you select the appropriate
template (legal.dot) in the list at the bottom of the dialog or they will
simply be added to Normal.dot, which is not what you want.

Sub test()

Dim frmChooser As UserForm1
Dim oTemplate As Template
Dim oAuto As AutoTextEntry

Set oTemplate = Templates("o:\msoffice97\winword\startup\legal.dot")
Set frmChooser = New UserForm1

With frmChooser
For Each oAuto In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAuto.Name
Next oAuto
.Show
End With

End Sub

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

I tried your test but it doesn't work. It comes up with a syntax error and
the below is red

For Each oAuto In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAuto.Name
Next oAuto
.Show

My autotexts are definitely in my legal.dot template and not in normal.
When I go into addins and open up my legal.dot they are all there in the
autotext tab.

Many thanks.

P.S. Your English and spelling is better than mine so I wouldn't worry
about your last post!
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
I tried your test but it doesn't work. It comes up with a syntax
error and the below is red

For Each oAuto In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAuto.Name
Next oAuto
.Show

My autotexts are definitely in my legal.dot template and not in
normal. When I go into addins and open up my legal.dot they are all
there in the autotext tab.

Are they still there if you select your template in the "Look in:" drop down
field at the bottom of the "AutoCorrect > AutoText tab" dialog?
If they are, can you post the exact Sub you are using (the one that contains
the snippet you posted above)?
Also, while in the VBE, double click on the ComboBox that is supposed to
receive the autotext names, this will create a default event and its
corresponding Sub, something like:

Private Sub ComboBox1_Change()

End Sub

Can you post that as well?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

Yes they are still there when I select legal.dot in look in

This is the whole module:-

Private Sub CBOAddress_Change()

With frmChooser 'frmchooser is my form (a letter template)

Set oTemplate = AddIns("o:\msoffice97\winword\startup\legal.dot")

For Each oAutotext In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAutotext.Name
Next oAutotext
.Show
End With

End Sub


This is the sub you wanted. It only had the dims in the rest was in my ok
sub but tha's massive so I've moved them to the cboaddress sub and below.
I'm probably missing loads (very new at this as you can probably tell!!):-

Private Sub CBOAddress_Change()
Dim oTemplate As Template
Dim oAuto As AutoTextEntry

straddress = Me.CBOAddress.Text

ActiveDocument.Bookmarks("address").Select
Selection.TypeText straddress

End Sub

Many thanks Jean
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
Yes they are still there when I select legal.dot in look in

This is the whole module:-

Private Sub CBOAddress_Change()

With frmChooser 'frmchooser is my form (a letter template)

Set oTemplate = AddIns("o:\msoffice97\winword\startup\legal.dot")

For Each oAutotext In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAutotext.Name
Next oAutotext
.Show
End With

End Sub

I am confused. You have two CBOAddress_Change subs?
In which module are these two subs located?

The one above will not really work as every time the user makes a change in
the combobox (select an item for example) the code tries to add new items to
the list...

Where is the code that loads the userform and loads the combobox with the
data from the template?
This is the sub you wanted. It only had the dims in the rest was in
my ok sub but tha's massive so I've moved them to the cboaddress sub
and below. I'm probably missing loads (very new at this as you can
probably tell!!):-

Private Sub CBOAddress_Change()
Dim oTemplate As Template
Dim oAuto As AutoTextEntry

straddress = Me.CBOAddress.Text

ActiveDocument.Bookmarks("address").Select
Selection.TypeText straddress

End Sub

This Sub could be reduced to:

Private Sub CBOAddress_Change()

ActiveDocument.Bookmarks("address").Range.Text = Me.CBOAddress.Text

End Sub

(Try not to use the election object, it slows things down and it is
unreliable.)
If the user makes a second change, the bookmark will be gone and the code
will fail.

This code should be executed not every time the user makes a change in the
combobox, but once just prior to unloading the userform.
Since I do not know what else you have on the userform and who you
instantiate it, I am not sure where you should put that code.

If you have an "OK" button, you could put it in its Click event.
Or in the module that calls the form into being.

If the user can recall the userform as often as needed, then let us know
because the code as is cannot be used more than once (since the bookmark is
gone after execution).

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

The template is launched as a copy of the template using this macro:-

WordBasic.FileNew Template:="yw letter", NewTemplate:=0

In VBA I have an autonew in module 1 which lists all the solicitors names so
that the secretary can pick her solicitor and it puts his/her name, e-mail
address, phone number and sign off on automatically. The rest of the code is
in the OK click sub which has all the dims, variables, declarations and
bookmarks. Its a very large piece of code do you want it all ? I put my new
code in module 2 and that's the code I sent to you. As you rightly pointed
out that is called the same as my CBOaddress change sub which I hadn't
realised so I've given it the name.

Private Sub Addresslist()

Thanks for the tip about not using election objects, I will change all my
codes as you suggested

Many thanks for your help
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
The template is launched as a copy of the template using this macro:-

WordBasic.FileNew Template:="yw letter", NewTemplate:=0

I do not remember WordBasic off-hand.
Why are you using it?

In "modern" VBA, you should use:
Documents.Add

So, are you creating a document from a template, or opening a template as a
template?
In VBA I have an autonew in module 1 which lists all the solicitors
names so that the secretary can pick her solicitor and it puts
his/her name, e-mail address, phone number and sign off on
automatically. The rest of the code is in the OK click sub which has
all the dims, variables, declarations and bookmarks. Its a very
large piece of code do you want it all ? I put my new code in module
2 and that's the code I sent to you. As you rightly pointed out that
is called the same as my CBOaddress change sub which I hadn't
realised so I've given it the name.

Now, where is the code that loads the combobox and what is it?

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

The wordbasic was used by someone else and I just kept it. I'm creating a
document from a template. Below is all the codes I have:-

The below is in Module 1.

Sub autonew()

With frmChooser

.cboNames.AddItem "None"
.cboNames.AddItem "Stuart D McFarlane"
.cboNames.AddItem "Peter Cockburn"
.cboNames.AddItem "Shona J Flood"
.cboNames.AddItem "Matthew Stevens"
.cboNames.AddItem "Perminder Kaur"
.cboNames.AddItem "Andrew Newton"
.cboNames.AddItem "Dominic Goldthorp"
.cboNames.AddItem "Deborah Stirling"
.cboNames.AddItem "Nicola Hewitt"
.cboNames.AddItem "Hamani Parmar"
.cboNames.AddItem "Adelaide Seymour"
.cboNames.AddItem "Lynda Barraclough"

.Show

End With

End Sub

The below is in module 2:-

Private Sub Addresslist()

With frmChooser 'frmchooser is my form (a letter template)

Set oTemplate = AddIns("o:\msoffice97\winword\startup\legal.dot")

For Each oAutotext In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAutotext.Name
Next oAutotext
.Show
End With

End Sub

The below is behind the frmchooser - its very long code I'm afraid

Private Sub CBOAddress_Change()

Dim oTemplate As Template
Dim oAutotext As AutoTextEntry

straddress = Me.CBOAddress.Text

ActiveDocument.Bookmarks("address").Range.Text = Me.CBOAddress.Text

End Sub

Private Sub cmdOK_Click()

'Declare all basic variables, and populate main ones with data from the form
Dim strFrom As String
Dim strEmail As String
Dim strOurRef As String
Dim strSalutation As String
Dim strSubject As String
Dim strYourRef As String
Dim strSignoff As String
Dim strCC As String
Dim strEnc As String
Dim strName As String
Dim strSign As String
Dim strReply As String


strFrom = Me.cboNames.Text
strEmail = "@yorkshirewater.co.uk"
strOurRef = Me.txtOurRef.Value
strSalutation = Me.txtsalutation.Value
strSubject = Me.txtSubject.Value
strYourRef = Me.txtYourRef.Value
'End of variable declarations and population


'Calculate who document is from, and populate variables with person-specific
information
Select Case strFrom
Case "None"
strEmail = ""

Case "Stuart D McFarlane"
strEmail = "stuart.mcfarlane" & strEmail
strNo = "4159"
strRef = "SDM"
strReply = "Mr Stuart McFarlane"

Case "Peter Cockburn"
strEmail = "peter.cockburn" & strEmail
strNo = "4107"
strRef = "PC"
strReply = "Mr Peter Cockburn"

Case "Shona J Flood"
strEmail = "shona.flood" & strEmail
strNo = "4156"
strRef = "SF"
strReply = "Miss Shona Flood"

Case "Matthew Stevens"
strEmail = "matthew.stevens" & strEmail
strNo = "4141"
strRef = "MS"
strReply = "Mr Matthew Stevens"

Case "Perminder Kaur"
strEmail = "perminder.kaur" & strEmail
strNo = "4145"
strRef = "PK"
strReply = "Miss Perminder Kaur"

Case "Andrew Newton"
strEmail = "andrew.newton" & strEmail
strNo = "4147"
strRef = "AN"
strReply = "Mr Andrew Newton"

Case "Dominic Goldthorp"
strEmail = "dominic.goldthorp" & strEmail
strNo = "4142"
strRef = "DG"
strReply = "Mr Dominic Goldthorp"

Case "Deborah Stirling"
strEmail = "deborah.stirling" & strEmail
strNo = "4146"
strRef = "DS"
strReply = "Mrs Deborah Stirling"

Case "Nicola Hewitt"
strEmail = "nicola.hewitt" & strEmail
strNo = "4139"
strRef = "NH"
strReply = "Miss Nicola Hewitt"

Case "Hamani Parmar"
strEmail = "hamani.parmar" & strEmail
strNo = "4144"
strRef = "HP"
strReply = "Mrs Hamani Parmar"

Case "Adelaide Seymour"
strEmail = "adelaide.seymour" & strEmail
strNo = "4170"
strRef = "AS"
strReply = "Miss Adelaide Seymour"

Case "Lynda Barraclough"
strEmail = "lynda.barraclough" & strEmail
strNo = "4138"
strRef = "LMB"
strReply = "Miss Lynda Barraclough"

End Select

'End of person-specific variable population

'Calculate the sign-off - faithfully or sincerely/Stuart McFarlane or
someone else
If optfaithfully.Value = True Then
strSign = "faithfully"

If strFrom = "Stuart D McFarlane" Then
strSignoff = "Head of Legal Services"
strName = strFrom
Else
strName = ""
strSignoff = "for Legal Services"
End If

Else
strSign = "sincerely"

If strFrom = "Stuart D McFarlane" Then
strSignoff = "Head of Legal Services"
strName = strFrom
Else
strName = strFrom
strSignoff = "for Legal Services"
End If

End If
'End of calculating the sign-off


'Start of navigating to bookmarks and inserting appropriate text
ActiveDocument.Bookmarks("email").Select
Selection.TypeText strEmail

ActiveDocument.Bookmarks("extno").Select
Selection.TypeText strNo

ActiveDocument.Bookmarks("FE").Select
Selection.TypeText strRef


ActiveDocument.Bookmarks("ourref").Select
Selection.TypeText strOurRef

ActiveDocument.Bookmarks("salutation").Select
Selection.TypeText strSalutation

ActiveDocument.Bookmarks("subject").Select
Selection.TypeText strSubject

ActiveDocument.Bookmarks("yourref").Select
Selection.TypeText strYourRef

ActiveDocument.Bookmarks("signoff").Select
Selection.TypeText strSignoff

ActiveDocument.Bookmarks("sign").Select
Selection.TypeText strSign

ActiveDocument.Bookmarks("name").Select
Selection.TypeText strName

ActiveDocument.Bookmarks("reply").Select
Selection.TypeText strReply

'End of inserting text


'Start of calculating and inserting the 'Copies' and 'Enclosures' text
If txtcc.Value <> "" Then
ActiveDocument.Bookmarks("cc").Select
Selection.TypeText "Copy to: " & txtcc.Value
End If

If txtenc.Value <> "" Then
ActiveDocument.Bookmarks("enc").Select
Selection.TypeText "Enclosures: " & txtenc.Value
End If
'End of inserting 'Copies' and 'Enclosures' text


Unload Me

'takes cursor to top of document and navigates to the first field
Selection.HomeKey Unit:=wdStory
Selection.NextField.Select
Selection.Fields.Unlink 'converts date field to text


ActiveDocument.Bookmarks("bodytext").Select

End Sub

Private Sub UserForm_Click()

End Sub
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
The wordbasic was used by someone else and I just kept it. I'm
creating a document from a template. Below is all the codes I have:-

Here is a revised version of your code. All you need are two subs. One to
launch and manipulate the userform, as well as populating the document. The
other Sub just to hide the form.

You may have to modify the "CC" and "Enc" related code as I do not know what
txtcc and txtenc are. Here, in this code, I presume that they are text
fields from the userform.

Generally speaking, it is better to keep all code outside of the userform
code, except for code that dynamic (Change or Click events for example).

'_______________________________________
Sub AutoNew()
Dim myForm as frmChooser
Dim oTemplate As Template
Dim oAutotext As AutoTextEntry
Dim strFrom As String
Dim strEmail As String
Dim strOurRef As String
Dim strSalutation As String
Dim strSubject As String
Dim strYourRef As String
Dim strSignoff As String
Dim strCC As String
Dim strEnc As String
Dim strName As String
Dim strSign As String
Dim strReply As String
Dim strAddress As String
Dim strCC As String
Dim strEnc As String

Set oTemplate = Templates("o:\msoffice97\winword\startup\legal.dot")

Set myForm = New frmChooser

With myForm
With .cboNames
.AddItem "None"
.AddItem "Stuart D McFarlane"
.AddItem "Peter Cockburn"
.AddItem "Shona J Flood"
.AddItem "Matthew Stevens"
.AddItem "Perminder Kaur"
.AddItem "Andrew Newton"
.AddItem "Dominic Goldthorp"
.AddItem "Deborah Stirling"
.AddItem "Nicola Hewitt"
.AddItem "Hamani Parmar"
.AddItem "Adelaide Seymour"
.AddItem "Lynda Barraclough"
End With
For Each oAutotext In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAutotext.Name
Next oAutotext
.Show


strFrom = .cboNames.Text
strEmail = "@yorkshirewater.co.uk"
strOurRef = .txtOurRef.Value
strSalutation = .txtsalutation.Value
strSubject = .txtSubject.Value
strYourRef = .txtYourRef.Value
strAddress = .CBOAddress.Text
strCC = .txtcc.Text
strEnc = .txtenc.Text

Unload myForm
Set myForm = Nothing

'Calculate who document is from, and populate variables with person-specific
information
Select Case strFrom
Case "None"
strEmail = ""
Case "Stuart D McFarlane"
strEmail = "stuart.mcfarlane" & strEmail
strNo = "4159"
strRef = "SDM"
strReply = "Mr Stuart McFarlane"

Case "Peter Cockburn"
strEmail = "peter.cockburn" & strEmail
strNo = "4107"
strRef = "PC"
strReply = "Mr Peter Cockburn"

Case "Shona J Flood"
strEmail = "shona.flood" & strEmail
strNo = "4156"
strRef = "SF"
strReply = "Miss Shona Flood"

Case "Matthew Stevens"
strEmail = "matthew.stevens" & strEmail
strNo = "4141"
strRef = "MS"
strReply = "Mr Matthew Stevens"

Case "Perminder Kaur"
strEmail = "perminder.kaur" & strEmail
strNo = "4145"
strRef = "PK"
strReply = "Miss Perminder Kaur"

Case "Andrew Newton"
strEmail = "andrew.newton" & strEmail
strNo = "4147"
strRef = "AN"
strReply = "Mr Andrew Newton"

Case "Dominic Goldthorp"
strEmail = "dominic.goldthorp" & strEmail
strNo = "4142"
strRef = "DG"
strReply = "Mr Dominic Goldthorp"

Case "Deborah Stirling"
strEmail = "deborah.stirling" & strEmail
strNo = "4146"
strRef = "DS"
strReply = "Mrs Deborah Stirling"

Case "Nicola Hewitt"
strEmail = "nicola.hewitt" & strEmail
strNo = "4139"
strRef = "NH"
strReply = "Miss Nicola Hewitt"

Case "Hamani Parmar"
strEmail = "hamani.parmar" & strEmail
strNo = "4144"
strRef = "HP"
strReply = "Mrs Hamani Parmar"

Case "Adelaide Seymour"
strEmail = "adelaide.seymour" & strEmail
strNo = "4170"
strRef = "AS"
strReply = "Miss Adelaide Seymour"

Case "Lynda Barraclough"
strEmail = "lynda.barraclough" & strEmail
strNo = "4138"
strRef = "LMB"
strReply = "Miss Lynda Barraclough"
End Select

'End of person-specific variable population

'Calculate the sign-off - faithfully or sincerely/Stuart McFarlane or
someone else
If optfaithfully.Value = True Then
strSign = "faithfully"
If strFrom = "Stuart D McFarlane" Then
strSignoff = "Head of Legal Services"
strName = strFrom
Else
strName = ""
strSignoff = "for Legal Services"
End If
Else
strSign = "sincerely"
If strFrom = "Stuart D McFarlane" Then
strSignoff = "Head of Legal Services"
strName = strFrom
Else
strName = strFrom
strSignoff = "for Legal Services"
End If
End If
'End of calculating the sign-off

'Start of navigating to bookmarks and inserting appropriate text
With ActiveDocument
.Bookmarks("email").Range.Text = strEmail
.Bookmarks("extno").Range.Text = strNo
.Bookmarks("FE").Range.Text = strRef
.Bookmarks("ourref").Range.Text = strOurRef
.Bookmarks("salutation").Range.Text = strSalutation
.Bookmarks("subject").Range.Text = strSubject
.Bookmarks("yourref").Range.Text = strYourRef
.Bookmarks("signoff").Range.Text = strSignoff
.Bookmarks("sign").Range.Text = strSign
.Bookmarks("name").Range.Text = strName
.Bookmarks("reply").Range.Text = strReply
.Bookmarks("address").Range.Text = strAddress
'End of inserting text

'Start of calculating and inserting the 'Copies' and 'Enclosures' text
If Trim(strCC) <> "" Then
.Bookmarks("cc").Range.Text = "Copy to: " & strCC
End If
If Trim(strEnc) <> "" Then
.Bookmarks("enc").Range.Text = "Enclosures: " & strEnc
End If
'End of inserting 'Copies' and 'Enclosures' text
End With

'takes cursor to top of document and navigates to the first field
Selection.HomeKey Unit:=wdStory
Selection.NextField.Select
Selection.Fields.Unlink 'converts date field to text

ActiveDocument.Bookmarks("bodytext").Select

End Sub
'_______________________________________


'_______________________________________
Private Sub cmdOK_Click()

Me.Hide

End Sub
'_______________________________________

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

Thanks for that Jean. Are you saying that I just put the ok click sub behind
the form and the rest of your code in a new module? I tried that and it
won't work. Lots of the code is coming up red. The "CC" is to put copy to:
at the bottom of the letter and "Enc" is to put enclosure at the bottom of
the letter.

Thanks
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
Thanks for that Jean. Are you saying that I just put the ok click
sub behind the form and the rest of your code in a new module? I

Yes and no.
Yes for the OK button, but you do not need a new module since you already
have one that contains your own autonew sub. Just replace your autonew by
mine.
tried that and it won't work. Lots of the code is coming up red.

If it is red it means that the compiler cannot read the lines of code (watch
out for wrapped text form the NG reader).

If you are not sure, post some of the red line of code.
The "CC" is to put copy to: at the bottom of the letter and "Enc" is
to put enclosure at the bottom of the letter.

I now what CC and Enc are. I just meant that I could not tell where the
content for those variables came from as your code was not clear on the
topic...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

Hi Jean

Sorry its me again - the bain of your life! I have managed to get the
address dropdown to populate at last but its now coming up with an error with
the following codes, its saying they are empty:-

strFrom = cboNames.Text
strEmail = "@yorkshirewater.co.uk"
strOurRef = txtOurRef.Value
strSalutation = txtsalutation.Value
strSubject = txtSubject.Value
strYourRef = txtYourRef.Value
strAddress = CBOAddress.Text
strCC = txtcc.Text
strEnc = txtenc.Text

Any ideas please as they worked in my original macro. I have pasted below
my code. Many thanks in advance, its driving my insane!

Sub AutoNew()

Dim myForm As frmChooser
Dim oTemplate As Template
Dim oAutotext As AutoTextEntry
Dim strFrom As String
Dim strEmail As String
Dim strOurRef As String
Dim strSalutation As String
Dim strSubject As String
Dim strYourRef As String
Dim strSignoff As String
Dim strName As String
Dim strSign As String
Dim strReply As String
Dim strAddress As String
Dim strCC As String
Dim strEnc As String

Set oTemplate = Templates("o:\msoffice97\winword\startup\legal.dot")

Set myForm = New frmChooser

With myForm
With .cboNames
.AddItem "None"
.AddItem "Stuart D McFarlane"
.AddItem "Peter Cockburn"
.AddItem "Shona J Flood"
.AddItem "Matthew Stevens"
.AddItem "Perminder Kaur"
.AddItem "Andrew Newton"
.AddItem "Dominic Goldthorp"
.AddItem "Deborah Stirling"
.AddItem "Nicola Hewitt"
.AddItem "Hamani Parmar"
.AddItem "Adelaide Seymour"
.AddItem "Lynda Barraclough"
End With
For Each oAutotext In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAutotext.Name
Next oAutotext
.Show
End With


strFrom = cboNames.Text
strEmail = "@yorkshirewater.co.uk"
strOurRef = txtOurRef.Value
strSalutation = txtsalutation.Value
strSubject = txtSubject.Value
strYourRef = txtYourRef.Value
strAddress = CBOAddress.Text
strCC = txtcc.Text
strEnc = txtenc.Text

Unload myForm
Set myForm = Nothing

'Calculate who document is from, and populate variables with person-specific
Information

Select Case strFrom
Case "None"
strEmail = ""
Case "Stuart D McFarlane"
strEmail = "stuart.mcfarlane" & strEmail
strNo = "4159"
strRef = "SDM"
strReply = "Mr Stuart McFarlane"

Case "Peter Cockburn"
strEmail = "peter.cockburn" & strEmail
strNo = "4107"
strRef = "PC"
strReply = "Mr Peter Cockburn"

Case "Shona J Flood"
strEmail = "shona.flood" & strEmail
strNo = "4156"
strRef = "SF"
strReply = "Miss Shona Flood"

Case "Matthew Stevens"
strEmail = "matthew.stevens" & strEmail
strNo = "4141"
strRef = "MS"
strReply = "Mr Matthew Stevens"

Case "Perminder Kaur"
strEmail = "perminder.kaur" & strEmail
strNo = "4145"
strRef = "PK"
strReply = "Miss Perminder Kaur"

Case "Andrew Newton"
strEmail = "andrew.newton" & strEmail
strNo = "4147"
strRef = "AN"
strReply = "Mr Andrew Newton"

Case "Dominic Goldthorp"
strEmail = "dominic.goldthorp" & strEmail
strNo = "4142"
strRef = "DG"
strReply = "Mr Dominic Goldthorp"

Case "Deborah Stirling"
strEmail = "deborah.stirling" & strEmail
strNo = "4146"
strRef = "DS"
strReply = "Mrs Deborah Stirling"

Case "Nicola Hewitt"
strEmail = "nicola.hewitt" & strEmail
strNo = "4139"
strRef = "NH"
strReply = "Miss Nicola Hewitt"

Case "Hamani Parmar"
strEmail = "hamani.parmar" & strEmail
strNo = "4144"
strRef = "HP"
strReply = "Mrs Hamani Parmar"

Case "Adelaide Seymour"
strEmail = "adelaide.seymour" & strEmail
strNo = "4170"
strRef = "AS"
strReply = "Miss Adelaide Seymour"

Case "Lynda Barraclough"
strEmail = "lynda.barraclough" & strEmail
strNo = "4138"
strRef = "LMB"
strReply = "Miss Lynda Barraclough"
End Select

'End of person-specific variable population

'Calculate the sign-off - faithfully or sincerely/Stuart McFarlane or
someone else

If optfaithfully.Value = True Then
strSign = "faithfully"
If strFrom = "Stuart D McFarlane" Then
strSignoff = "Head of Legal Services"
strName = strFrom
Else
strName = ""
strSignoff = "for Legal Services"
End If
Else
strSign = "sincerely"
If strFrom = "Stuart D McFarlane" Then
strSignoff = "Head of Legal Services"
strName = strFrom
Else
strName = strFrom
strSignoff = "for Legal Services"
End If
End If
'End of calculating the sign-off

'Start of navigating to bookmarks and inserting appropriate text
With ActiveDocument
.Bookmarks("email").Range.Text = strEmail
.Bookmarks("extno").Range.Text = strNo
.Bookmarks("FE").Range.Text = strRef
.Bookmarks("ourref").Range.Text = strOurRef
.Bookmarks("salutation").Range.Text = strSalutation
.Bookmarks("subject").Range.Text = strSubject
.Bookmarks("yourref").Range.Text = strYourRef
.Bookmarks("signoff").Range.Text = strSignoff
.Bookmarks("sign").Range.Text = strSign
.Bookmarks("name").Range.Text = strName
.Bookmarks("reply").Range.Text = strReply
.Bookmarks("address").Range.Text = strAddress
'End of inserting text

End With
'Start of calculating and inserting the 'Copies' and 'Enclosures' text

If txtcc.Value <> "" Then
ActiveDocument.Bookmarks("cc").Select
Selection.TypeText "Copy to: " & txtcc.Value
End If

If txtenc.Value <> "" Then
ActiveDocument.Bookmarks("enc").Select
Selection.TypeText "Enclosures: " & txtenc.Value
End If


'End of inserting 'Copies' and 'Enclosures' text



'takes cursor to top of document and navigates to the first field
Selection.HomeKey Unit:=wdStory
Selection.NextField.Select
Selection.Fields.Unlink 'converts date field to text

ActiveDocument.Bookmarks("bodytext").Select





End Sub
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
Hi Jean

Sorry its me again - the bain of your life! I have managed to get the
address dropdown to populate at last but its now coming up with an
error with the following codes, its saying they are empty:-

strFrom = cboNames.Text
strEmail = "@yorkshirewater.co.uk"
strOurRef = txtOurRef.Value
strSalutation = txtsalutation.Value
strSubject = txtSubject.Value
strYourRef = txtYourRef.Value
strAddress = CBOAddress.Text
strCC = txtcc.Text
strEnc = txtenc.Text

Set oTemplate = Templates("o:\msoffice97\winword\startup\legal.dot")

Set myForm = New frmChooser

With myForm
With .cboNames
.AddItem "None"
.AddItem "Stuart D McFarlane"
.AddItem "Peter Cockburn"
.AddItem "Shona J Flood"
.AddItem "Matthew Stevens"
.AddItem "Perminder Kaur"
.AddItem "Andrew Newton"
.AddItem "Dominic Goldthorp"
.AddItem "Deborah Stirling"
.AddItem "Nicola Hewitt"
.AddItem "Hamani Parmar"
.AddItem "Adelaide Seymour"
.AddItem "Lynda Barraclough"
End With
For Each oAutotext In oTemplate.AutoTextEntries
.CBOAddress.AddItem oAutotext.Name
Next oAutotext
.Show
End With


strFrom = cboNames.Text
strEmail = "@yorkshirewater.co.uk"
strOurRef = txtOurRef.Value
strSalutation = txtsalutation.Value
strSubject = txtSubject.Value
strYourRef = txtYourRef.Value
strAddress = CBOAddress.Text
strCC = txtcc.Text
strEnc = txtenc.Text

Unload myForm
Set myForm = Nothing


<snip>

Sorry about that, my bad.
In my previous post, the code I gave you was missing an "End With"
statement. So I guess that when you tried to run it, the compiler informed
you that there was an IF or a With block problem. You then added the End
With yourself, but at the wrong place...

When you use this construct:

With myForm
 
A

Addy

Many thanks for that but it still doesn't work. I had tried the end with
before where you suggested but moved it as it wouldn't work. Any other ideas
please. Its still saying the str's are empty.

Cheers
 
A

Addy

Sorry false alarm - that bit of code works now I forgot the "."s thought I'd
put them in originally but then realised I'd taken them out. Only thing is
it now doesn't like

If optfaithfully.Value = True Then

Don't know why are its exactly the same as my original template code.

Cheers
 
J

Jean-Guy Marcil

Addy was telling us:
Addy nous racontait que :
Sorry false alarm - that bit of code works now I forgot the "."s
thought I'd put them in originally but then realised I'd taken them
out. Only thing is it now doesn't like

If optfaithfully.Value = True Then

Don't know why are its exactly the same as my original template code.

It is because when I suggested my code 3 messages ago, I had not seen that
particular line of code. Must have thought it was some variable... Looked
too fast.

Well, since "optfaithfully" must refer to a control on the userform. and
since you have
Unload myForm
Set myForm = Nothing
higher up in the code, the userform is now gone from memory, so
"optfaithfully" refers to nothing.

Do this change:

If myForm.optfaithfully.Value = True Then

and move

Unload myForm
Set myForm = Nothing

to between those two comments:

'End of calculating the sign-off

Unload myForm
Set myForm = Nothing

'Start of navigating to bookmarks and inserting appropriate text

_________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
A

Addy

That is fantastic thank you very much Jean. I moved the unload myform bit
right to the end because I had some other code similar and I also added
myform. to them also and it works. I just need to put in another field for
any addresses which need to be added and I've cracked it - cross your fingers
I can work that out by myself!!

Many thanks for all your help - you've been a godsend.

Addy
 

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