Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / February 2007

Tip: Looking for answers? Try searching our database.

Creating a function

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
red6000 - 19 Feb 2007 12:01 GMT
Hi,

I have a load of forms which are essentially Identical and contain 5
textboxes.

In my code I then make use of the values held in the textboxes along with
some other standard code:

What I would like to do is create a function to make my code more efficient.
where the argument of the function is the formname.

Ie, for example I have 5 forms named (in reality I have numerous forms)

formA
formB
formc
formD
formE

each form holds 6 textboxes (textbox6 thru to 11).

Now my function I have created is:

Function addAddress(frmName As String)

   Dim TB6 As String
   Dim TB7 As String
   Dim TB8 As String
   Dim TB9 As String
   Dim TB10 As String
   Dim TB11 As String

   TB6 = frmName & ".TextBox6.Value"
   TB7 = frmName & ".TextBox7.Value"
   TB8 = frmName & ".TextBox8.Value"
   TB9 = frmName & ".TextBox9.Value"
   TB10 = frmName & ".TextBox10.Value"
   TB11 = frmName & ".TextBox11.Value"

   Documents("BaseLetter.doc").Activate

   Selection.Find.ClearFormatting
   Selection.Find.Text = "aaa"
   Selection.Find.Execute
   Selection.Delete Unit:=wdCharacter, count:=1

   Selection.TypeText Text:=TB6
   Selection.TypeParagraph
   Selection.TypeText Text:=TB7
   Selection.TypeParagraph
   Selection.TypeText Text:=TB8
   Selection.TypeParagraph
   Selection.TypeText Text:=TB9
   Selection.TypeParagraph
   Selection.TypeText Text:=TB10
   Selection.MoveRight Unit:=wdCell
   Selection.ParagraphFormat.Alignment = wdAlignParagraphRight
   Selection.TypeText Text:="rgt"

   Selection.Find.Text = "ddd"
   Selection.Find.Execute
   Selection.Delete Unit:=wdCharacter, count:=1
   Selection.TypeText Text:=TB11

   Selection.Find.Text = "eee"
   Selection.Find.Execute
   Selection.Delete Unit:=wdCharacter, count:=1
   Selection.TypeText Text:="insert"

   Selection.Find.Text = "bbb"
   Selection.Find.Execute
   Selection.Delete Unit:=wdCharacter, count:=1

End Function

but when I run it by calling the function with:

addAddress ("formA")

it populates with the text of 'formA.Textbox6.Value' instead of the value
held in textbox 6 etc.

How can I build the correct name (I know it is to do with using the argument
of String, but what should I use)?

Thanks.
Doug Robbins - Word MVP - 19 Feb 2007 12:56 GMT
In that you have ".TextBox6.Value" enclosed in quote marks, it will be
treated as a string.

Apart from not being sure that is your only problem, instead of doing the
Selection.Find business, you should either insert Bookmarks into the
template and then insert the text into the .Range of the bookmarks, or
insert {DOCVARIABLE} fields into the document and use code to set the value
of the variables and update the fields in the document.

Signature

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

> Hi,
>
[quoted text clipped - 82 lines]
>
> Thanks.
steve.allen75@googlemail.com - 19 Feb 2007 13:20 GMT
On 19 Feb, 12:56, "Doug Robbins - Word MVP" <d...@REMOVECAPSmvps.org>
wrote:
> In that you have ".TextBox6.Value" enclosed in quote marks, it will be
> treated as a string.
[quoted text clipped - 101 lines]
>
> - Show quoted text -

Hi thanks for the reply.  I appreciate that the rest of my code is not
the best, but I am trying to just get it to work generally first and
then improve later.

I've changed the code to remove the "" around .TextBox6.Value, but
that errors with Invalid or unqualified reference.  I assume because I
still have the arguments set as type String.

Ignoring the rest of the code I am effectively am looking for the code
to do the following:

Dim xForm As something (where x is the name of a userform)
Dim zValue As something (where z is the value of a textbox)

zValue = xForm.TextBox6.Value

MsgBox("The value held in TextBox 6 in " & xForm & " is " zValue)
red6000 - 19 Feb 2007 13:29 GMT
Hi thanks for the reply.  I appreciate that the rest of my code is not
the best, but I am trying to just get it to work generally first and
then improve later.

I've changed the code to remove the "" around .TextBox6.Value, but
that errors with Invalid or unqualified reference.  I assume because I
still have the arguments set as type String.

Ignoring the rest of the code I am effectively am looking for the code
to do the following:

Dim xForm As something (where x is the name of a userform)
Dim zValue As something (where z is the value of a textbox)

zValue = xForm.TextBox6.Value

MsgBox("The value held in TextBox 6 in " & xForm & " is " zValue)

> In that you have ".TextBox6.Value" enclosed in quote marks, it will be
> treated as a string.
[quoted text clipped - 91 lines]
>>
>> Thanks.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.