Group:  Microsoft Word ยป microsoft.public.word.vba.userforms
Thread: If...Then pointers, dates, and other bits

DotNetBag
.NET Development Newsgroups

HTVi
TV Discussion Newsgroups

Our Hot Pick: Rising Antivirus 2006 - Certified by TUV & Checkmark! Get 10% discount by entering this coupon code: ONDISCOUNT10
Rising Antivirus 2006

If...Then pointers, dates, and other bits
"Math" <math.lang[ at ]gmail.com> 17.07.2006 16:05:00
Hi,

I'm hoping someone will point me in the right direction to find info on
this. I've tried looking it up all over the place, but I suspect I'm
not using the correct wording to get the right answer.

I'm doing a basic letter template with a UserForm to gather the info.
As it stands, I've got it working at a very basic "type on the form,
load through into Bookmarks" level - but I want to add more.

Firstly, I want to be able to pick a company name from a combo and have
it complete 2 other fields - Contract Manager and Address. I presume I
need an If...Then statement to do the lookup, but I don't know quite
how (I have tried). Also, the address will be multiline - is that a
problem?

Secondly, some companies only have one contract - so that could be
filled in on company selection. Other companies have two, and so a
combo would be needed to give the option. How could I do this? I
presume it's again an if...then, with the result of loading a different
set of contracts into the combo box (3 possible results - a, b, a&b)

I have a field for a date - how do I make sure that it's recorded as a
datevalue rather than a string?

How do I format the case of the text from the UserForm before loading
it into the bookmarks?

Any links or help that can be given will be greately appreciated :)

Matt

Re: If...Then pointers, dates, and other bits
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 17.07.2006 18:51:05
This routine loads a listbox or it could be a combobox, with client details
stored in a table in a separate
document (which makes it easy to maintain with additions, deletions etc.),
that document being saved as Clients.Doc for the following code.

On the UserForm, have a list box (ListBox1) and a Command Button
(CommandButton1) and use the following code in the UserForm_Initialize() and
the CommandButton1_Click() routines

Private Sub UserForm_Initialize()
Dim sourcedoc As Document, i As Integer, j As Integer, myitem As Range,
m As Long, n As Long
' Modify the path in the following line so that it matches where you
saved Clients.doc
Application.ScreenUpdating = False
' Open the file containing the client details
Set sourcedoc = Documents.Open(FileName:="e:\worddocs\Clients.doc")
' Get the number or clients = number of rows in the table of client
details less one
i = sourcedoc.Tables(1).Rows.Count - 1
' Get the number of columns in the table of client details
j = sourcedoc.Tables(1).Columns.Count
' Set the number of columns in the Listbox to match
' the number of columns in the table of client details
ListBox1.ColumnCount = j
' Define an array to be loaded with the client data
Dim MyArray() As Variant
'Load client data into MyArray
ReDim MyArray(i, j)
For n = 0 To j - 1
For m = 0 To i - 1
Set myitem = sourcedoc.Tables(1).Cell(m + 2, n + 1).Range
myitem.End = myitem.End - 1
MyArray(m, n) = myitem.Text
Next m
Next n
' Load data into ListBox1
ListBox1.List() = MyArray
' Close the file containing the client details
sourcedoc.Close SaveChanges:=wdDoNotSaveChanges
End Sub

Private Sub CommandButton1_Click()
Dim i As Integer, Addressee As String
Addressee = ""
For i = 1 To ListBox1.ColumnCount
ListBox1.BoundColumn = i
Addressee = Addressee & ListBox1.Value & vbCr
Next i
ActiveDocument.Bookmarks("Addressee").Range.InsertAfter Addressee
UserForm2.Hide
End Sub

The Initialize statement will populate the listbox with the data from the
table and then when a client is selected in from the list and the command
button is clicked, the information for that client will be inserted into a
bookmark in the document. You may want to vary the manner in which it is
inserted to suit our exact requirements, but hopefully this will get you
started.

To make it easy for you, the code has been written so that it will deal with
any number of clients and any number of details about each client. It
assumes that the first row of the table containing the client details is a
header row.


--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Math" <math.lang[ at ]gmail.com> wrote in message
news:1153152300.230309.191780[ at ]p79g2000cwp.googlegroups.com...
[Quoted Text]
> Hi,
>
> I'm hoping someone will point me in the right direction to find info on
> this. I've tried looking it up all over the place, but I suspect I'm
> not using the correct wording to get the right answer.
>
> I'm doing a basic letter template with a UserForm to gather the info.
> As it stands, I've got it working at a very basic "type on the form,
> load through into Bookmarks" level - but I want to add more.
>
> Firstly, I want to be able to pick a company name from a combo and have
> it complete 2 other fields - Contract Manager and Address. I presume I
> need an If...Then statement to do the lookup, but I don't know quite
> how (I have tried). Also, the address will be multiline - is that a
> problem?
>
> Secondly, some companies only have one contract - so that could be
> filled in on company selection. Other companies have two, and so a
> combo would be needed to give the option. How could I do this? I
> presume it's again an if...then, with the result of loading a different
> set of contracts into the combo box (3 possible results - a, b, a&b)
>
> I have a field for a date - how do I make sure that it's recorded as a
> datevalue rather than a string?
>
> How do I format the case of the text from the UserForm before loading
> it into the bookmarks?
>
> Any links or help that can be given will be greately appreciated :)
>
> Matt
>


Re: If...Then pointers, dates, and other bits
"Math" <math.lang[ at ]gmail.com> 18.07.2006 17:06:51
Many thanks for this Doug. Storing the contact data in an external
document was top of my list of things to do in the future.

What I'm still not clear on is how the array is used in the UserForm
for a lookup.

I start off with a combo showing Company names, now nicely populated
from an external list. If I pick CompanyA off the list, and have that
transcribe to the appropriate bookmark on the letter, how do I then
have it look up to see that, by picking CompanyA, the contact should be
ContactA, and the address should be AddressA?

Before your reply, I'd been trying to hardwire it using something
like...

Private Sub cmdOK_Click()
Dim strAddress As String
Dim strConManager As String

If cboSupplier.Value = "CompanyA" Then
strConManager = "ContactA"
strAddress = "AddressA" _
& vbCrLf & "City" _
& vbCrLf & "Post Code"
End If

etc.

Would this line of thought have worked, and how would it work with an
external document?

Also, how would I implement the bit where the selection from one Combo
influences the content of another Combo? There are 2 contracts and a
supplier might work on one of them or both, so I'd put an A, B, or C
field in too and was going to try and find a way to use that field to
dictate the contents of the second combo.

I'm really aware of taking up your time, so if there are any sources
(threads, webpages, books) that explain how to do this sort of project
I'd be really extremely grateful. I've seen so many letter generating
templates that surely this has been written up to death by now?

Thanks
Matt

Re: If...Then pointers, dates, and other bits
"Doug Robbins - Word MVP" <dkr[ at ]REMOVECAPSmvps.org> 18.07.2006 18:47:11
Use the .BoundColumn property of the Combbox to specify which column of data
in it to insert into the document.

--
Hope this helps.

Please reply to the newsgroup unless you wish to avail yourself of my
services on a paid consulting basis.

Doug Robbins - Word MVP

"Math" <math.lang[ at ]gmail.com> wrote in message
news:1153242411.379236.223750[ at ]m73g2000cwd.googlegroups.com...
[Quoted Text]
> Many thanks for this Doug. Storing the contact data in an external
> document was top of my list of things to do in the future.
>
> What I'm still not clear on is how the array is used in the UserForm
> for a lookup.
>
> I start off with a combo showing Company names, now nicely populated
> from an external list. If I pick CompanyA off the list, and have that
> transcribe to the appropriate bookmark on the letter, how do I then
> have it look up to see that, by picking CompanyA, the contact should be
> ContactA, and the address should be AddressA?
>
> Before your reply, I'd been trying to hardwire it using something
> like...
>
> Private Sub cmdOK_Click()
> Dim strAddress As String
> Dim strConManager As String
>
> If cboSupplier.Value = "CompanyA" Then
> strConManager = "ContactA"
> strAddress = "AddressA" _
> & vbCrLf & "City" _
> & vbCrLf & "Post Code"
> End If
>
> etc.
>
> Would this line of thought have worked, and how would it work with an
> external document?
>
> Also, how would I implement the bit where the selection from one Combo
> influences the content of another Combo? There are 2 contracts and a
> supplier might work on one of them or both, so I'd put an A, B, or C
> field in too and was going to try and find a way to use that field to
> dictate the contents of the second combo.
>
> I'm really aware of taking up your time, so if there are any sources
> (threads, webpages, books) that explain how to do this sort of project
> I'd be really extremely grateful. I've seen so many letter generating
> templates that surely this has been written up to death by now?
>
> Thanks
> Matt
>


Home | Search | Terms | Imprint | Contact
Newsgroups Reader - provided by WiredBox.Net