Consulting

Results 1 to 13 of 13

Thread: Solved: How to check if a bookmark is empty

  1. #1
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location

    Question Solved: How to check if a bookmark is empty

    Hi all,

    If I have missed this somewhere I apologize. I am trying to create a form that has text fields in it and they are bookmarked. What I would like is some kind of a macro that checks each bookmark and if it is empty to display an input box that would capture the data necessary and put it in the correct field. I would like this macro on exit, or save or print or all three. What I really can't figure out is how to check to see if the bookmark is empty. If I could just get that much I think I would be able to get the rest.

    If you would please just help me get started I would be so grateful.

    Using Word XP and Windows 2000
    Thanks,
    Tracy

  2. #2
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    I found this in the help section and pasted it in to my Before Print macro, but it doesn't work.

    [VBA]Private Sub appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean)
    If ActiveDocument.Bookmarks.Exists("Date") = True Then
    If ActiveDocument.Bookmarks("Date").Empty = True Then _
    MsgBox "You must enter the date."
    End If
    End Sub[/VBA]

    Is there a problem with doing this before Print? Any ideas?

    Thanks again.
    Thanks,
    Tracy

  3. #3
    VBAX Regular
    Joined
    Sep 2004
    Posts
    65
    Location
    Dunno,

    It works if you use Public Sub FilePrint() in Word VBA, instead of
    appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean) .

    As a quess I'd say the fault was in declaring appWord

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by TButhe
    Is there a problem with doing this before Print? Any ideas?
    Hi beauty,

    I don't understand the question...isn't it working for you? It should work IMO.

    What problems are you having with it?
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  5. #5
    Administrator
    VP-Knowledge Base
    VBAX Guru MOS MASTER's Avatar
    Joined
    Apr 2005
    Location
    Breda, The Netherlands
    Posts
    3,281
    Location
    Quote Originally Posted by Bilby
    Dunno,

    It works if you use Public Sub FilePrint() in Word VBA, instead of
    appWord_DocumentBeforePrint(ByVal Doc As Document, Cancel As Boolean) .

    As a quess I'd say the fault was in declaring appWord
    Hi Bilby,

    Yes the FilePrint() "WordMacro" should work just fine of course.

    But I think the DocumentBeforePrint Application Event should work just as fine. The question however is if appWord was declared properly with events in a classmodule?

    HTH,
    _________
    Groetjes,

    Joost Verdaasdonk
    M.O.S. Master

    Mark your thread solved, when it has been, by hitting the Thread Tools dropdown at the top of the thread.
    (I don't answer questions asked through E-mail or PM's)

  6. #6
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    Well, I tried what Bilby said yesterday (Thanks Bilby) after I posted and he is absolutely right. It works fine if it is in regular Word macro but it doesn't if it is in a Before Print macro. It seems to work too if it is just a bookmark and not a text field with a bookmark. However, I need to have a place for them to enter data on a protected document so I need to have a form field. I really don't want to do a userform just because this is such a simple document (I will if I have to). I have attached the document with some of the stuff taken out but you should get the idea. Not all the fields are required so some of them don't have bookmarks. Thanks for any and all help.

    BTW - the problem is that it just doesn't do anything it prints without giving me the input box.

    Thanks,
    Tracy

  7. #7
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    Ok, I can't seem to get the file attached. I keep getting upload errors each time I try. It is zipped and it is not a large file at all. I tried re-zipping it and that didn't help. I will try later. Thanks.
    Thanks,
    Tracy

  8. #8
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    Finally got the file to upload!!
    Thanks,
    Tracy

  9. #9
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Tracy,

    To set up Application Events you need to do a couple of things ..

    1. Create a New Class Module and put this at the start of it:[vba]Public withevents appWord as Word.application[/vba]

    2. If you now click on the left hand dropdown above the code pane you will be able to select appWord.

    3. Next you can pick the event you want (Documentbeforeclose) from the right hand dropdown. This will generate the skeleton procedure into which you can put your code.

    4. You can name your Class module if you like but, for now, let's assume it has the default name of Class1

    That sets up the code. To make it active you must now:

    1. In a normal code module, enter this before the first procedure[vba]Public myWord as new Class1[/vba]

    2. Add a sub:[vba]Sub AutoNew()
    Set myWord.appWord = Word.Application
    End Sub[/vba]

    Now save your template with all that in it and next time you create a document from it your before close event should be enabled.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  10. #10
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    Tony,
    First of all -- THANKS!!!!! I appreciate that you took time to help me out.

    I got your steps to work with just a bookmark and no form field. However I can't get it to work with with a text form field that has a bookmark. (I'm selecting Text Form Field from the Forms toolbar and then putting the bookmark in the properties of the form field) Any ideas? Should I be checking the form field instead of the bookmark to see if it is empty? If so, I haven't got any idea how to do that but I haven't looked for it yet either. I will start looking for that right now and I will post back if I get anywhere with it. Thanks again.
    Thanks,
    Tracy

  11. #11
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Hi Tracy,

    You should be able to look at ..

    document_reference.FormFields("formfield/bookmark_name").Result
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  12. #12
    VBAX Wizard
    Joined
    May 2004
    Posts
    6,713
    Location
    Seems to be a little confusion here.

    Formfields have their own Collection, and can be referenced directly using it.

    ActiveDocument.Formfields("Text1").Result returns the value. So
    [vba]If ActiveDocument.Formfields("Text1").Result = ""[/vba] is a test to see if the text formfield "Text1" is blank.

    Formfields are ALSO part of the Bookmarks Collection, and can be referenced using that. However, because when referencing the Bookmark Collection, you reference it as a field, you get the field codes as well. Perhaps the following may help.

    Document has ONE formfield, a text formfield named Text1.
    [vba]Sub ShowValue()
    Dim FieldResult As String
    ActiveDocument.Bookmarks("Text1").Select
    FieldResult = Selection.Text
    Selection.Collapse Direction:=wdCollapseEnd
    MsgBox ActiveDocument.FormFields("Text1").Result & vbCrLf & _
    ActiveDocument.Bookmarks("Text1").Range.Text & vbCrLf & _
    FieldResult
    End Sub[/vba]

    Say you type "Bob" into the formfield, and the above code is set as the Onexit macro. The messagebox will display:

    Bob
    FormText Bob
    Bob

    The first line is the .Result
    The second line is the .Range.Text
    The third line is the text of ther Selection, after the formfield is selected.

  13. #13
    VBAX Regular TButhe's Avatar
    Joined
    Sep 2004
    Location
    Sioux Falls, SD
    Posts
    64
    Location
    I haven't forgotten about this just won't be able to work on it for a while. Thanks for all your suggestions. I'll post back when I get back to this.
    Thanks,
    Tracy

Posting Permissions

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