• Referencing a Bookmark (field) in Macro (Win 2KPro, Office2KPro)

    Home » Forums » AskWoody support » Productivity software by function » MS Word and word processing help » Referencing a Bookmark (field) in Macro (Win 2KPro, Office2KPro)

    • This topic has 9 replies, 5 voices, and was last updated 22 years ago.
    Author
    Topic
    #387865

    I have a word doc with various form fields inserted. On one field, there is a list of values for a drop-down and in another field, there is a Number associated with each program name. What I’d like to do is have the Program Number field automatically update itself depending on the selection of the Program Name field. I have created a Select Case macro but haven’t had much success referencing the Program Name Field value to set the variable for the Select Case code. How do you reference the value of a Word field (or, as it calls itself, a Bookmark) in code?

    Viewing 1 reply thread
    Author
    Replies
    • #678587

      To get the vaule of a FormField, you need to use:

      Debug.Print ThisDocument.FormFields(“DropDown1”).Result

      In this case DropDown1 is the bookmark name of the DropDown form field.

    • #678598

      Here’s an idea. Use the “Run macro on Exit” property of the first drop-down field to update the second field:

      Sub DropDown1_Exit()
      
      Select Case ActiveDocument.FormFields("Dropdown1").Result
          Case "Peter"
              n = 10
          Case "Paul"
              n = 20
          Case "Mary"
              n = "30"
      End Select
      
      ActiveDocument.FormFields("Dropdown2").Result = n
      
      End Sub
      

      Unfortunately, the user has to actually EXIT the field before the updating occurs.
      In a real UserForm (not a form field in a doc) you could make this update “real-time” which users will find more intuitive.

      • #678606

        that looks about right. from what i can gather, Word Form fields apply macros either on Exit or on Enter & applying the change to On Exit for the Program Names field (which will be tabbed in before Program Number) is the better choice. Of course, even better would be to not have an ‘updateable’ Program Number field because, after going thru all the trouble to assign a value to it, the User could tab in, erase or change the assigned value…in which case you’d have to re-run the same macros as on on Exit to Program Number field. A little goofy, but I wouldn’t be suprised if Word’s form field handling is primitive. There’s no radio buttons provided in the Forms toolbar, for instance. Also, not sure if you can control tab order or disable a field. Well, it ain’t Access we’re talking about, but Word. I’ve asked my client to look over the automation & let me know if drop-downs are even something he wants. With the drop-downs, you prevent multiple selections for a category, but the form no longer looks like the passive paper form so it may be more confusing for the user. don’t know…I *like* drop-downs, but that doesn’t mean the client will!

        • #678618

          (Edited by HansV to activate link to post – see Help 19)

          Good points, Steve. I just posted a summary of how I like to do “form fill-in” templates in another thread: post 255721

          I insert a UserForm in the VB editor and use it to allow proper data entry with all the bells & whistles then stuff the results in fields & bookmarks, etc.
          Form Fields just don’t cut it after you’ve used real forms. UserForms are practically the same thing you’re used to in MS Access forms.

          • #678630

            Thanks for the reply, esp. the referenced thread discussion. I might need to use {REF} in some fields as info is repeated on the second page of the form. As for using VB to create better forms in Word, good suggestion but for this project I think I can get by using what Word makes available.

            One thing that is actually something of a positive using Word’s native form toolset: you get a very good sense of how the form would look in HTML/asp! The native form handling with these technologies is even more primitive, as you don’t have onExit events. For example, you can select a Program Name in the initial form and have the system find/display the associated Program Number on the preview/submit page. (You could probably use javascript for event-driven web pages, but I avoid javascript for critical functionality as there are browser compatibility issues with javascript.) But you can see how a web page might look after creating a form in Word (except of course Radio Buttons, which don’t exist in Word forms but do in HTML).

            • #679515

              Hi Steve

              I think I am correct in assuming that Dory was actually referring to Word’s own VBA, rather than VB, when she mentioned the “VB editor”. VBA is, of course, integral to MS Office, not an “extra” application like VB is.

              Alan

            • #679547

              Right you are, Alan. In Word’s Tools…Macros… menu, it is called the “Visual Basic Editor”. I wouldn’t send anyone over to Visual Basic 6.0 for a simple trick like this! smile

              When you know how to do little tricks in the “VB Editor” of Word, Excel & Access you hardly ever have to bother with Visual Basic stand-alone application development. I hope more people try Visual Basic for Applications (or macros or embedded VB or whatever you want to call it) and aren’t scared off by the idea of “programming”. It’s great stuff. (The night security guard at my client’s office was reading a C++ manual the other night. Lookin’ for a career change. I recommended he start with VBA macros in Excel or Word before tackling that challenge. Got to have some easy wins to keep scaling that learning curve.) OK…this is getting WAY off topic. Apology to moderator. doh

            • #680103

              that’s what i assumed when she suggested using VB for creating custom forms. I’ve never actually tried using VB 6+ etc. but have done a lot of stuff using VB for Office in various apps. However, when I stated I’d stick with the native form elements provided by Word, I was referring to what you get in the View | Toolbars… Forms toolbar. You have to use the VB Editor in Word to hook up actions on these Word forms (which seem to be a collection of custom field codes) at any rate.

      • #679511

        Finally got around to installing the suggested code. works perfectly! also, if you disenable the ‘target’ form field you can’t tab into it and change any results. very nice.

    Viewing 1 reply thread
    Reply To: Referencing a Bookmark (field) in Macro (Win 2KPro, Office2KPro)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: