• User Fields in Word (Word 2000)

    Author
    Topic
    #366568

    Hi,

    My first post……..

    Does anybody know how to define a field in WORD, prompt the person who opens the document for some input, which will be assigned to the defined field and then use this field throughout the document with the entered data.

    I have looked at ASK and FILL-IN options but they do not give me what I want, I am happy to use macros if required.

    Many thanks

    Mike

    Viewing 1 reply thread
    Author
    Replies
    • #568884

      One way is to use a Document_OPen macro in the ThisDocument module of the document.

      1. Query the user when the document is opened.
      2. Stick the input somewhere in the document.
      3. Assign a bookmark to the inserted info.
      4. Use REF fields to refer to the bookmark.

      • #568898

        Hi Howard

        Thanks for your input, I have tried the first step and that has not worked, my code is below

        Private Sub App_DocumentOpen(ByVal Doc As Document)

        Dim strCountry As String
        Dim strPerson As String

        strCountry = InputBox(“Please enter Your name”)

        strPerson = InputBox(“Please enter your Country”)

        End Sub

        I have not gone any further as I thought I would test the macro first, can you see anything I have missed.

        Regards

        Mike

        • #569078

          Mike,

          Probably not a bad idea to check out the link Charles has posted.
          But since you’ve got a near-working macro, let’s take a look at getting it to work.

          First thing: “Auto Open” macros – Word provides no less than three different ones: “AutoOpen” – which needs to go into a standard code module, “Document_Open”, which needs to go into the ThisDocument code module (as Howard pointed out) and “App_DocumentOpen”, which needs to be declared With Events and put into a class module.

          Of these three, “App_DocumentOpen” is the only one you don’t want to use here! (It has a special use – usually put in a global template in the Startup directory, so that you can have custom code run every time any Word document is opened.)

          AutoOpen and Document_Open are essentially the same; there are slight differences, just don’t ask what they are! (Actually here’s one difference: the AutoOpen macro will appear in the Macros dialog list, while Document_Open will not – reason enough to use Document_Open.)

          Another thing to consider is where do you put this macro i.e. are you putting it an actual document, or in the template upon which new documents are based. It’s almost always best to do the latter.

          So we’re going to put this Document_Open macro into the ThisDocument module of a template.
          There’s still one more preliminary: we don’t always want this macro to run! – we want it to run when a document, based on the template, is opened, but we don’t want it to run when we open the template itself – so we are going to need to add some code into the macro to prevent it from running when we are working in the template itself.

          Actually here’s yet one more preliminary: we need to put bookmarks into the document, to receive the input the user types into the input boxes.
          So in the template, use Insert > Bookmarks to add two bookmarks, where you want the Country and Person text to appear (let’s call them “bmkCountry” and “bmkPerson”.

          With all of those preliminaries taken care of, the revised code for your procedure would look like:

          Private Sub Document_Open()
          
          Dim strCountry As String
          Dim strPerson As String
          
          If ThisDocument.Type  wdTypeTemplate Then
              strCountry = InputBox("Please enter Your name")
              strPerson = InputBox("Please enter your Country")
              
              ActiveDocument.Bookmarks("bmkCountry").Range.Text = strCountry
              ActiveDocument.Bookmarks("bmkPerson").Range.Text = strPerson
          End If
          
          End Sub

          Gary

          • #569703

            Hi Gary,

            Thanks for your input, it has helped me get a little further, I would like to extend it slightly as I am still having lots of very simple issues and it is a bit frustrating.

            I have included your suggestions and made some modification. I have removed the check to see if this is a template document or not for the moment.

            The request 2 prompt boxes work fine now and the data goes into the assigned bookmarks. But when I try to look at the data in the bookmarks or use a REF option in my document I get nothing. I have also tried to find the start and end of the bookmark without any success. If I jump to the bookmark in my macro that works but the others do not, see all my code option below.

            My final objective is to get 2 pieces of information, put them is a word document, use them in several place within the document, but if the data has already been put in the document then do not ask for it again, I hope that makes sense……

            Many thanks for your help.

            Regards

            Mike

            Private Sub Document_Open()

            Dim strCountry As String
            Dim strPerson As String
            Dim strCountryOrig As String
            Dim strPersonOrig As String
            Dim strBookStart As String
            Dim strBookEnd As String

            ActiveDocument.Bookmarks(“bmkCountry”).Select

            intBookStart = ActiveDocument.Bookmarks(“bmkCountry”).Range.Start
            intBookEnd = ActiveDocument.Bookmarks(“bmkCountry”).Range.End

            strCountryOrig = ActiveDocument.Bookmarks(“bmkCountry”).Range.Text
            strPersonOrig = ActiveDocument.Bookmarks(“bmkPerson”).Range.Text

            If ActiveDocument.Bookmarks(“bmkCountry”).Empty = True Then

            strCountry = InputBox(“Please enter Your name”)
            strPerson = InputBox(“Please enter your Country”)

            ActiveDocument.Bookmarks(“bmkCountry”).Range.Text = strCountry
            ActiveDocument.Bookmarks(“bmkPerson”).Range.Text = strPerson

            End If

            End Sub

            • #569894

              Edited by Gary Frieder on 12-Feb-02 00:41.

              Hi again Mike,

              My fault, as I didn’t think through the implications of the Ref fields needing to read the contents of the bookmarks.

              The Ref field needs to read the content that’s ‘inside’ the bookmark, but the method I’ve provided doesn’t put the text ‘inside’ the boomkark.

              If you’ve got an empty (I-beam) bookmark, then

              ActiveDocument.Bookmarks(“bmkTest”).Range.Text = “Text”

              will put the word ‘Text’ to the right of the bookmark, not inside it.

              If you’ve got a ‘wrapped’ bookmark (say a bookmark wrapped around a space), then the above code will put in ‘Text’ but will delete the bookmark.

              In both cases, the Ref field will not pick up this text.

              What’s needed is a method to insert the text, but to make sure that it ends up ‘wrapped inside’ the bookmark. There are a number of custom functions floating around to do this; the following is the simplest one I’ve come across (apologies I can’t give a credit for this – I saw it quite a long time ago and didn’t note where).

              Public Function fInsertWithBookmarks(BmkName As String, _
                                                  InsertText As String) _
                                                              As Boolean
              Dim rngBmk As Range
              'First test that bookmark exists
              If ActiveDocument.Bookmarks.Exists(BmkName) Then
                  Set rngBmk = ActiveDocument.Bookmarks(BmkName).Range
                  rngBmk.Text = InsertText
                  'this overwrites the bookmark, so need to add it again:
                  ActiveDocument.Bookmarks.Add Name:=BmkName, Range:=rngBmk
                  Set rngBmk = Nothing
                  fInsertWithBookmarks = True
              Else
                  MsgBox BmkName & " bookmark doesn't exist in this document. "
              End If
              
              End Function
              

              You would call this function, in each place in your procedure where you want to insert the text into the bookmark, for example:

              fInsertWithBookmarks “bmkCountry”, strCountry
              fInsertWithBookmarks “bmkPerson”, strPerson

              Hope this helps,
              Gary

            • #569904

              Thanks Gary,

              That has now solved my problem, everything is now working as I want.

              Thanks for your help and everybody who has replied.

              Regards

              Mike

    • #568939

      Hi Mike,

      What is it that you want to do that the ASK field isn’t doing for you? Where you want to use the field information in the document, insert a REF field. Take a look at the Ask Field Tutorial from my download page. It has samples as well as the enabling macros.

    Viewing 1 reply thread
    Reply To: User Fields in Word (Word 2000)

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

    Your information: