Bypassing Document_Open code

R

red6000

Hi,

I have a document that when opened presents a userform and then runs code on
pressing the 'OKAY' button.

What I would like is to amend my code so that the the 2nd time (and
subsequent times) the document is opened, the Document_Open code does not
run?

Is this possible?

Thanks.
 
G

Greg Maxey

I believe you want to have a "template" that runs AutoNew code when you
create a new document.
 
G

Greg Maxey

I don't seem to follow why you can't use a template, but I suppose you could
set a variable when you open your docuement the first time and use it
something like this:

Private Sub Document_Open()
On Error GoTo Handler
StepIn:
If ThisDocument.Variables("Iamborned").Value = _
"I've already been opened before" Then
Exit Sub
Else
MsgBox "Put your existing code here"
ThisDocument.Variables("Iamborned").Value = _
"I've already been opened before"
End If
Exit Sub
Handler:
ThisDocument.Variables("Iamborned").Value = " "
Resume StepIn
End Sub
 
T

Tony Jollans

Let's see if I've got this right.

You create a new document. You add a macro to it. You want that macro to run
the next time you open the document - and only the next time.

So you wrote some code, or got it from somewhere, for a one-time use and you
want to automate that one time?

It's an interesting way of working. Oh, and by the way, you can't have a
document without a template however unsuitable an option you find that.
 
R

red6000

Thanks Greg, that works perfectly!

I'll try and explain what I'm doing.

I have a whole load of code that includes about 30 userforms to create a
letter package which could be 300 pages long if all option are chosen.

Now this document contains various letters. Some of the letters require
payment for the service we are requesting so the macro also creates the 'fee
payment letter' as a seperate document ready for issuing on receipt of the
service requested (with all names and addresses in the letter).

The problem is that some people when they receive our request ask for
payment up front so the 'fee payment' letter has a userform that is run when
the document is opened which asks 'Is this fee payment in advance or in
receipt' and then that generates that correct wording for the letter we then
issue with teh cheque.

I could have used a template, but that would have meant teaching staff on
how to launch he fee payment letter from the appropriate template, so for
ease of use I wanted to have the various fee payment letters as documents
that have the code ran the first time it is opened after being created by
the original code.

Hope that explains all.
 

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