Results 1 to 11 of 11
  1. #1
    Lounger
    Join Date
    Mar 2001
    Location
    Los Angeles, California, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Office 97: get formfield name

    Hi all,

    (I hope this is the right place for this)
    I've created a userform the data from which can be used in any of several fields in a Word template I built. Rather than create a new userform and associated programming for each field, I'd like to use the same one. It would thus need to know which field the user is currently in. I have the form itself working fine, but I don't know how to get the bookmark name for the current field so I can assign it to a variable that would then serve in place of a "hard coded" bookmark name. Here's the code I wrote behind the OK button: data is gathered from two comboboxes, then concatenated and written to a formfield on the document.

    Private Sub cmdOK_1_Click()

    Dim strRankStep1 As String
    Dim intRankTemp As Integer
    Dim strStepTemp As String
    Dim strRank As String
    Dim strStep As String
    Dim response As Variant


    intRankTemp = cmbLevel_1
    strStepTemp = cmbStep_1

    strRank = Str(intRankTemp)
    strStep = strStepTemp
    strRankStep1 = "Asst. " + strRank + ", " + "Step " + strStep

    ActiveDocument.FormFields("txtTitle_1").Result = strRankStep1

    'I want to use a variable in place of "txtTitle_1."

    Unload frmRankStep1
    End Sub

    So, how do I get the name of a formfield's bookmark?

    thanks in advance, <img src=/S/confused.gif border=0 alt=confused width=15 height=20>

  2. #2
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Anthony,

    I'm not quite clear whether you're talking about a form field on a document or on a userform.

    Assuming it's a form field on a document, I've tended to use a different shell routine for each form field, which then calls the common main routine with a parameter.

    <pre>sub FF1
    call common("FF1")
    end sub

    sub FF2
    call common("FF2")
    end sub

    sub Common(strName as string)
    msgbox strName
    end sub
    </pre>


    I then attach FF1 to an OnExit from one form field, FF2 to another form field.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  3. #3
    Lounger
    Join Date
    Mar 2001
    Location
    Los Angeles, California, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    My bad, I was writing quickly and in a bit of frustrated mood. <img src=/S/headthrob.gif border=0 alt=headthrob width=15 height=15>

    I created a template with many formfields. Three of those fields can hold exactly the same information: the rank and step of the employee. The userform I created allows the operator to select rank and step and have the result placed in the formfield. The way things are written now, the result goes to txtTitle_1. I'd like the programming to know which field the cursor is in and put the data in that same field. The formfields are bookmarked as txtTitle_1, txtTitle_2, and txtTitle_3. What I'd like is a way for the program to get the bookmark of the formfield so that I can put this into a variable, the variable then serving to identify the correct formfield in the code. That way, I could use the one userform to work with all three formfields.

    Does this make it any clearer? <img src=/S/grovel.gif border=0 alt=grovel width=31 height=23>

    Thanks!

  4. #4
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Anthony,

    I've attached a quick and dirty to do something along those lines, just expanding a bit on my first post.
    Attached Files Attached Files
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  5. #5
    Lounger
    Join Date
    Mar 2001
    Location
    Los Angeles, California, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Thanks, Geoff. I'll play around with that to see what I can do. <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

  6. #6
    Lounger
    Join Date
    Mar 2001
    Location
    Los Angeles, California, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Hi Geoff,

    The example you gave pointed me in what I thought was the right direction, and if/elseif/else-then statement using the formfields object of the selection object collection. But now Word tells me the object doesn't exist. I've attached the template: I'd be grateful if you (or anyone else) would take a look at the code behind the OK button on the userform and tell me why the if/then statement fails.

    thanks!

  7. #7
    Lounger
    Join Date
    Mar 2001
    Location
    Los Angeles, California, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Rats, I screwed-up the attachment. Here is is for real. <img src=/S/blush.gif border=0 alt=blush width=15 height=15>
    Attached Files Attached Files

  8. #8
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Anthony,

    Here's the way I'd code the whole thing:
    <pre>Private Sub cmdOK_1_Click()

    If cmbLevel_1.Text = "" Then
    MsgBox "Must select a level"
    cmbLevel_1.SetFocus
    Exit Sub
    End If

    If cmbStep_1.Text = "" Then
    MsgBox "Must select a step"
    cmbStep_1.SetFocus
    Exit Sub
    End If

    ActiveDocument.FormFields(Selection.BookmarkID).Re sult = _
    "Asst. " + cmbLevel_1.Text + ", " + "Step " + cmbStep_1.Text

    Unload frmRankStep1
    End Sub

    Private Sub UserForm_Initialize()
    Dim intRank As Integer

    For intRank = 1 To 5
    cmbLevel_1.AddItem intRank
    Next intRank

    cmbStep_1.AddItem "1.0"
    cmbStep_1.AddItem "1.5"
    cmbStep_1.AddItem "2.0"
    cmbStep_1.AddItem "2.5"
    cmbStep_1.AddItem "3.0"
    cmbStep_1.AddItem "3.5"
    cmbStep_1.AddItem "4.0"
    cmbStep_1.AddItem "4.5"
    cmbStep_1.AddItem "5.0"
    cmbStep_1.AddItem "5.5"
    cmbStep_1.AddItem "6.0"
    cmbStep_1.AddItem "6.5"
    cmbStep_1.AddItem "7.0"
    cmbStep_1.AddItem "7.5"
    cmbStep_1.AddItem "8.0"
    cmbStep_1.AddItem "8.5"
    cmbStep_1.AddItem "9.0"
    cmbStep_1.AddItem "9.5"
    cmbStep_1.AddItem "10.0"

    End Sub
    </pre>


    So- I've
    .added validation
    .replaced the "if" by using the "Selection.BookmarkID" as an index (I've learnt something myself)
    .removed temporary variables
    .Taken away the arrays and replaced them by using "Additem"

    I hope that's of some use.
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  9. #9
    Lounger
    Join Date
    Mar 2001
    Location
    Los Angeles, California, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Thanks, Geoff. That does exactly what I needed. <img src=/S/joy.gif border=0 alt=joy width=23 height=23>

  10. #10
    Platinum Lounger
    Join Date
    Dec 2000
    Location
    Queanbeyan, New South Wales, Australia
    Posts
    3,730
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    Great.

    I thought later though. If you had other bookmarks other than formfields, the code would probably not work. Just a caution!
    Subway Belconnen- home of the Signboard to make you smile. Get (almost) daily updates- follow SubwayBelconnen on Twitter.

  11. #11
    Lounger
    Join Date
    Mar 2001
    Location
    Los Angeles, California, USA
    Posts
    31
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Office 97: get formfield name

    That crossed my mind, too, but, as the only bookmarks in the form are those that name the formfields, I think it's safe in this case. Thanks again!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •