Check for Bookmark then run userform

L

Lee

I want to write some code that will check to see if a
bookmark exists and if it does, run a userform. Once the
user has filled in the userform and clicked OK, I want the
code to find the bookmark and delete it.
If the bookmark doesn't exist, then I don't want the code
to run...

I have the following code:
Sub Autonew()
If ActiveDocument.Bookmarks.Exists("new") = True Then
FrmOutwardLetter.Show
End If
End Sub

and within the userform code I have the following code to
delete the bookmark:
ActiveDocument.Bookmarks("new").Select
Selection.Delete

BUT nothing seems to be working! The userform doesn't
show anymore - What am I doing wrong? (It's probably all
wrong!).

I was trying to create a custom boolean property, set its
value to false and change it to true once the userform
runs. Then always check for the value of the said
property when document is opened, and if it is true, do
not run the code. Wasn't sure how to do this as this is
all new to me... :eek:(

Thanks in advance
Lee
 
J

Jezebel

The first part of your code should certainly work to show the form if the
bookmark exists. If your form is not displaying, put a stop statement into
your code and check at that point: does the bookmark actually exist?

However, a bookmark is a poor choice for this. A DocProperty or DocVariable
would be a better choice.

The code that deletes the bookmark will work, but I'm not sure that it does
what you want: for purposes of your code you want to delete the bookmark
itself. What you have deletes the range of your document to which the
bookmark applies. Simpler is
 
M

Malcolm Smith

Oops, Jezebels's code seems to have vanished, I think that he means (if he
will allow me to continue with his work) to have the following code in the
form's code:


If ActiveDocument.Bookmarks.Exists("new") Then
ActiveDocument.Bookmarks("new").Delete
End If

Note that I check again for the bookmark. Yes, I know that it's been
checked for before but now we're leaping into another class and who knows
if in future this class will be called from elsewhere where the bookmark
has not been checked.

Secondly, note that I didn't need to ask if the If statement were True.
This is because all conditional statements work on a Boolean result; i.e.
if True then enter else don't. And since the .Exists property of the
Bookmarks collection returns a Boolean then there's no point in
effectively saying:

If True = True then

That's it really. Hope that this helps.

- Malc
www.dragondrop.com
 
L

Lee

I have tried a docvariable but can't seem to get it to
work...
I tried:
If ActiveDocument.Variables("new").Value = 0 Then
FrmOutwardLetter.Show
End If
and I had a variable in the document named "new".

This is probably wrong... Don't know how to do this as I'm
new at VBA. Can anyone help with some code please?

Thanks
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top