Results 1 to 8 of 8
  1. #1
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    224
    Thanks
    0
    Thanked 0 Times in 0 Posts

    User Fields in Word (Word 2000)

    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

  2. #2
    Gold Lounger
    Join Date
    Dec 2000
    Location
    New Hampshire, USA
    Posts
    3,386
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: User Fields in Word (Word 2000)

    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.

  3. #3
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    224
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: User Fields in Word (Word 2000)

    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

  4. #4
    Silver Lounger Charles Kenyon's Avatar
    Join Date
    Jan 2001
    Location
    Sun Prairie, Wisconsin, Wisconsin, USA
    Posts
    2,048
    Thanks
    124
    Thanked 119 Times in 116 Posts

    Re: User Fields in Word (Word 2000)

    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 <A target="_blank" HREF=http://addbalance.com/word/download/index.htm#AskField> Ask Field Tutorial</A> from my download page. It has samples as well as the enabling macros.
    Charles Kyle Kenyon
    Madison, Wisconsin

  5. #5
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: User Fields in Word (Word 2000)

    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:

    <pre>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</pre>

    Gary

  6. #6
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    224
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: User Fields in Word (Word 2000)

    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

  7. #7
    Super Moderator
    Join Date
    Dec 2000
    Location
    New York, NY
    Posts
    2,970
    Thanks
    3
    Thanked 29 Times in 27 Posts

    Re: User Fields in Word (Word 2000)

    <P ID="edit" class=small>Edited by Gary Frieder on 12-Feb-02 00:41.</P>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).

    <pre>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
    </pre>

    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

  8. #8
    3 Star Lounger
    Join Date
    Nov 2001
    Posts
    224
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: User Fields in Word (Word 2000)

    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

Posting Permissions

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