PDA

View Full Version : Solved: How to check if a bookmark is empty



TButhe
09-26-2005, 11:48 AM
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. :help

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

Using Word XP and Windows 2000

TButhe
09-26-2005, 01:25 PM
I found this in the help section and pasted it in to my Before Print macro, but it doesn't work.

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

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

Thanks again. :hi:

Bilby
09-26-2005, 03:19 PM
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

MOS MASTER
09-27-2005, 01:28 AM
Is there a problem with doing this before Print? Any ideas?


Hi beauty, :hi:

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

What problems are you having with it? :whistle:

MOS MASTER
09-27-2005, 01:30 AM
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

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, :whistle:

TButhe
09-27-2005, 06:25 AM
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.

:banghead:

TButhe
09-27-2005, 07:03 AM
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.

TButhe
09-28-2005, 08:43 AM
Finally got the file to upload!!:clap2:

TonyJollans
09-28-2005, 09:44 AM
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:Public withevents appWord as Word.application

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 procedurePublic myWord as new Class1

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

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.

TButhe
09-28-2005, 11:02 AM
Tony,
First of all -- THANKS!!!!! I appreciate that you took time to help me out. :beerchug:

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.

TonyJollans
09-28-2005, 12:37 PM
Hi Tracy,

You should be able to look at ..

document_reference.FormFields("formfield/bookmark_name").Result

fumei
09-28-2005, 02:19 PM
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
If ActiveDocument.Formfields("Text1").Result = "" 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.
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

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.

TButhe
10-04-2005, 09:10 AM
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.